I basically want to do something like this in my base template:
{% if the block 'headline' is not empty %}
<div class="something"><h1>{% block headline %}{% end block %}</h1></div>
{% endif %}
In jinja2 it appears blocks are not variables and you can't get at their contents or test their values, or anything else but output them.
This seems like it would be a no-brainer to allow this, but I can't figure out a way. Do I have to use macros instead of blocks?
You should be able check the contents of a block using the self.blockname syntax.
{% if self.headline() is not empty %}
{# Write out Headline HTML wrapper here #}
{% endif %}
To quote from the documentation:
If you want to print a block multiple times you can however use the special self variable and call the block with that name:
<title>{% block title %}{% endblock %}</title>
<h1>{{ self.title() }}</h1>
{% block body %}{% endblock %}
Related
If I have this in my "main" (or project's) base.html:
{% block content %}
I'm inside block content.
{% block main_sidebar %}
I'm inside my sidebar now.
{% endblock main_sidebar %}
{% endblock %}
And, in the app's base.html (this base extends off the main base.html), I have this:
{% block main_sidebar %}
I want to change this.
{% endblock main_sidebar %}
The output doesn't change in my app's base.html for the main sidebar.
If I change {% block content %} to a custom name, then it works. I guess {% block content %} has special privileges as a block name and so can't be overwritten in a case like the one above?
I need to create a small side block with form(it contains only one field and button) and I want it to be included to every page except base.html
I thought about making simple view function, but maybe there are better ways to do this?
I'm using Python and Django 1.6
In general, you shouldn't use base.html directly, but because you are and because it would be a huge hassle to change it in every other template, what you can do is, in the view function that returns base.html, you can add a boolean to the context and check the boolean to determine what template you are using.
Something like this:
def view_that_uses_base.html(request):
is_base = True
return render_to_response("base.html", {"is_base":is_base}, RequestContext(request,{}))
And then in the template:
{% block sidebar %}
{% if is_base%}
{% else %}
#Your code here
{% endif %}
{% endblock sidebar %}
You must use templates to do that.
In other words, try creating $DJANGO_ROOT/templates/main.html using the following code:
<html>
<head>
</head>
<body>
{% block one_field_and_a_button %}
<input />
<button>I am everywhere</button>
{% endblock %}
{% block my_custom_content %}
{% endblock %}
</body>
<html>
Then all other templates must extend that main.html template and insert their own data.
Imagine this is $DJANGO_ROOT/templates/login.html. It will only replace "my_custom_content" and will inherit all other blocks including "one_field_and_a_button"
{% extends 'templates/main.html' %}
{% block my_custom_content %}
Hello World! This is the login
{% endblock %}
Finally, if you want to have a base.html that does not have that part of the code containing one field and a button, you can do the following.
Imagine this is $DJANGO_ROOT/templates/base.html. It will replace both "one_field_and_a_button" and "my_custom_content". However, in this case, "one_field_and_a_button" will be replaced with blank space that will not show in your html code.
{% extends 'templates/main.html' %}
{% block one_field_and_a_button %} {% endblock %}
{% block my_custom_content %}
Hello World! This is my base.html template
{% endblock %}
Hope it works for you!
You can use block tag in base.html, i think you are searching foe something like this
base.html
{% block code %}
{% include 'sidebar.html' %}
{% endblock %}
index.html
{% extends base.html %}
{% block code %}
{% endblock %}
and every other templates
just extend base html
{% extends base.html %}
I'm currently writing a quick python script to adapt all my old templates to a new base-template.
To do this I need to move the code inside the {% block body %} somewhere else.
I already got this one to match all my {% load smth %}
r"\{% load [^\{%]+? %\}"
What I want to match is the code between {% block body %} and {% endblock %}
Example:
{% block body %}
<div class="row">
<div class="span12">
[...]
</div>
</div>
{% endblock %}
unfortunately, there is no way to use regexp here, unless you use the notation {% endblock body %}, or you don't use nested blocks. Here is an example why it will fail:
{% block body %}
<div class="row">
<div class="span12">
{% block foo %}
[...]
{% endblock %}
</div>
</div>
{% endblock %}
regex will catch nested {% endblock %} as end of the body block
Leaving the obvious problem of nested blocks aside, this would be the regex to match all not-nested blocks:
\{% block [^\{%]+? %\}[\s\S]*\{% endblock %\}
Let's say I have this structure:
{# base.html #}
{% block content %}{% endblock %}
{# page.html #}
{% extends "base.html" %}
{% block content %}
{% include "snippet.html" %}
{# and I also want somehow to redefine {% block snippet_content %} of snippet here #}
{% endblock %}
{# snippet.html #}
<bells_and_whistles>
{% block snippet_content %}{% endblock %}
</bells_and_whistles>
I hope that the code is self-explaining.
Is there an elegant way to achieve this?
I'm afraid it is not possible in the way you want to do it.
Your options are:
Create a modified_snippet.html inheriting from snippet.html and overriding the block and include it instead
Change your snippet_content block to a {{ snippet_content }} variable and pass its contents using {% include "snippet.html" with snippet_content="My content" %}. Of course this method is quite limited.
So if I were to do something like {% block content %}
{{variable}}
{% endblock %}
in my HTML, and variable is equal to "Test <br /> test2" how come the prints out and does not make a new line? is there a way to fix this?
Jinja2 automatically escapes special characters for you. Probably simplest way is to use safe filter:
{{ variable|safe }}
If your output is escaped and you see literal <br /> text in your browser, switch off autoescaping for the variable:
{% block content %}{% autoescape false %} {{variable}} {% endautoescape %}{% endblock %}
or tell Jinja2 that the variable is safe for interpolation:
{% block content %} {{variable|safe}} {% endblock %}