<p>In a web application, is it acceptable to use HTML in your code (non-scripted languages, Java, .NET)?</p>

<p>There are two major sub questions:</p>

<ol>
<li>Should you use code to print HTML, or otherwise directly create HTML that is displayed?</li>
<li>Should you mix code within your HTML pages?</li>
</ol>

[{'answer_id': 64764, 'author': 'Chris Broadfoot', 'author_id': 3947, 'author_profile': 'https://Stackoverflow.com/users/3947', 'pm_score': 5, 'selected': True, 'text': '<p>Generally, it\'s better to keep presentation (HTML) separate from logic ("back-end" code). Your code is decoupled and easier to maintain this way.</p>\n'}, {'answer_id': 64769, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>Its fugly, and not type safe.  But people do it without consequence.  I'd prefer using a DOM or, at a minimum, classes designed to write HTML using type safe semantics.  Also, its not all that good to mix UI with logic...</p>\n"}, {'answer_id': 64776, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 3, 'selected': False, 'text': '<p>As long as your HTML-writing code is separate from your application logic, and the HTML is guaranteed to be well-formed somehow, you should be okay.</p>\n\n<p>The only code that should be mixed in markup-based pages (i.e, those that contain literal HTML) is the code used for formatting the HTML (e.g., a loop for writing out a list). </p>\n\n<p>There are trade-offs whether you put the code in with the HTML or you use pure code to write the HTML out using quoted string literals.</p>\n'}, {'answer_id': 64780, 'author': 'Ivan Bosnic', 'author_id': 3221, 'author_profile': 'https://Stackoverflow.com/users/3221', 'pm_score': 1, 'selected': False, 'text': '<p>No, if you want to build good and maintainable software, and to achieve loose coupling.</p>\n'}, {'answer_id': 64800, 'author': 'David Thibault', 'author_id': 5903, 'author_profile': 'https://Stackoverflow.com/users/5903', 'pm_score': 0, 'selected': False, 'text': '<p>If I need methods that generate HTML I usually isolate them in an HtmlHelpers class. That way you keep <em>some</em> level of separation. The ASP.NET MVC Framework does this quite successfully.</p>\n'}, {'answer_id': 64812, 'author': 'Sören Kuklau', 'author_id': 1600, 'author_profile': 'https://Stackoverflow.com/users/1600', 'pm_score': 1, 'selected': False, 'text': '<p>If I understand the question right, you\'re asking whether it\'s a good practice to mix markup with back-end code. No. While this is commonly done, it\'s still a bad idea.</p>\n\n<p>You should read up on the <a href="http://en.wikipedia.org/wiki/Model-view-controller" rel="nofollow noreferrer">MVC</a> paradigm, as well as on existing questions on the matter, such as <a href="https://stackoverflow.com/questions/40242/what-is-the-best-way-to-migrate-an-existing-messy-webapp-to-elegant-mvc">What is the best way to migrate an existing messy webapp to elegant MVC?</a> and <a href="https://stackoverflow.com/questions/23899/best-practices-for-refactoring-classic-asp">Best practices for refactoring classic ASP?</a></p>\n'}, {'answer_id': 64819, 'author': 'asksol', 'author_id': 5577, 'author_profile': 'https://Stackoverflow.com/users/5577', 'pm_score': 0, 'selected': False, 'text': '<p>If you mean printing out HTML in your code, then no. Unless you have a good reason not to, you should use <a href="http://en.wikipedia.org/wiki/Template_engine_&#40;web&#41;" rel="nofollow noreferrer">templates</a></p>\n\n<p>Even if you think you don\'t need this now, there\'s always a good chance you\'ll need it later. Maybe you want to output in a different format than HTML, or you want different presentation for the same data. You usually have the need for these things further down the road, so it\'s best to use one from the start.</p>\n'}, {'answer_id': 64835, 'author': 'JW.', 'author_id': 4321, 'author_profile': 'https://Stackoverflow.com/users/4321', 'pm_score': 1, 'selected': False, 'text': "<p>The point is to keep the display logic separate from the rest of the code. In any complex site you'll have code mixed in with your HTML, but the code should be for display purposes only. It shouldn't be doing any complex calculations.</p>\n\n<p>For example, templates will contain loops and conditionals. Plus you'll probably have a library of HTML-specific routines, like printing out an &lt;option&gt; list based on a list object.</p>\n\n<p>Imagine you were writing an application that has two output modes: HTML and something else. How would you write it, to avoid duplicating code? That will probably point you in the right direction.</p>\n"}, {'answer_id': 64838, 'author': 'dawnerd', 'author_id': 69503, 'author_profile': 'https://Stackoverflow.com/users/69503', 'pm_score': 0, 'selected': False, 'text': "<p>I hate when developers print() a bunch of html. It's completely unnecessary and looks ugly in any text editor that shows print/echo strings in red.</p>\n"}, {'answer_id': 64849, 'author': 'Larry Foulkrod', 'author_id': 1642688, 'author_profile': 'https://Stackoverflow.com/users/1642688', 'pm_score': 1, 'selected': False, 'text': '<p>The HTML that makes up the view has to get sent to the browser in some way.  In .net, each server control emits its own HTML markup as part of the page lifecycle.  So yes it is OK to use HTML in server side code.</p>\n\n<p>Perhaps you should try following the ASP.net pattern.  Create a bunch of controls that represent UI elements and make them responsible for emitting their own HTML based on their state.</p>\n'}, {'answer_id': 64896, 'author': 'rjzii', 'author_id': 1185, 'author_profile': 'https://Stackoverflow.com/users/1185', 'pm_score': 0, 'selected': False, 'text': '<p>I agree with everyone else that you should try as hard as you can to separate the HTML/XHTML markup from the application logic. However, sometimes you do need to generate HTML/XHTML in the application logic for various reasons. </p>\n\n<p>In these cases what I have been trying to do is to ensure the bare minimum amount of presentation code is in mixed in with the application logic and try to migrate everything else over to the presentation code. It is worth nothing that is some cases you have situations where you could have everything moved over to the presentation layer, but it might be a bit easier to generate the markup as part of the application logic. In those cases, your best bet is likely to be to go the route that makes the most sense in terms of time.</p>\n'}, {'answer_id': 64932, 'author': 'Lucas Oman', 'author_id': 6726, 'author_profile': 'https://Stackoverflow.com/users/6726', 'pm_score': 0, 'selected': False, 'text': '<p>I don\'t think there\'s any excuse for generating HTML inside your business logic. Don\'t even do it when it\'s just a "quick fix" or when you\'ll "go back and fix it later", because that never happens.</p>\n\n<p>To reiterate my position from other questions, using some control logic (conditionals, loops) within HTML to construct it is OK. Do NOT do any data massaging or business logic in the HTML. You have to be disciplined, but it\'s worth it. Maintenance is much easier if your concerns (like logic and display) are separated.</p>\n'}, {'answer_id': 65086, 'author': 'Xian', 'author_id': 4642, 'author_profile': 'https://Stackoverflow.com/users/4642', 'pm_score': 0, 'selected': False, 'text': '<p>Ideally you are aiming for a <strong>separation of concerns</strong> between your presentation (UI) code and your domain (business logic) code.</p>\n\n<p>The reason why you should avoid coupling these two concerns (in either direction) is simple...</p>\n\n<p>You will only have <strong>one reason to change</strong> a piece of code. whether this is from structural/styling changes in your html design, or from your business rules changing, you should only have to make the change in one place.</p>\n\n<p>To a lesser extent, although many purists would disagree, by sprinkling HTML code through your domain code or vice versa you are <strong>creating noise</strong> for the next developer who comes along to read/maintain it. </p>\n'}, {'answer_id': 65358, 'author': 'Brian Kim', 'author_id': 5704, 'author_profile': 'https://Stackoverflow.com/users/5704', 'pm_score': 0, 'selected': False, 'text': '<ol>\n<li>I try to <strong>avoid using code to print HTML "directly"</strong>. It is difficult to maintain, edit, add styles and etc. Some cases like generating an HTML email in the code, I create a text file or HTML file with markers like, [name], [verification code] and etc. I load this from the code and replace those markers. This way, you can edit the style of the email without re-compiling your code. <strong>Separating "presentation" and "logic" is a good practice in my opinion.</strong></li>\n<li><strong>Mixing code within HTML is generally not a good practice</strong> in similar reasons as said in #1. However, I do use code in HTML for things like simple dynamic strings that are displayed multiple times on a page or pages. I think this is better than creating multiple server controls for same exact values to set. Since this is not code "logic" mixed in the HTML, I think this is ok.</li>\n</ol>\n'}]
['https://Stackoverflow.com/questions/64760', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/8509/']