How do web templates separate content and logic?

Tim Roberts timr at probo.com
Sun Jun 29 23:27:00 EDT 2008


John Salerno <johnjsal at gmailNOSPAM.com> wrote:
>
>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"). 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>
>
>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.

Technically, you are probably right, but a model like MVC is supposed to
enable better programming.  It's not intended to be straightjacket and
handcuffs.  If that snippet makes sense to you, then there's nothing wrong
with it.

What's the alternative?  The alternative is to have your Python code do
something like this:

    topicList = []
    for topic in topics:
        topicList.append( topic )
    topicList = '</li><li>'.join( topicList )
and have your HTML page be:
    <ol>
      <li>${topicList}</li>
    </ol>

but now I've put chocolate into my peanut butter by embedding <li> tags in
my Python code.

>So maybe my question was a little premature. 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? (For example, connecting to a database, like 
>Sebastian's example. That definitely seems out of place in an HTML file.)

If it seems out of place to you, then you shouldn't do it.  In general, you
need to find a model that makes sense to you, and that allows you to write
readable, workable, maintainable code.
-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-list mailing list