Le 07/02/2012 00:41, Daniel a écrit :
Hello everyone,

I was wondering if there is a way to test for the existence of content
in a block.

For example I have a layout template that is extended by all my other
templates. If those child templates don't specify content for a
specific block, how can I determine in the parent template if the
child template didn't specify anything for the content block so I can
have default content if it's not specified?

Hi,

This is not quite what you asked for, but we have a Jinja2 extension that adds a "showonmatch" tag:

http://paste.pocoo.org/show/547689/

Usage looks like this:

{% showonmatch "ul li a" %}
<ul>
  {% for item in some_list %}
     <li><a href="{{ item.url }}">{{ item.name }}</a></li>
  {% endfor %}
</ul>
{% else %}
<p>No items</p>
{% endshowonmatch %}

After the block (up to {% else %}) is executed, the result is parsed as HTML by lxml, and matched against the CSS selector. (The argument to the block.) If the selector matches anything the first part is rendered. Otherwise, the 'else' clause (if any) is rendered. (And the first part is thrown away, including the <ul> tags.)

This example is simple and a {% if %} would also work, but full CSS selectors are more valuable in more complex cases.

Regards,
--
Simon Sapin

--
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