Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

As far as I can see there are two ways of building a html page: \"inside\" the l

ID: 651976 • Letter: A

Question

As far as I can see there are two ways of building a html page: "inside" the language (I'll call it building for lack of a better name) or outside the language (using templates). Let me explain a bit what I mean by this before I jump to the questions.

Building
One way is to build the html page "inside" the language you are using, using preferably in-language constructs (not just by concatenating strings), and converting these constructs to html in one last step.

A really good example of this is Clojure's hiccup library, which uses the Clojure maps and vectors to represent the html. This allows one to use the full power of the language one's working in to manipulate these structures.

Templating
The second way is to write the html separately from the main program and use some kind of template DSL to insert logic into the templates. The hamlet package for Haskell is an example of this.

Now my question is: what are other advantages and disadvantages of both approaches?

I'd love to get more acquainted with these concepts so pointers to articles etc. are more than welcome. Also please inform me if there is another approach I didn't think of.

Summary of answers so far
Pro templating
It's easier to separate the view from the rest of the code. The code will be easier to maintain and you avoid mixing business logic with the rest of your code.
This approach generally is much more accessible to people with less technical skills.
Con templating
(none so far)

Pro building
Functional languages, make it easy to write declarative code natively so a template language is not required.
You can get all the power and flexibility of your programming language, and still get the benefits of a declarative syntax.
Con building
Designers/clients are bad at your preferred logic language.
It still boils down to hard-coding the page into codem just that it is not separate template code.
Has more power, that could be used inappropriately. You have to take extra care
to separate view code and business logic.
In ideal cases it is faster though.

Explanation / Answer

Template languages provide a declarative syntax, which is much less error-prone for this particular use case than an imperative syntax. In other words, you're specifying what the end result looks like rather than step by step instructions for how to build it. That's why template languages are so popular when the back end is written in a more imperative language.

On the other hand, functional languages, especially in the Lisp family, make it very easy to write declarative code natively, so a template language is not really required. You can get all the power and flexibility of your programming language, and still get the benefits of a declarative syntax.

However, if you have more power, that tempts you to use it inappropriately. You have to take extra care to separate view code and business logic, or else you will end up with something very difficult to maintain.

The other huge drawback is some of the best designers are not very good programmers. Even a lot of good object oriented programmers get lost in functional languages. If you want to work with those designers without a workflow where they always hand off an HTML file to a programmer for transcription, then you probably want to stick with a dedicated template language.