Another templating solution

1 view
Skip to first unread message

Armando M. Baratti

unread,
Jan 2, 2005, 6:02:35 PM1/2/05
to cherryp...@googlegroups.com
Hello Remi,

I'm new to CherryPy (using it for only 2 days) and have found it to be very promising.
The only thing I didn't like much in it is the templating options (CherryTemplate or Cheetah), because both mix Python and html togheter.
I'd prefer something that could permit me to do the gross of the page in an wysiwyg HTML editor (it's a matter of productivity) and then add the Python funcionality.

So I've made a small script that can parse a regular HTML file and replace a special tag for the data from a dictionary passed to it.
The tag has the format:
<script language="amb"> Var </script>

"amb" are just my initials, the script permits you to change that to whatever you like (ok, something like "javascript" would not work fine ;-).
The <script> tag is very common, so I think it would not be a problem to use any comon HTML editor (I've used OpenOffice's), that it will permit to choose the "language" attribute.

So the only thing you have to do is:
- edit an HTML template on an wysiwyg HTML editor, placing <script language="amb"> Var </script> evreywhere you want
e.g:  This is the <b>current date: <script language="amb"> Date </script></b><br>
          and the <b>current time: <script language="amb"> Time </script></b>

- inside the CherryPy script call PrepPage passing a dictionary with the data:
e.g:
import time
from cherrypy import cpg
from PrepPage import PrepPage
class MyPage:
    def index(self):
        Today = time.localtime()
        PageDic = {'Date': '[%02d/%02d/%04d]' % (Today[2], Today[1], Today[0]),
                             'Time': '[%02d:%02d:%02d]' % (Today[3],Today[4], Today[5]}
        return PrepPage('model.html', PageDic)
    index.exposed = True

This way you get a good separation of template and Python code.
You could even use HTMLgen to generate rather complex HTML data as objects,  to be passed through the dictionary.
e.g.
from HTMLgen import *
...
        Options = List(['First option', 'Second option'])          # This will generate a bullet list (it could be an entire table)
        for NewOption in SomeList:
                Options.append(NewOption)
        PageDic = {'Date': '[%02d/%02d/%04d]' % (Today[2], Today[1], Today[0]),
                             'Time': '[%02d:%02d:%02d]' % (Today[3],Today[4], Today[5],
                            'OptionList': str(Options)}

I don't know if you (or someone) think it would be useful, at least it was for me.
I'll probably have no more time to work on it (I'm very, very busy), so I'm giving it for the CherryPy comunity in the hope it helps someone else.

Armando

PrepPage.py

Remco Boerma

unread,
Jan 3, 2005, 3:39:39 AM1/3/05
to cherryp...@googlegroups.com
Hi Armando, folks,


Happy new year to everyone!!



Armando M. Baratti wrote:
The only thing I didn't like much in it is the templating options (CherryTemplate or Cheetah), because both mix Python and html togheter.
I'd prefer something that could permit me to do the gross of the page in an wysiwyg HTML editor (it's a matter of productivity) and then add the Python funcionality.

So I've made a small script that can parse a regular HTML file and replace a special tag for the data from a dictionary passed to it.
The tag has the format:
<script language="amb"> Var </script>
That's the reason why we have the http://trac.cherrypy.org/cgi-bin/trac.cgi/wiki/XyaptuFilter . you could then write:
<p>
    <b>$boldText</b>
</p>
And have another few structures, without being able to code python in your html pages.
Checkout http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/162292  if you want to see  more of the templating language.

Reply all
Reply to author
Forward
0 new messages