Commit 90772890 authored by Julien Muchembled's avatar Julien Muchembled

jinja2: add support for inline templates

parent 8b177917
2.4.3-dev (unreleased)
2.4.3 (2013-08-02)
======================
* No changes yet.
* jinja2: add support for inline templates.
2.4.2 (2012-08-21)
==================
......
from setuptools import setup, find_packages
import os
version = '2.4.3-dev'
version = '2.4.3'
name = 'slapos.recipe.template'
long_description = open("README.txt").read() + "\n" + \
open(os.path.join('slapos', 'recipe',
......
......@@ -65,6 +65,9 @@ Mandatory:
``template``
Template url/path, as accepted by zc.buildout.download.Download.__call__ .
For very short template, it can make sense to put it directly into
buildout.cfg: the value is the template itself, prefixed by the string
"inline:" + an optional newline.
``rendered``
Where rendered template should be stored.
......@@ -390,14 +393,13 @@ Section dependency
You can use other part of buildout in the template. This way this parts
will be installed as dependency::
>>> write('foo.in', '{{bar}}')
>>> write('buildout.cfg', '''
... [buildout]
... parts = template
...
... [template]
... recipe = slapos.recipe.template:jinja2
... template = foo.in
... template = inline:{{bar}}
... rendered = foo
... context = key bar dependency:foobar
...
......@@ -443,7 +445,6 @@ Let's create a sample recipe modifying its option dict::
Let's just use ``buildout.cfg`` using this egg::
>>> write('foo.in', '{{bar}}')
>>> write('buildout.cfg',
... '''
... [buildout]
......@@ -452,7 +453,8 @@ Let's just use ``buildout.cfg`` using this egg::
...
... [template]
... recipe = slapos.recipe.template:jinja2
... template = foo.in
... template = inline:
... {{bar}}
... rendered = foo
... context = key bar sample:data
...
......
......@@ -133,13 +133,19 @@ class Recipe(object):
umask = None
def __init__(self, buildout, name, options):
self.template = zc.buildout.download.Download(
template = options['template']
if template.startswith('inline:'):
template = template[7:].lstrip('\r\n')
self.get_template = lambda: template
else:
template = zc.buildout.download.Download(
buildout['buildout'],
hash_name=True,
)(
options['template'],
template,
md5sum=options.get('md5sum'),
)[0]
self.get_template = lambda: open(template).read()
import_delimiter = options.get('import-delimiter',
DEFAULT_IMPORT_DELIMITER)
import_dict = {}
......@@ -203,7 +209,7 @@ class Recipe(object):
undefined=StrictUndefined,
loader=self.loader,
).from_string(
open(self.template).read(),
self.get_template(),
).render(
**self.context
)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment