librelist archives

« back to archive

Call jinja2 macro by passing the name from a variable

Call jinja2 macro by passing the name from a variable

From:
dorian i
Date:
2013-01-19 @ 18:39
So i have a list of objects from different classes and i want to call:

{% for o in objects %}

 {{ o.__class__.__name__ }}   #I want to call the macro that is named as
the class name, not just the class title.

{% endfor %}

The macros don't have arguments. This can be fixed by many else-ifs.
If this is possible, of course.

Thanks

Re: [flask] Call jinja2 macro by passing the name from a variable

From:
Juan Pablo Scaletti
Date:
2013-01-20 @ 16:22
If the macros are in "macros.html", you can do:

   {% import "macros.html" as m %}

   {% for o in objects %}
    {{  m|attr(o.__class__.__name__)() }}
   {% endfor %}

It works, though it doesn't look very good . Couldn't you make the 
objects render their own template?

   {% for o in objects %}
    {{ o.render()|safe }}
   {% endfor %}

JP
> dorian i <mailto:dorian.hoxha@gmail.com>
> 19 de enero de 2013 13:39
> So i have a list of objects from different classes and i want to call:
>
> {% for o in objects %}
>
>  {{ o.__class__.__name__ }}   #I want to call the macro that is named 
> as the class name, not just the class title.
>
> {% endfor %}
>
> The macros don't have arguments. This can be fixed by many else-ifs.
> If this is possible, of course.
>
> Thanks

Re: [flask] Call jinja2 macro by passing the name from a variable

From:
dorian i
Date:
2013-01-20 @ 16:47
could not decode message