Hi,

On 2010-06-17 12:23 PM, angelbit wrote:
I'm trying to create an extension like Django template tags for
jinja2. the basic idea is a tag in the main template( {% widget
last_tweets %} ) that return a node of a source taked from another
file ( widgets/last_tweets.html for example ).
Why do you need an extension for that?  A function is perfectly fine:

    from jinja2 import contextfunction

    @contextfunction
    def widget(context, template_name, **extra_context):
        t = jinja_env.get_template('widgets/' + template_name)
        ctx = dict(context.items())
        ctx.update(extra_context)
        return t.render(ctx)

    jinja_env.globals['widget'] = widget

And then in the template::

   {{ widget('last_tweets.html') }}

Now the problem is the Parser class, object of this type his passed
already instantiated on parse hook extension and don't have a method
for pass a custom jinja source code during runtime.
Jinja extensions work ahead of time. They emit nodes from an abstract syntax tree which are then used to generate sourcecode. This is usually a very complex topic and only recommended if functions no longer work which are a lot easier to implement and way easier to debug.

Keep in mind that Jinja2 has capable expressions which can replace about 95% of all Django extensions.


Regards,
Armin

--
You received this message because you are subscribed to the Google Groups 
"pocoo-libs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pocoo-libs?hl=en.

Reply via email to