Tags jinja2 macros vs jsp2.0

I am a java programmer learning python / jinja .

My biggest beef with jinja2 macros is limiting one caller (). for example, I could do the following in jsp2 tags:

tag def:

<% attribute name="title" fragment="true"> <div class='title'>${title}</div> <div class='body'><jsp:doBody/></div> 

using:

 <myTag> <jsp:attribute name='title'> <c:if test='${blah}'>This is only here sometimes</c:if> </jsp:attribute> <jsp:body> <c:if test='${something}'>some dynamic content</c:if> </jsp:body> </myTag> 

what I want stress is that both the contents of the body and the title attribute are dynamic . In addition, there are no hackers to set variables for dynamic content and transfer them.

now look at a jinja macro that does the same thing:

 {% macro myTag(title='', caller) -%} <div class='title'>{{ title }}</div> <div class='body'>{{ caller() }}</div> {%- endmacro %} 

but wait! I cannot easily put dynamic content in the title attribute!

 {% call myTag(title='imagine putting some content here that involves 5 loops, 4 ifs and whatnot?') %} {% if something %}some dynamic content{% endif %} {% endcall %} 

Is this a problem with being a newbie or is it a lack of genie?

+4
source share
4 answers

and now there is a better solution:

http://mankyd.github.com/jinjatag/

0
source

So this is actually the main function of Mako Templates for Python . It is not so widely used for a function, but it is important for me, as it is quite important in custom template tags, as you mention, so there:

http://www.makotemplates.org/docs/defs.html#calling-a-def-with-embedded-content-and-or-other-defs

JSP is one of several template systems that Mako draws inspiration from.

+5
source

In addition to the hack suggested by dave, you can pass the result of a local macro to any other macro. This is especially useful when you do not know in advance whether a parameter defined locally will be passed, or it will be obtained from a template variable:

 {% macro doTag(title, body) %} <div class='title'>{{ title }}</div> <div class='body'>{{ body }}</div> {% endmacro %} {% macro tagBody() %} {% if something %}some dynamic content{% endif %} {% endmacro %} {{ doTag(title='some simple title', body=tagBody() }} {% macro tagTitle() %} content here that involves 5 loops, 4 ifs {% endmacro %} {{ doTag(title=doTitle(), body=tagBody() }} 
+5
source

So, you are right that you cannot go through several blocks. However, you can pass the variable back to the caller that it can act on. So you can do something like this:

 {% macro test() %} {{ caller('a') }} {{ caller('b') }} {% endmacro %} {% call(which) test() %} {% if which == 'a' %}hello {% elif which == 'b' %}world {% endif %} {% endcall %} 

To describe this in more detail, call(which) defines a caller that takes one argument, which . When a macro refers to the caller, it passes the identifier to the caller for the content block that actually wants to return the caller. Then the caller can act on this.

This is not elegant proof, but it works.

+3
source

All Articles