= [GenshiRecipes Genshi Recipes]: Using Genshi with Pylons = In Pylons 0.9.6rc1, you need to replace `template_engine='mako'` with `template_engine='genshi'` in `config/environment.py`. Apparently as of Pylons 0.9.5, if you have Genshi installed you may invoke Genshi templates just by using `render_response("genshi","")`. The instructions below are helpful for making genshi the "default" templates. To use Genshi with [http://pylonshq.com/ Pylons] 0.9.4 and below, you need to take some additional steps, which is what's going to be explained here. See also the [http://docs.pythonweb.org/display/pylonscookbook/Genshi+templates Pylons Cookbook variant] == Adding Genshi Template Engine == First you need some changes in your application's '''`middleware.py`'''. === Genshi only === {{{ #!python # Load our Pylons configuration defaults config = load_environment() config.init_app(global_conf, app_conf, package='') # Setup Genshi(only) Template Engine config.template_engines = [] config.add_template_engine('genshi', '.templates', {}) }}} === Genshi and Myghty === {{{ #!python # Load our Pylons configuration defaults config = load_environment() config.init_app(global_conf, app_conf, package='') # Setup Genshi Template Engine myghty = config.template_engines.pop() config.add_template_engine('genshi', '.templates', {}) config.template_engines.append(myghty) }}} Finally, the Pylons template plug-ins currently expect paths to act as module imports, so you will also need to create an empty `__init__.py` file inside appname/templates. == !WebHelpers and Genshi == To use [http://pylonshq.com/WebHelpers/ WebHelpers] within a Genshi template, the helper return values need to be wrapped in a Markup object, as it would otherwise be escaped. Think carefully before following this suggestion, however, as this effectively works around the white-listing effect of the Markup object, automatically running any HTML that users may submit to your site. It is safer (albiet much more annoying) to manually wrap specific WebHelpers in your pages. === The Hard Way === You can do for example(I won't explain the template, for that consult the [http://genshi.edgewall.org/wiki/Documentation/index.html Genshi Guide]): {{{ #!text/html The Title

hello

${XML(h.)} }}} ...but the above becomes quite painfull when using several !WebHelpers. === The Easy Way === The solution is to wrap all helper functions into Markup objects. For that, edit '''`/lib/helpers.py`''' and add: {{{ #!python from genshi.core import Markup def wrap_helpers(localdict): def helper_wrapper(func): def wrapped_helper(*args, **kw): if not callable(func(*args, **kw)): return Markup(func(*args, **kw)) else: return Markup(func(*args, **kw)()) wrapped_helper.__name__ = func.__name__ return wrapped_helper for name, func in localdict.iteritems(): if not callable(func) or not func.__module__.startswith('webhelpers.rails'): continue localdict[name] = helper_wrapper(func) wrap_helpers(locals()) }}} ---- '''Note''': as of !WebHelpers 1.0 the `webhelpers.rails` package [http://webhelpers.groovie.org/whats_new.html#deleted-packages has been removed] and it seems the `wrap_helpers` workaround above isn't required anymore(?) ---- Now edit '''`/config/middleware.py`''' and just add: {{{ #!python from .lib import helpers }}} The above will cause all helpers to be wrapped uppon application initialization. Now you can use the [http://pylonshq.com/WebHelpers/ WebHelpers] like you used to with [http://www.myghty.org/ Myghty], for example: {{{ #!text/html The Title

hello

${h.} }}} == Final Thoughts == You might also think about adding to '''`/lib/helpers.py`''': {{{ #!python from genshi.builder import tag }}} The Genshi tag object is just like the '''`content_tag()`''' !WebHelper, but it will be faster since it does not have to pass through the wrapper. And that's all....