views:

206

answers:

5

I have a web-app consisting of some html forms for maintaining some tables (SQlite, with CherryPy for web-server stuff). First I did it entirely 'the Python way', and generated html strings via. code, with common headers, footers, etc. defined as functions in a separate module.

I also like the idea of templates, so I tried Jinja2, which I find quite developer-friendly. In the beginning I thought templates were the way to go, but that was when pages were simple. Once .css and .js files were introduced (not necessarily in the same folder as the .html files), and an ever-increasing number of {{...}} variables and {%...%} commands were introduced, things started getting messy at design-time, even though they looked great at run-time. Things got even more difficult when I needed additional javascript in the or sections.

As far as I can see, the main advantages of using templates are: Non-dynamic elements of page can easily be viewed in browser during design. Except for {} placeholders, html is kept separate from python code. If your company has a web-page designer, they can still design without knowing Python.

while some disadvantages are: {{}} delimiters visible when viewed at design-time in browser Associated .css and .js files have to be in same folder to see effects in browser at design-time. Data, variables, lists, etc., must be prepared in advanced and either declared globally or passed as parameters to render() function.

So - when to use 'hard-coded' HTML, and when to use templates? I am not sure of the best way to go, so I would be interested to hear other developers' views.

TIA, Alan

+1  A: 

I would highly recommend using templates. Templates help to encourage a good MVC structure to your application. Python code that emits HTML, IMHO, is wrong. The reason I say that is because Python code should be responsible for doing logic and not have to worry about presentation. Template syntax is usually restrictive enough that you can't really do much logic within the template, but you can do any presentation specific type logic that you may need.

ymmv.

Matthew J Morrison
Yes, since a web developer can take care of the templates, you can focus on the coding.
Prozaker
+1  A: 

I think that templates are still the best way to separate presentation from business logic. The key is a good templating engine, in particular, one that can make decisions in the template itself. For Python, I've found the Genshi templating engine to be quite good. It's used by the Trac Wiki/Issue tracking system and is quite powerful, while still leaving the templates easy to work with.

For other tasks, I tend to use the BeautifulSoup module. I'll create a simple HTML page, parse it with BeautifulSoup, use the resulting object to add the necessary data, and then write the output to its destination (typically a file for me).

Craig Trader
No. You need presentation logic. That should be as powerful as your business logic, in order to DRY. Be OO, be debugable, testable. That no longer looks like html
Stephan Eggermont
@Stephan Eggermont - what is an example of a piece of "presentation logic" that would be as "powerful" as business logic? "presentation logic" is really putting "things" in "places" - business logic is doing calculations, checking conditions and deriving values - when would you need to do things that complex for the presentation of data?
Matthew J Morrison
e.g. client side validation, which can be (partially) derived from the business rules
Stephan Eggermont
Client-side validation is nice to have (it frequently makes the application more usable), but you still have to have that validation in the business layer. Way too many web applications have used JavaScript to 'enforce' application limits, only to have someone manually craft a URL that ignores those restrictions.If you want to generate pieces of validation code from your application specifications, that's fine, but you shouldn't have to modify your application code just because your company changed its color palette, or because someone discovered a new vulnerability in Internet Explorer.
Craig Trader
That is exactly why you don't want a (non-lisp) template system. With code I can declare once and derive client side, server side validations and database constraints.
Stephan Eggermont
You can achieve that result with lots of tools. Templates have worked well for me for a very long time, but I'm not asking you to use them.
Craig Trader
+5  A: 

Although I'm not python developer, I'll answer here - I believe the idea of using templates is common for PHP and Python.

Using templates has many many many adventages, like

  • keeping the code clean. Separating the "logic" (controller) code from "view" is very important, believe me that working on projects that mix HTML / CSS / JS output is really hard.
  • keeping HTML in separate files doesn't require you to modify the code itself. For example, placing in the controller code (python code) might require you to put slash before each " character.
  • you can ask your webdesigner to learn basics of templates syntax, thus he's able to help you much without destroying your work on logics (which is quite common when a person with no experience in given language modifies something)

in fact there are many more adventages, but those are most important for me.

The only disadventage is that you have to pass parameters to the rendering function ... which doesn't require much of work, believe me :) Anyway it's much easier than maintaining any project that mixes logics with view.

Generally you should have a look at >MVC pros and cons question< : http://stackoverflow.com/questions/26685/what-is-mvc-and-what-are-the-advantages-of-it

migajek
It is easier to keep code clean in Seaside than in any of the template systems I've seen.
Stephan Eggermont
@Stephan Eggermont I would agree that is probably true for a developer, and probably untrue for a designer. The point of templating systems is to allow a designer to do design work and have a developer to be able to use it instead of taking a design and re-writing it using some other syntax to create the original result.
Matthew J Morrison
We need an efficient way to work together. Templates are a failure.
Stephan Eggermont
+1  A: 

As a Seaside developer, I see no use for templates, if your designer can do css. In practice, I find it impossible to keep templates DRY. Take a look at this question to see the advantages of using code (a DSL) to generate your page. You might be bound by legacy, of course.

Separation of presentation (html) and business logic in web apps does not lead to good modularisation, i.e. low coupling and high cohesion. That is why separate template systems don't work well.

I just read Jeff Atwoods "What's Wrong With CSS". This again is a problem long solved in the smalltalk world with the Phantasia DSL

Stephan Eggermont
I agree that a DSL isn't always a good idea for a templating language... however, Genshi, Jinja2 or Django's templating framework is pretty much just straight HTML with a few special tags. These types of templates fix a lot of the issues that may arise using a DSL.
Matthew J Morrison
Huh? I'm claiming there is no place for templates. The Seaside DSL is fine for generating HTML (and javascript), and integrated.
Stephan Eggermont
Part of the point of a templating system is that the presentation layer may vary wildly. Why should I rely on auto-generated HTML when the desired output might not even BE HTML, or might be a version of HTML that the generator doesn't support? I'm not sure what problem you're trying to solve, but it's certainly no problem I've ever encountered.
Nicholas Knight
Sorry, I misread your post... so you're saying that with a DSL you can write your code once and it will automatically know how to render a UI for user interaction, RSS/ATOM, and XML for Soap/REST requests? If that's the case, and a single DSL can do that... that's pretty cool! Where do I sign up?
Matthew J Morrison
The comparison I think Stephan is making is between a relatively uncomposed set of templates (easy for HTML monkeys to work with, not very DRY) to a system where you're using reusable code components from the top all the way down to the little bits and pieces. However, you can do components all the way down using templates too, though perhaps not as elegantly. Stephan's role of designer as CSS-only may not be what works in other shops, though, especially where there's lots of static content.
Owen S.
If you do components all the way in templates, the designer no longer can work with them, and it doesn't look like html anymore. Matthew: take a look at Magritte in the seaside book: http://book.seaside.st
Stephan Eggermont
Everybody should rely on generated HTML, as it is much more likely to be correct... But there are also LaTeX, PDF and XML generators, and it is much easier to change the generator in one place than 100 page templates
Stephan Eggermont
+4  A: 

The simplest way to solve your static file problem is to use relative paths when referring to them in your html. For example: <img src="static/image.jpg" />

If you're willing to put in a little more work, you can solve all the design-time problems you mentioned by writing a mini-server to display your templates.

  1. Maintain a file full of simple data structures containing example values for all your templates.
  2. Use a microframework like Werkzeug to serve http on your local machine.
  3. Write a root request handler that scans your data structure list or templates directory to produce an index page with links to all your templates.
  4. Write a secondary request handler for non-root requests, which renders the requested template with the data structure of the same name.

You can write this tool in a few hours, and it makes template design very convenient. One nice feature of Werkzeug's built-in wsgi server is that it can automatically reload itself when it detects that a file has changed. You can leave your mini-server running while you edit templates and click links on your index page all day.

Forest
Nice idea Forest - on your suggestion I've just written a small prog. using CherryPy to display the template as it should look, css files, test-data and all... Good idea re. having an index page listing all files within a single folder. Regards.
Alan Harris-Reid