How to dynamically include templates?

3 views
Skip to first unread message

jerryji

unread,
Oct 4, 2008, 10:21:43 AM10/4/08
to Genshi
Hi,

How can I dynamically include non-file templates?

This is my master template --

"""
<?xml version="1.0" encoding="utf-8"?>
<html xmlns="..." xmlns:py="..." xmlns:xi="...">
<head />
<body>
<py:for each="widget_template in widget_templates">
<xi:include href="[How to include widget_template here?]" />
</py:for>
</body>
</html>
"""

where widget_template is a _string_ retrieved from a database --

"""
<?xml version="1.0" encoding="utf-8"?>
<div xmlns="..." xmlns:py="..." py:strip="True">
<div>widget_info</div>
</div>
"""

Many thanks in advance!

Jerry

kindy

unread,
Oct 8, 2008, 9:56:57 PM10/8/08
to Genshi
use this as master template.

!!!!!
i have not test this code, just be careful.
!!!!!

"""
<?xml version="1.0" encoding="utf-8"?>
<?python
from genshi.template import MarkupTemplate
the_ctxt = locals().get('__data__', None)
?>
<html xmlns="..." xmlns:py="..." xmlns:xi="...">
<head />
<body>
<py:for each="widget_template in widget_templates">
${MarkupTemplate(widget_template).generate(the_ctxt)}
</py:for>
</body>
</html>

kindy

unread,
Oct 8, 2008, 11:06:41 PM10/8/08
to Genshi
sorry, but i miss something in the code.
change
from genshi.template import MarkupTemplate
to
from genshi.template.markup import MarkupTemplate

jerry

unread,
Oct 10, 2008, 9:59:01 PM10/10/08
to Genshi
Thanks kindy, it works like a charm :)

BTW, does anyone think it worth being documented on the Genshi site?

Jerry

kindy

unread,
Oct 10, 2008, 10:46:38 PM10/10/08
to Genshi
In genshi 0.5.1, it seems like that the .generate() is not necessary,
but required in genshi 0.4.x.
Reply all
Reply to author
Forward
0 new messages