DTML peek
[email protected] (Teodor Cimpoesu) Thu, 24 Feb 2000 19:06:10 +0200
| Newsgroups | php.template |
|---|---|
| Organization | DIGICOM S.A. Romania |
| Message-ID | <[email protected]> |
As promised, I took the red-wire through Zope DTML
and came with sort of synthesis which I hope to inspire
some (new) ideas:
General:
I'm not sure I understood what's the exact semantic of the
PTE (PHP Template Engine). Is it intended to be used in the
PHP manner
{{<-delim __meta-keyword__ delim->}}
or
{{TAG}}[content {{/TAG}} ]?
I would prefer `a template markup language' to a ``s/$foo/'bar'/''
Following are some excerpts from Zope's DTML documentation
In my dreams, things would happen similarily, but...
syntax:
<dtml-tag attr1="value1" attr2="value2" ...>
sigleton: <dtml-tag ...>
duplex : <dtml-tag ...> .* </dtml-tag>
attributes:
name - if not present then the first ecountered atom is the name
<dtml-var name="foo"> eq <dtml-var foo>
The name can also de a function name, or a template name (in
which
case it is first resolved then fetched in place)
Also if it is a function, the result is cached for that scope
<dtml-if expensiveCall>
<dtml-var expensiveCall> <!--fetched from cache-->
</dtml-if> <!--end of cache scope-->
expr - the expression is evaluated and the result returned
It's enclosed in double quotation marks
it can make use of a special variable `_', the global namespace
fmt - a format for the containing data (e.g. useful for date style)
can be a custom one (a function)
e.g. <dtml-var date fmt=DayOfWeek> calls DayOfWeek with the
value of date
supports C style descriptors
e.g <dtml-var cost fmt-"$%.2d" null='n/a'>
null - just as `default' keyword (value when variable is null)
size - maximum size
etc - when truncating, what string to be appended (default is `...')
e.g <dtml-var spam size=10 etc="...">
Other attributes with no arguments:
lower, upper,capitalize - obvious
spacify - something like s/_/ /
thousand_commas - when the value is a number insert commas at every
three digits
html_qoute , url_quote - special chars 2 entities
newline_to_br - do nl2br() on the value
So, I would have liked something like <php-var> in PTE
Other cool stuff :
REQUEST, RESPONSE objects (also present in ASP, servlets)
This is OT maybe, or maybe it should go in PEAR implemented on
a higher layer, I dunno
Example:
default document source (modified from the docs)
<dtml-var standard_html_header>
<h2><dtml-var document_title></h2>
this is <dtml-var document_id> document. <br>
<dtml-var standard_html_footer>
The standard_* stuff are DTML Methods describe at top level and aquired
by
lower documents. This can eventually be customized in a config file or
imported as separate templates.
A question here: I guess `include' just gets the template w/o
`resolving' it
Can we also have `import' to get in a template already parsed?
Conditional tags :
<dtml-if>, <dtml-unless> - obvious
Iterative insertion: <dtml-in>
e.g.
<table>
<tr>
<th>Name</th>
<th>Phone</th>
</th>
<dtml-in employees sort=name>
<tr>
<td><dtml-var name></td>
<td><dtml-var phone></td>
</tr>
</dtml-in>
</table>
To avoid an empty table:
<dtml-if employees>
(same as above)
<dtml-else>
No Employees
</dtml-if>
(tip: but dtml-in has also an dtml-else, in case the set is empty!)
special variables are defined within dtml-in scope:
sequence-start, sequence-end - (boolean) first/last element in set
sequence-item, sequence-key - current item, the key associated w/ the
item
sequence-index - the index of the current item, 0 based
dtml-in attributes (some of):
sort - sort the sequence of objects (by attribs values)
mapping - (no args) sais the items are mapping objects
batch processing related (also variables for batch processing)
dtml-with tag : for easy access to objects (just like in JavaScript,
AFAIK)
dtml-let tag : (multiple) assignment
dtml-call tag : function calls
e.g. <dtml-call "addDocument('foo.html',namespace(skin='puffy'))">
Exception handling with {dtml-try,dtml-except,dtml-raise}
e.g.
<dtml-try>
cost per unit: $<dtml-var expr="_.float(total/units)">
<dtml-except ZeroDivisionError>
cost per unit: N/A
</dtml-try>
(exceptions are tabu in PHP, though one still can implement an
`emulator')
Commenting tags: <dtml-comment> ignored </dtml-comment>
The super-dooper dtml-tree:
simplest: <dtml-tree> <dtml-var id> </dtml-tree>
By default, displays branches and sub-branches of an object, found by
calling tpValues on the object being displayed (the function can be
customized)
attribs(some):
branches - name of the method used to find subobjects
branch_expr - expression evaluated to find subobjects
leaves - document used to expand subobjects that do not have
sub-branches
sort - sort the branches
Also some variables are set within dtml-tree scope:
tree-item-expanded, tree-item-url, tree-root-url, tree-level,
tree-colspan
another cool tag : dtml-sendmail
e.g.
<dtml-sendmail smtphost="my.host.com">
To: PHP-TPL <<dtml-var phptpl>>
From: me <<dtml-var me>>
Subject: <dtml-var subject>
<dtml-var body>
</dtml-var sendmail>
attribs:
mailhost - an alternate MailHost object (defined by Zope)
smtphost - outgoing server
port - default 25
mailto - (list of) recipient(s)
mailfrom - sender
subject - obvious
For attachments, there is dtml-mime
attribs:
type - MIME type
disposition - set disposition header
encode - default base64, none if set to 7bit
e.g.
<dtml-sendmail
mailfrom="[email protected]" mailto="[email protected]"
subject="missing `u' at line 22" smtphost="mightyqmail.com"
port="8025">
<dtml-mime type=text/plain encode=7bit>
Hey, you fotgot your lipstick at home, I attached it
in case you need it later today :-P
<dtml-boundary
type=application/x-lipstick
disposition=attachment encode=base64>
<dtml-var binary_lipstick>
</dtml-mime>
</dtml-sendmail>
As far as I can see, most of the ppl involed in this discussion
are biased toward the braces notation, which I don't really like, so
my rates are low, anyway hope you might get some ideas from DTML.
I've been peeking into the python source, to see how it is actually
doing the stuff, but I dunno what can be of interest from them,and also
that nobody would agree with my joke of posting python code :-)
bye.
--
CIMPOESU Teodor, Web Programmer
@ DIGICOM S.A. Bucharest, Romania
@ Internet, site development
@ see http://internet.digiro.net/