Edgewall Software

Changes between Initial Version and Version 1 of GenshiRecipes/PylonsWithGenshi


Ignore:
Timestamp:
Oct 20, 2006, 2:33:28 PM (18 years ago)
Author:
s0undt3ch
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • GenshiRecipes/PylonsWithGenshi

    v1 v1  
     1= [GenshiRecipes Genshi Recipes]: Using Genshi with Pylons =
     2
     3To use Genshi with [http://pylonshq.com/ Pylons] you need to make some aditional steps, which is what's going to be explained here.
     4
     5== Adding Genshi Template Engine ==
     6First you need some changes in your application's '''`middleware.py`'''.
     7
     8=== Genshi only ===
     9{{{
     10#!python
     11    # Load our Pylons configuration defaults
     12    config = load_environment()
     13    config.init_app(global_conf, app_conf, package='<appname>')
     14
     15    # Setup Genshi(only) Template Engine
     16    config.template_engines = []
     17    config.add_template_engine('genshi', '<appname>.templates', {})
     18}}}
     19
     20=== Genshi and Myghty ===
     21
     22{{{
     23#!python
     24    # Load our Pylons configuration defaults
     25    config = load_environment()
     26    config.init_app(global_conf, app_conf, package='<appname>')
     27
     28    # Setup Genshi Template Engine
     29   myghty = config.template_engines.pop()
     30   config.add_template_engine('genshi', '<appname>r.templates', {})
     31   config.template_engines.append(myghty)
     32}}}
     33
     34== !WebHelpers and Genshi ==
     35To 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.
     36
     37=== The Hard Way ===
     38You can do for example(I won't explain the template, for that consult the [http://genshi.edgewall.org/wiki/Documentation/index.html Genshi Guide]):
     39{{{
     40#!text/html
     41<!DOCTYPE html
     42    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     43    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
     44<html xmlns="http://www.w3.org/1999/xhtml"
     45      xmlns:py="http://genshi.edgewall.org/"
     46      xmlns:xi="http://www.w3.org/2001/XInclude"
     47      py:strip="">
     48<head py:match="head">
     49  <title>The Title</title>
     50  <body>
     51     <h1>hello</h1>
     52     ${XML(h.<helper>)}
     53  </body>
     54</html>
     55}}}
     56...but the above becomes quite painfull when using several !WebHelpers.
     57
     58=== The Easy Way ===
     59The solution is to wrap all helper functions into Markup objects. For that, edit '''`<appname>/lib/helpers.py`''' and add:
     60{{{
     61#!python
     62from genshi.core import Markup
     63
     64def wrap_helpers(localdict):
     65    def helper_wrapper(func):
     66        def wrapped_helper(*args, **kw):
     67            return Markup(func(*args, **kw))
     68        wrapped_helper.__name__ = func.__name__
     69        return wrapped_helper
     70    for name, func in localdict.iteritems():
     71        if not callable(func) or not func.__module__.startswith('webhelpers.rails'):
     72            continue
     73        localdict[name] = helper_wrapper(func)
     74wrap_helpers(locals())
     75}}}
     76Now edit '''`<appname>/config/middleware.py`''' and just add:
     77{{{
     78#!python
     79from <appname>.lib import helpers
     80}}}
     81The above will cause all helpers to be wrapped uppon application initialization.
     82
     83Now you can use the WebHelpers like you used to with Myghty, for example:
     84
     85{{{
     86#!text/html
     87<!DOCTYPE html
     88    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     89    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
     90<html xmlns="http://www.w3.org/1999/xhtml"
     91      xmlns:py="http://genshi.edgewall.org/"
     92      xmlns:xi="http://www.w3.org/2001/XInclude"
     93      py:strip="">
     94<head py:match="head">
     95  <title>The Title</title>
     96  <body>
     97     <h1>hello</h1>
     98     ${h.<helper>}
     99  </body>
     100</html>
     101}}}
     102
     103== Final Thoughts ==
     104
     105You might also think about adding to '''`<appname>/lib/helpers.py`''':
     106{{{
     107#!python
     108from genshi.builder import tag
     109}}}
     110The 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.
     111
     112And that's all....