How do web templates separate content and logic?

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Mon Jun 30 16:57:50 CEST 2008


John Salerno a écrit :
> bruno.desthuilliers at gmail.com wrote:
> 
>> For which definitions of "content" and "logic" ???
>>
>> The point of mvc is to keep domain logic separated from presentation
>> logic, not to remove logic from presentation (which just couldn't
>> work). Templating systems are for presentation logic. Whether they
>> work by embedding an existing complete programmation language or by
>> providing they're own specialised mini-language (or a mix of both) is
>> not the point here IMHO.
> 
> No, I don't mean presentation logic at all. I mean something along the 
> lines of combining HTML (which is what I refer to as "content") and 
> Python (which is what I meant by "logic").

Some (if not most) templating systems use their own mini-language to 
handle presentation logic.

> So for example, if you have 
> code like this (and this isn't necessarily proper code, I'm just making 
> this up, but you'll see what I mean):
> 
> <body>
>   <h1>Big Important Topic</h1>
>     <p>This is where I say something important about</p>
>       <ol>
>       % for topic in topics:
>           <li>${topic}</li>
>       </ol>
> </body>


In Django's template system, this would looks like:

<body>
   <h1>Big Important Topic</h1>
     <p>This is where I say something important about</p>
     <ol>
       <!-- no, this is not Python -->
       {% for topic in topics %}
         <li>{{ topic }}</li>
       {% endfor %}
      </ol>
</body>

In ZPT, it would be:

<body>
   <h1>Big Important Topic</h1>
     <p>This is where I say something important about</p>
     <ol>
      <tal:repeat repeat="topic topics">
       <li tal:content="topic">Yadda</li>
      </tal:repeat>
     </ol>
</body>


> Humph, I just made up that example to make the point that when you no 
> longer have pure HTML, but instead have programmatic logic (Python) 
> mixed in with the HTML, then you are mixing content and logic.
> 
> However, as soon as I finished typing it out, it occurred to me that 
> even the so-called logic in this example is really only producing more 
> "content" to display.

Indeed.

> So maybe my question was a little premature.

The meme "thou shall not mix domain logic with presentation" is very 
often misunderstood as "you must not have anything else than html in 
templates", which is just plain non-sense. Even declarative templating 
systems (cf Has's post) require some special (ie: non standard) stuff to 
work.

> Or could it just be that 
> this is a *good* way to mix HTML and Python, and there are other ways 
> which may be bad?

Bingo.

> (For example, connecting to a database, like 
> Sebastian's example. That definitely seems out of place in an HTML file.)

Yeps.



More information about the Python-list mailing list