= [GenshiRecipes Genshi Recipes]: Using Genshi with Pylons = To use Genshi with [http://pylonshq.com/ Pylons] you need to make some aditional steps, which is what's going to be explained here. == 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) }}} == !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. === 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): 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()) }}} 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 trough the wrapper. And that's all....