Templating engine?

H

has

Daniel Ellison said:
Who said ElementTree was a generic DOM system?

Oops. Sorry. Should've said "DOM-style", seeing as the acronym "DOM"
is imbued with rather specific meaning. Basically, I mean anything
that represents a document as an object model.
Admittedly, the just-in-time lookup on IDs is less than ideal. Better
would be a system which cached the elements containing IDs in a
dictionary on parsing the document.

Have you looked at PyMeld/Nevow.renderer/HTMLTemplate at all? These
systems only convert "special" elements into independent template
nodes, resulting in a much simpler, more efficient object model.

HTH
 
H

has

Daniel Ellison said:
Who said ElementTree was a generic DOM system?

Oops. Sorry. Should've said "DOM-style", seeing as the acronym "DOM"
is imbued with rather specific meaning. Basically, I mean anything
that represents a document as an object model.
Admittedly, the just-in-time lookup on IDs is less than ideal. Better
would be a system which cached the elements containing IDs in a
dictionary on parsing the document.

Have you looked at PyMeld/Nevow.renderer/HTMLTemplate at all? These
systems only convert "special" elements into independent template
nodes, resulting in a much simpler, more efficient object model.

HTH
 
P

Paramjit Oberoi

Presentation logic is application code too, regardless of where you
put it and what language it's written in.

- The advantage of embedding logic in markup is it's easier for
non-programmers and other folk who aren't good at/don't like working
in the abstract to see what's going on. (i.e. easier mental modelling)

- The advantage of separating the two is very designer-friendly markup
and complete freedom in how you design and implement the logic. (i.e.
easier maintainance)

With HTMLTemplate et-al, there's nothing to stop you putting the
controller code in separate modules to the model code, making it easy
to edit the former without messing with the latter. (Splitting each
layer into its own module is probably a good design practice anyway.)

You are right: there are three pieces to be managed (1)application logic,
(2)presentation logic, and (3)markup. MY understanding of HTMLTemplate is
that it enforces the separation of markup and all logic (both presentation
and application), which is certainly very designer friendly.

The problem is that:

(1)presentation logic is often trivial, which means it is often not
separated from application logic.

(2)markup and presentation logic are often quite closely coupled (i.e.
significant changes to the markup often require changes to the
presentation logic), but they are in different places (and even worse, the
presentation logic may even be mixed with the application logic).

The second point above is why I don't understand your claim that the
separation of presentation logic and markup would be easier to maintain.
The only advantage I see is that separating the two makes it easier for
designers to work with any tools they want to use.

Ideally all three, application logic, presentation logic, and markup would
be separate, but I think it's more important to separate application logic
from presentation+markup than markup from application+presentation. Also,
presentation logic and markup often just naturally belong together.

But, my views are also colored by the fact that I've never dealt with
large and complicated templates. In that case it would probably be a good
idea to separate the template from presentation code just to keep the
complexity under control.
DOM-style templating systems aren't 'substitution-only', and can
easily match or surpass macro-style systems for power and flexibility
at a fraction of the size and complexity.

By flexibility I meant flexibility in what you can do by modifying the
template alone (since presentation logic can be added to the template).
In the case of 'DOM-style' systems you may have to edit the template as
well as the presentation logic (which would mean modifying the
'program', not just the template).
 
G

Graham Fawcett

Rene Pijlman said:
Aahz:

The question is: is this irony or sarcasm? :)

I wasn't going to participate in the "my favourite templating engine
is the one for you"-fest, but I can't let incendiary comments about
*my* favourite go unchallenged... ;-)

Aahz is right: if you're comfortable working at the level of servlets,
then Quixote is an outstanding choice.

Although Quixote includes its own templating alternative (PTL, kind of
a templating scheme turned inside-out), it's quite template-agnostic.
Quixote has been used in conjunction with Cheetah, ZPT, stan (well,
something like stan) and others. Or use none of the above, and just
write pure Python to handle your requests.

It's CGI on steroids; servlets without the subclassing; lo, it is the
One True Solution to All Your Web Programming Needs; you'll wonder how
the Web ever got built without it.

this-ad-has-been-approved-by-the-Quixote-campaign-manager'ly yours,

-- Graham
 
?

=?iso-8859-15?Q?Pierre-Fr=E9d=E9ric_Caillaud?=

Ah, simple.

I think Zope is an enormously complex, difficult to learn software which
can apparently do anything, but is so complex to use you can really only
use already-developed modules. Besides Zope is more suited to sites chere
people edit articles. I also don't like it because of ZODB, which is
basically a reinvented, slower filesystem, and also because it forces its
mode of thoughts upon you.

On the other hand, Skunkweb is just a framework, it does what you want
(beautifully), and doesn't get in the way the rest of the time. You should
try it...
 
H

has

Paramjit Oberoi said:
You are right: there are three pieces to be managed (1)application logic,
(2)presentation logic, and (3)markup. MY understanding of HTMLTemplate is
that it enforces the separation of markup and all logic (both presentation
and application), which is certainly very designer friendly.

The problem is that:

(1)presentation logic is often trivial, which means it is often not
separated from application logic.

As with desktop application development, it's up to the developer to
separate/not separate the View's Controller from the Model layer.
Nobody's forcing them to combine the two. I think you're worrying
unnecessarily - folk developing desktop apps seem to manage just fine.

(2)markup and presentation logic are often quite closely coupled (i.e.
significant changes to the markup often require changes to the
presentation logic), but they are in different places

Having the HTML and code in different places isn't in itself a bad
thing. There is coupling between the two, but it doesn't have to be as
tight as it is in macro-style systems.

(and even worse, the
presentation logic may even be mixed with the application logic).

Again, if the programmer wants to write spaghetti then that's their
choice. Not my place to dictate/save them from themselves.

The second point above is why I don't understand your claim that the
separation of presentation logic and markup would be easier to maintain.

I can work on the HTML using all my favourite standard HTML tools, and
the code using all my favourite standard Python tools. I can deal with
each half individually without having the other getting in my way and
spoiling my view. I can hash around the HTML as much as I like with,
at most, minimal breakage - the only stuff in the HTML to break is the
name bindings that tie it to the Controller code, and those are
quickly and easily replaced. The API is very small and simple. How I
structure my code is not dictated by the structure of my template. The
implementation can be as granular as you like, so should be easy to
test and profile right down to the atomic level. I can hand the HTML
half to one developer to take care of and the code half to another.

Ideally all three, application logic, presentation logic, and markup would
be separate, but I think it's more important to separate application logic
from presentation+markup than markup from application+presentation.

DOM-style templating engines provide the three-way separation you
describe.

But, my views are also colored by the fact that I've never dealt with
large and complicated templates. In that case it would probably be a good
idea to separate the template from presentation code just to keep the
complexity under control.

You can find an example of complex use (~100-line template, ~200 LOC)
in the appscript project on my site. See the
appscript.htmldoc.Renderer module. And yes, the clear separation of
code from markup really comes into its own. Mind you, I find it quite
pleasant in simple use too even though the heavy decoupling tends to
make it a little more verbose than equivalent TAL/Cheetah code.
By flexibility I meant flexibility in what you can do by modifying the
template alone (since presentation logic can be added to the template).
In the case of 'DOM-style' systems you may have to edit the template as
well as the presentation logic (which would mean modifying the
'program', not just the template).

You're making a false distinction here. The template and the
presentation logic are as much a part of the "program" as the business
layer they sit on top of. Editing the template markup is modifying the
application. Editing the template logic is modifying the program.
Editing the business logic is modifying the program too. DOM-style
templating systems allow you to work on the first and/or second
without touching the first, just as macro-style systems do.


Now, if you believe that it's the templating system's job to impose
and enforce a certain kind of architecture upon the developer - one
that makes it impossible to mix presentation and business logic
together - then you can certainly make that argument. The popularity
of BDSM languages like Java would indicate there's plenty of folk
around who like working within restrictive tools and environments. I
come from the other school of thought - the one that says that tools
should be small, simple, extensible and as free of built-in
limitations as possible, and assumes the developer is smart enough to
make their own informed decisions as to how they use them. The
popularity of highly dynamic languages from Python and Perl right back
to Lisp suggests there's also plenty of folk who prefer extremely
flexible, malleable tools. But this is a different debate


Anyway, if you want to compare DOM-style templating systems to
something else, try a modern desktop GUI development system such as OS
X's AppKit framework + Interface Builder. They're far closer
conceptually to this type of system than to PHP et-al, which have
their roots in command line macro pre-processors, and it might help
you see where they're coming from a bit better.

HTH

has
 
P

Paramjit Oberoi

The second point above is why I don't understand your claim that the
I can work on the HTML using all my favourite standard HTML tools, and
the code using all my favourite standard Python tools. I can deal with
each half individually without having the other getting in my way and
spoiling my view. I can hash around the HTML as much as I like with,
at most, minimal breakage - the only stuff in the HTML to break is the
name bindings that tie it to the Controller code, and those are
quickly and easily replaced. The API is very small and simple. How I
structure my code is not dictated by the structure of my template. The
implementation can be as granular as you like, so should be easy to
test and profile right down to the atomic level. I can hand the HTML
half to one developer to take care of and the code half to another.

OK, pretty good arguments. I see the point. I guess I would see it more
strongly if my favourite tool for editing both HTML and python wasn't a
standard text editor said:
You're making a false distinction here. The template and the
presentation logic are as much a part of the "program" as the business
layer they sit on top of. Editing the template markup is modifying the
application. Editing the template logic is modifying the program.

I think it is a very real distinction. If you are a user of the
application, you don't want to have to maintain a private set of
modifications to it. Also, the application might be installed in a
read-only location... It might even be compiled, once all the python
compilers really take off.
Now, if you believe that it's the templating system's job to impose
and enforce a certain kind of architecture upon the developer - one
that makes it impossible to mix presentation and business logic
together - then you can certainly make that argument. The popularity

Not at all. In fact, it's the HTMLTemplate-like systms that enforce the
separation of markup from all code. With Cheetah you can have as little
or as much code in the template as you want---no separation of any kind is
enforced. This lack of enforced separation of markup and code is really
convenient.

-param
 
G

Gabriel Cooper

H

has

[snip list of arguments]
OK, pretty good arguments. I see the point. I guess I would see it more
strongly if my favourite tool for editing both HTML and python wasn't a
standard text editor <0.5 wink>.

Only my first argument related to authoring tools; the rest are
general design and management issues.

BTW, my favourite HTML and Python editing tools are also simple text
editors, and I still find the separation of markup and code
advantageous. I like being able to wear my "HTML stylist" hat separate
to my "code geek" hat, especially when it comes to editing and
previewing templates, something DOM-style systems are especially good
at.

I think it is a very real distinction. If you are a user of the
application, you don't want to have to maintain a private set of
modifications to it. Also, the application might be installed in a
read-only location... It might even be compiled, once all the python
compilers really take off.

I see no difference between having an application that loads up a
folderful of Cheetah templates, and having an application that loads
up a folderful of PyMeld/Nevow.renderer/HTMLTemplate templates. You're
seeing barriers where there aren't any; monolithic construction where
there's no need for it. It's completely up to the developer where they
want to put the joins in the system, which bits they want to decouple
from the rest of the system and expose to outsiders, and which they
want to lock away in the application core. Which is just how things
should be, and DOM-style templating systems certainly don't interfere
with it.

(Mind, I've thrown away thousands of lines of working code - including
complete templating engines - precisely because it imposed the sorts
of stupid artificial divisions that made them frustratingly inflexible
and inextensible, so haven't arrived at the DOM-like design by
accident.:)

Not at all. In fact, it's the HTMLTemplate-like systms that enforce the
separation of markup from all code.

There's nothing to stop you inlining chunks of HTML in your Python
code, or adding a preprocessor to the system that extracts chunks of
code out of your HTML template and assembles them into a controller.

With Cheetah you can have as little or as much code in the template as you want

Not exactly true, as it's impossible to have _no_ code in a Cheetah
template. At minimum you'll always have basic flow control and
assignments. DOM-style templating systems start from a much lower
complexity baseline and let you build upward as you want. (See Lisp
philosophy.)

Hope that clarifies.
 
?

=?ISO-8859-1?Q?Thomas_R=F6sner_aka_TRauMa?=

has said:
(Mind, I've thrown away thousands of lines of working code - including
complete templating engines - precisely because it imposed the sorts
of stupid artificial divisions that made them frustratingly inflexible
and inextensible, so haven't arrived at the DOM-like design by
accident.:)

I had some questions and reservations regarding your aproach, but they
vanished while I looked at the code and docs of HTMLTemplate (btw, perhaps
you could link the manual from your hp).

Only one question left: if you use a dom-like aproach to templating, don't
you force a certain tree like structure on the template? As far as I
understood, there is nothing like getElementbyID() to access a node object
regardless of where it is located in the tree. Say, I got a node named
"Menu", which is a div containing some links and a logo (menu.logo), and I
have to access this logo, and later on the designer (likely me wearing my
designer hat and being ignorant about such issues) decides to move the logo
somewhere else... Wouldn't that be a case of presentation forcing changes
upon application logic? Or am I missing something?

Question 2 (while I'm at it): For those of us stuck with PHP-only hosting,
do you know any comparable Template Engine for PHP? Up to now I used smarty,
but I'd like to try the DOM-Aproach.

Regards
Thomas
 
?

=?ISO-8859-1?Q?Thomas_R=F6sner_aka_TRauMa?=

Thomas said:
Question 2 (while I'm at it): For those of us stuck with PHP-only hosting,
do you know any comparable Template Engine for PHP?

Other than OOt, which I just googled myself :).

Greets
Thomas
 
H

has

Thomas Rösner aka TRauMa said:
Only one question left: if you use a dom-like aproach to templating, don't
you force a certain tree like structure on the template?

That's the idea, yes. Object model == tree structure. Same principle
as in GUI toolkits. Even macro-based templates will follow a tree
structure in their logic (though they're not as explicit about it),
unless they're based on unstructured flow control constructs like
GOTOs. Structured programming == tree structures. There is no escape.
:)

As far as I
understood, there is nothing like getElementbyID() to access a node object
regardless of where it is located in the tree.

There isn't, but you could always hack such a feature in if you wanted
to. (How useful it would be in practice I don't know; it doesn't
really make much sense in context of Repeater objects.)
Say, I got a node named
"Menu", which is a div containing some links and a logo (menu.logo), and I
have to access this logo, and later on the designer (likely me wearing my
designer hat and being ignorant about such issues) decides to move the logo
somewhere else...

Depends where you move it. e.g. If you start with the following node
structure:

tem:template
con:title
rep:menu
con:link
con:heading
con:story

and rearrange it to:

tem:template
con:title
con:heading
con:story
rep:menu
con:link

then the hierarchy is still the same, so no change to presentation
logic is required.

It's only with gross alterations like changing the template node
hierarchy, renaming/re-typing nodes, etc. that the presentation logic
must also change; e.g.:

tem:template
con:title
con:mainContent
con:heading
rep:menu
con:link
con:story
Wouldn't that be a case of presentation forcing changes
upon application logic?

Gross changes to presentation markup will require changes to
presentation logic. Same would be true for a templating system like
Cheetah or PSP that embeds presentation logic in markup. Same would be
true for a Tkinter- or wxPython-based GUI. And so on. None of these
scenarios should ever require changes to business logic if designed
for proper separation between application layers.

Also remember there's no reason your presentation logic should be
physically embedded in the application core where they're hard or
impossible to access. Presentation code could be kept in standalone
files that sit in the same directory as the HTML templates they belong
to. Or if you feel that having separate files for presentation markup
and presentation logic is a bit messy, you could put both into a
single file where [e.g.] the presentation logic follows the markup:

---- file '/foo/bar/MyTemplate.tpl' ----

<html>
blah-blah-blah
</html>

on render_template(node,...):
blah-blah-blah

----- end file -----

Just use the following to read this file in and separate and compile
its constituent parts:

templatePath = '/foo/bar/MyTemplate.tpl'

f = open(templatePath)
html, controllerSrc = f.read().split('</html>')
f.close()
controller = compile(controllerSrc, templatePath, 'exec')
template = HTMLTemplate.Template(controller, html)


Or write your own HTML preprocessor based on HTMLParser and you could
even embed individual Python functions within separate processing
instructions that you can strew throughout your HTML, then have the
preprocessor separate them out and assemble the pieces into HTML
document and controller module. There's lots of ways you could hack
it. If you come up with something cool, send me a URL and I'll be
pleased to link to it.

do you know any comparable Template Engine for PHP?

I don't, but even if there aren't any HTMLTemplate is small and simple
enough that it should be reasonably straightforward to port.
(Portability and hackability were two of the issues guiding its
design; the Python version is itself a port. I'll assume PHP will have
a suitable SAX-based HTML parser you can bolt it onto.) The only two
areas you might have trouble with are:

1. the syntactic sugar in the API, but that can easily be done without
(see the vanilla API design below)

2. the object cloning, which might not be feasible on other languages,
but that code could be rewritten to use regular object instantiation
instead of copying.



------- HTMLTemplate Vanilla API Reference Design -------

Node -- Abstract base class
Methods:
node(name) -- get a node
setNode(name, node) -- replace a node


Container (extends Node) -- A mutable HTML element ('con')
Methods:
att(name) -- get a tag attribute's value
setAtt(name, value) -- set a tag attribute's value
delAtt(name) -- delete a tag attribute

content() -- get the HTML element's content with &<>" characters
automatically escaped
raw() -- get the HTML element's raw content (i.e. no automatic
character escaping)
setContent(value) -- get the HTML element's content with &<>"
characters automatically escaped
setRaw(value) -- set the HTML element's raw content (i.e. no
automatic character escaping)

omit() -- don't render this node
omitTags() -- render this node's content only


Repeater (extends Container) -- A mutable, repeatable HTML element
('rep')
Methods:
repeat(callback, sequence, *args) -- render an instance of this node
for each item in sequence


Template (extends Node) -- The top-level template node
Constructor:
Template(callback, html, attribute='node', encoder=defaultEncoder,
decoder=defaultDecoder)
Methods:
render(*args) -- render this template
structure() -- print the object model's structure for diagnostic use
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,128
Latest member
ElwoodPhil
Top