Re: [Webware-discuss] template system in WebWare?

Chuck Esterbrook wrote:
> Geoff Talvola wrote:
> > How about a class derived from Page that does variable substitution into
> > an external HTML file with simple markup? For example, a class might
> > contain:
> >
> > [munch]
> I think it could be a good idea and would have potential applications for simple cases. I just don't see it as a replacement for PSP.
>
> At the very least, I would extend your approach so that if I type {foo()}, then that method is invoked on self. I've used something like that before in a class-based CGI system that I built.
>
> Of course, the fact that we use a class hierarchy and servlet factories means you can always take an approach like this for your project. You could even customize the approach if you had a special kind of application.
>
> Given that many pages end up looping over a set of data at some point, do you have a proposal for that, or would you go with Ben's approach, or DTML, ...?
I'm not sure, but if there is any one approach that works better with HTML editors than another, that would be a good reason to choose it. I don't know enough about HTML editors to make that judgement.
But, as I stated in the message I just sent, if PSP could be used as a simple presentation language for a WebKit class, we could avoid introducing a new mini-language, and as you say, you can always hack in complex code
right into the PSP page when needed.
For example:
class MyPSPPage(PSPPage):
def __init__(self):
self.title = 'This is the title'
self.records = [ {'firstname':'Al', 'lastname':'Gore'},
{'firstname':'Bill', 'lastname':'Clinton'}
]
The PSP file could be:
<B><%=self.title%></B>
<HR>
<TABLE>
<%for r in self.records:%>
<TR><TD><%=r.firstname%></TD><TD><%=r.lastname%></TD><TR>
<%end%>
</TABLE>
Some magic in the PSPPage base class allows the PSP page to access the object's attributes through self.
--
- Geoff Talvola
Parlance Corporation
gtalvola@...

Thread view

This message shamelessly steals ideas from a recent message by Benjamin
Saller Bender on the python-web-modules mailing list, but applies the
ideas to WebWare.
Benjamin Saller Bender makes some very good points about why you might
want to separate the code driving the logic of a web page from the HTML
presentation of the web page. I won't repeat those arguments here, but
I will propose a way to accomplish it in WebKit:
How about a class derived from Page that does variable substitution into
an external HTML file with simple markup? For example, a class might
contain:
def __init__(self):
self.title = 'This is the title'
self.records = [ {'firstname':'Al', 'lastname':'Gore'},
{'firstname':'Bill', 'lastname':'Clinton'}
]
and the HTML file might look like:
<B>{title}</B>
<HR>
<TABLE>
{BEGIN records}
<TR><TD>{firstname}</TD><TD>{lastname}</TD><TR>
{END}
</TABLE>
Basically, the Python class assigns values into self, to be rendered
into the HTML file which looks as much like straight HTML as possible.
- Geoff Talvola
Parlance Corporation
gtalvola@...

Geoff Talvola wrote:
>
> This message shamelessly steals ideas from a recent message by Benjamin
> Saller Bender on the python-web-modules mailing list, but applies the
> ideas to WebWare.
>
> Benjamin Saller Bender makes some very good points about why you might
> want to separate the code driving the logic of a web page from the HTML
> presentation of the web page. I won't repeat those arguments here, but
> I will propose a way to accomplish it in WebKit:
>
> How about a class derived from Page that does variable substitution into
> an external HTML file with simple markup? For example, a class might
> contain:
>
> def __init__(self):
> self.title = 'This is the title'
> self.records = [ {'firstname':'Al', 'lastname':'Gore'},
> {'firstname':'Bill', 'lastname':'Clinton'}
> ]
>
> and the HTML file might look like:
>
> <B>{title}</B>
> <HR>
> <TABLE>
> {BEGIN records}
> <TR><TD>{firstname}</TD><TD>{lastname}</TD><TR>
> {END}
> </TABLE>
>
> Basically, the Python class assigns values into self, to be rendered
> into the HTML file which looks as much like straight HTML as possible.
>
> - Geoff Talvola
> Parlance Corporation
> gtalvola@...
I think it could be a good idea and would have potential applications for simple cases. I just don't see it as a replacement for PSP.
At the very least, I would extend your approach so that if I type {foo()}, then that method is invoked on self. I've used something like that before in a class-based CGI system that I built.
Of course, the fact that we use a class hierarchy and servlet factories means you can always take an approach like this for your project. You could even customize the approach if you had a special kind of application.
Given that many pages end up looping over a set of data at some point, do you have a proposal for that, or would you go with Ben's approach, or DTML, ...?
-Chuck

Chuck Esterbrook wrote:
> Geoff Talvola wrote:
> > How about a class derived from Page that does variable substitution into
> > an external HTML file with simple markup? For example, a class might
> > contain:
> >
> > [munch]
> I think it could be a good idea and would have potential applications for simple cases. I just don't see it as a replacement for PSP.
>
> At the very least, I would extend your approach so that if I type {foo()}, then that method is invoked on self. I've used something like that before in a class-based CGI system that I built.
>
> Of course, the fact that we use a class hierarchy and servlet factories means you can always take an approach like this for your project. You could even customize the approach if you had a special kind of application.
>
> Given that many pages end up looping over a set of data at some point, do you have a proposal for that, or would you go with Ben's approach, or DTML, ...?
I'm not sure, but if there is any one approach that works better with HTML editors than another, that would be a good reason to choose it. I don't know enough about HTML editors to make that judgement.
But, as I stated in the message I just sent, if PSP could be used as a simple presentation language for a WebKit class, we could avoid introducing a new mini-language, and as you say, you can always hack in complex code
right into the PSP page when needed.
For example:
class MyPSPPage(PSPPage):
def __init__(self):
self.title = 'This is the title'
self.records = [ {'firstname':'Al', 'lastname':'Gore'},
{'firstname':'Bill', 'lastname':'Clinton'}
]
The PSP file could be:
<B><%=self.title%></B>
<HR>
<TABLE>
<%for r in self.records:%>
<TR><TD><%=r.firstname%></TD><TD><%=r.lastname%></TD><TR>
<%end%>
</TABLE>
Some magic in the PSPPage base class allows the PSP page to access the object's attributes through self.
--
- Geoff Talvola
Parlance Corporation
gtalvola@...

Geoff Talvola wrote:
> Some magic in the PSPPage base class allows the PSP page to access the object's attributes through self.
That magic exists right now due to the ancestor class KeyValueAccess.
You can say:
valueForName("firstName")
valueForName("user.phoneNumber")
valueForName("request.fields.comments")
valuesForNames(['x', 'y', 'z']) # returns a list of values
Legal values are any public or private methods or attributes. You can also traverse into dictionaries.
-Chuck