On Mar 4, 2008, at 9:18 AM, Ian Bicking wrote:

I would prefer a pattern like:

# in, maybe, helpers.py or something...
from something import MakoRenderer
render = MakoRenderer(
  base_path=os.path.join(os.path.dirname(__file__), 'templates'),
  ... whatever other options you might set ...)

What is MakoRenderer? The goal is to have as little beyond what the template language needs to render something. Pylons already has the config for the templates handy in the configuration object, though of course perhaps that can be simplified a little.

Then you have a clear place to do app-wide configuration (stuff like
whether you are using autoquoting). MakoRenderer would have a __call__
method with the appropriate signature.  The lines of code wouldn't
increase significantly, as it would reduce the size of wsgiapp by about
the same amount.

As Mike was saying, if the load_environment has a line that loads the Mako TemplateLoader, we'd have a load_environment.py like this:

from mako.lookup import TemplateLookup

def load_environment(global_conf, app_conf):
    """Configure the Pylons environment via the ``pylons.config``
    object
    """
    # Pylons paths
    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    paths = dict(root=root,
                 controllers=os.path.join(root, 'controllers'),
                 static_files=os.path.join(root, 'public'),
                 templates=[os.path.join(root, 'templates')])

    # Initialize config with the basic options
    config.init_app(global_conf, app_conf, package='solon',
                    template_engine='mako', paths=paths)

    config['routes.map'] = make_map()
    config['pylons.g'] = app_globals.Globals()
    config['pylons.h'] = solon.lib.helpers

    # Setup SQLAlchemy database engine
    engine = engine_from_config(config, 'sqlalchemy.')
    init_model(engine)

# Setup the Mako template loader (or whatever template engine you want) config['pylons.g'].mako_lookup = TemplateLookup(directories=paths['templates'])

# CONFIGURATION OPTIONS HERE (note: all config options will override
    # any Pylons config options)


Then the render function would look like this:

def render_mako(template_name, cache_key=None, cache_type=None,
                                cache_expire=None):
    """Render a template with Mako

    Accepts the cache options ``cache_key``, ``cache_type``, and
    ``cache_expire`` in addition to other keyword arguments that should
    be passed into Mako's ``Template.render`` function.

    """
    # First, get the globals
    globs = pylons_globals()

    # Try and grab a template reference
    template = globs['g'].mako_lookup.get_template(template_name)

    # Update the template variables with the Pylons globals
    kwargs.update(globs)

    def render_template():
        return template.render(**globs)

    return cached_template(template_name, render_template, cache_key,
cache_type, cache_expire, ns_options=('fragment',))


If you wanted to change options to the Mako templateloader, it'd be really obvious how to do that... or change it to a different engine's template loader as appropriate. What purpose does the MakoRenderer object have?

Mike, I believe I've updated this as you suggested, such that Mako under Pylons expects your variables to be attached to 'c' which are then available under 'c' in Mako. Clearly users can tweak the render function as desired should they wish to use it in different ways.

Cheers,
Ben

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to