[ZPT] TAL omitAllTags proposal
Godefroid Chapelle
gotcha@swing.be
Fri, 22 Mar 2002 12:08:08 +0100
We are using PageTemplates outside Zope to generate SQL data definitions.
You find hereunder the type of code we wrote first in order to get
well-formatted (human readable) SQL code.
CREATE TABLE <tal:block tal:replace="table/name"/> (
<tal:block tal:repeat="field table/field"> <tal:block
tal:replace="field/name"/> <tal:block tal:replace="field/type"/><tal:block
tal:condition="python:hasattr(field,'length')">(<tal:block
tal:replace="field/length"/>)</tal:block><tal:block
tal:condition="python:hasattr(field,'notnull')"> NOT
NULL</tal:block><tal:block
tal:condition="python:hasattr(field,'foreignkey')"> REFERENCES <tal:block
tal:replace="field/foreignkey/table/name"/>(<tal:block
tal:replace="field/foreignkey/field/name"/>)</tal:block>,</tal:block>
PRIMARY KEY (<tal:block tal:repeat="primarykey
table/primarykeys"><tal:block tal:replace="primarykey/name"/><tal:block
tal:condition="not:repeat/primarykey/end">, </tal:block></tal:block>)
);
This type of code is quite difficult to debug because of the multiplicity
of 'tal:block' elements.
So we rewrote it like this :
CREATE TABLE <tablename tal:omit-tag="" tal:replace="table/name"/> (
<fields tal:omit-tag="" tal:repeat="field table/field"> <fieldname
tal:omit-tag="" tal:replace="field/name"/> <fieldtype tal:omit-tag=""
tal:replace="field/type"/><typelength tal:omit-tag=""
tal:condition="python:hasattr(field,'length')">(<tal:block
tal:replace="field/length"/>)</typelength><notnull tal:omit-tag=""
tal:condition="python:hasattr(field,'notnull')"> NOT
NULL</notnull><foreignkey tal:omit-tag=""
tal:condition="python:hasattr(field,'foreignkey')"> REFERENCES <tablename
tal:omit-tag="" tal:replace="field/foreignkey/table/name"/>(<fieldname
tal:omit-tag=""
tal:replace="field/foreignkey/field/name"/>)</foreignkey>,</fields>
PRIMARY KEY (<primarykeys tal:omit-tag="" tal:repeat="primarykey
table/primarykeys"><tal:block tal:replace="primarykey/name"/><comma
tal:omit-tag="" tal:condition="not:repeat/primarykey/end">,
</comma></primarykeys>)
);
where we get easier code to debug but with a lot of 'tal:omit-tag'.
As I am generating SQL, I know I do not want any xml-like tags. I would
like to state it and avoid repetitions of 'tal:omit-tag'.
I would be able to use the following code:
CREATE TABLE <tablename tal:replace="table/name"/> (
<fields tal:repeat="field table/field"> <fieldname
tal:replace="field/name"/> <fieldtype tal:replace="field/type"/><typelength
tal:condition="python:hasattr(field,'length')">(<tal:block
tal:replace="field/length"/>)</typelength><notnull
tal:condition="python:hasattr(field,'notnull')"> NOT
NULL</notnull><foreignkey
tal:condition="python:hasattr(field,'foreignkey')"> REFERENCES <tablename
tal:replace="field/foreignkey/table/name"/>(<fieldname
tal:replace="field/foreignkey/field/name"/>)</foreignkey>,</fields>
PRIMARY KEY (<primarykeys tal:repeat="primarykey
table/primarykeys"><tal:block tal:replace="primarykey/name"/><comma
tal:condition="not:repeat/primarykey/end">, </comma></primarykeys>)
);
I went through the python source code. It was really easy to add the feature.
I came to the following patches:
TAL/HTMLTALParser.py ( 1.30.4.3.4.1 )
102c
< def __init__(self, gen=None):
---
> def __init__(self, gen=None, omitAllTags=0):
109a
> self.omitAllTags = omitAllTags
288a288,289
> if self.omitAllTags:
> taldict['omit-tag'] = ''
PageTemplates/PageTemplate.py ( 1.20.10.2 )
49c49,50
< def pt_edit(self, text, content_type):
---
> def pt_edit(self, text, content_type, omitAllTags=0):
> self.omitAllTags = omitAllTags
145c
< parser = HTMLTALParser(gen)
---
> parser = HTMLTALParser(gen, self.omitAllTags)
As both patches are light and nonintrusive, I propose to introduce them in
the core.
--
Godefroid Chapelle
BubbleNet sprl
rue Victor Horta, 18 / 202
1348 Louvain-la-Neuve
Belgium
Tel + 32 (10) 459901
Mob + 32 (477) 363942
TVA 467 093 008
RC Niv 49849