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 %}
Related
I have two Django templates (in a Pinax 0.9.x project) with text in a blocktrans block. One is working and the other isn't.
The one that's working looks like:
{% extends "site_base.html" %}
...
{% block body %}
<h1>ABC</h1>
<h2><em>DEF</em></h2>
<p>
{% blocktrans %}
GHI
...
The one that's not working is getting a debug mode error of:
TemplateSyntaxError at /JKL/
Invalid block tag: 'blocktrans', expected 'endblock' or 'endblock body'
It follows those headings by giving the following source code, with the blocktrans highlighted in red:
{% extends "site_base.html" %}
{% block head_title %}MNO{% endblock %}
{% block body %}
<p>
{% blocktrans %}
PQR
{% endblocktrans %}
</p>
Are these two blocks of code isomorphic? If they aren't, what is the difference? What should I be doing to have paragraphs with their content working in blocktrans tags?
Thanks,
You have to load the i18n template tags:
<!-- your_template.html -->
{% load i18n %}
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 %\}
Is it possible to apply jinja2 filters to {% block ... %} constructs? What I was hoping to do was something along the lines of:
{% block content|upper %}
here is some content that will be rendered in upper case
{% endblock %}
...but this doesn't work; the above example will result in an error. Is there any other way to wrap a chunk of template text in a jinja2 filter?
You can use filter sections:
{% block content %}
{% filter upper %}
Here is some content that will be rendered in upper case.
{% endfilter %}
{% endblock %}
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 %}