Django blocktrans - inside HTML tag - django

Is it possible to put some HTML tag inside this Django block: {% blocktrans %}{% endblocktrans %}?
For example:
{% blocktrans %}Django<br>framework needed{% endblocktrans %}

Certainly, sometimes you even have to use blocktrans template tag actually.
Check:
Translating text blocks with Django .. what to do with the HTML?
Django templates: Best practice for translating text block with HTML in it
https://groups.google.com/forum/#!topic/django-users/j_r6y1VeAag

From the docs:
HTML markup, however, is common enough that it's probably ok to use in translatable strings. But please bear in mind that the GNU
gettext tools don't verify that the translations are well-formed HTML.

Related

Is space matter in Jinja?

What David has written in CS50 2022 - Lecture 9 - Flask, layout.html is
{% block body %}{% endblock %}
What I typically do in Django, a similar Python web framework is
{% block body %}
{% endblock %}
Is space matter in Jinja? Is it a matter of style? Because I know HTML is also space insensitive.
I do not believe spaces are important. Just there are replicated in the rendered template.
In template components most included, many spaces in the component can create many spaces in the rendered html that you will send to web client. I use the {% spaceless %}{% endspaceless %} for removing them in django template language.
In Jinja, you can set trim_blocks and lstrip_blocks on the env.
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True
You can also use - for removing spaces in conditional block:
{%- if ... -%} --> strips before and after
You can use + for be sure to preserve space in same way.

Escape jinja2 syntax in a jinja2 template

I serve dynamic pages from Jinja2 templates in Flask. Now I am defining client-side templates in say, Jinja2-clone Nunjucks inside a script tag. Problem is, the client-side templates has syntax like <% %> that Flask's Jinja2 interpreter may interpret instead of rendering verbatim.
How can I make the entire block of scripts render verbatim?
You can disable interpretation of tags inside a {% raw %} block:
{% raw %}
Anything in this block is treated as raw text,
including {{ curly braces }} and
{% other block-like syntax %}
{% endraw %}
See the Escaping section of the template documentation.

_() or {% trans %} in Django templates?

In Django templates, you can use either {{ _("Hello World") }} or {% trans "Hello World" %} to mark strings to be translated. In docs, the “official” approach seems to be the {% trans %} tag, but the _() syntax is mentioned too once.
How these approaches differ (except syntax) and why should be one preferable rather than the other?
One difference is that you obviously can't use {% trans %} with tags and filters. But does that mean that I can just use _() everywhere, like {{ _("String") }}? It works and looks much cleaner and more consistent than using {% trans "String" %} with standalone strings and _() with tags and filters.
So it seems that there's technically no difference as of Django 1.5. Template engine internally marks a variable for translation (by setting its translate attribute) in two cases:
when you do {% trans VAR %} (see TranslateNode), or
if the name of a variable starts with _( and ends with ) (see Variable.__init__).
Later, when the variable is being resolved, Django wraps it with ugettext or pgettext if it sees the translate attribute.
However, as can be seen from source code, there are some flexibility considerations in favor of {% trans %} tag:
you can do {% trans "String" noop %}, which will put the string for translation into .po files, but won't actually translate the output when rendering (no internal translate attribute on variable, no ugettext call);
you can specify message context*, like {% trans "May" context "verb" %};
you can put translated message into a variable for later use*, like {% trans "String" as
translated_string %}.
* As of Django 1.4.
Please feel free to correct me or post a better answer in case I'm missing anything.
The trans template tag calls the ugettext() function. In Django _() is an alias to ugettext().
This is covered in the django docs.

How do you print out the exact text "{{text}}" in a Django template?

How do you print out "{{text}}" in a Django template? If I type it into a Django html template it gets interpreted as the variable text. I want the actual text:
{{text}}
To appear in the html output.
To output the characters used to compose template tags, you have to use a specific template tag called templatetag. If you want to output the {{ characters, for example, you use {% openvariable %} and the output of that template tag would be {{.
So for your example,
{% openvariable %} text {% closevariable %}
would output:
{{ text }}
The best way is to use the templatetag tag. However, if i recall correctly, using {{ "{{text}}" }} will also work, this is however undocumented behaviour, so there is no real guarantee this will never break.

Writing eclipse templates

I am writing django templates in Eclipse->prefrences->templates, to autocomplete DJango templates. I wrote this
{% block ${cursor} %}
{% endblock %}
Now, when I request and do autocompletion, after typing {% the autocompletion is
{% {% block %}
{% endblock %}
While I would like
{% block %}
{% endblock %}
With cursor after block. How can I do this?
Instead of typing {% and selecting dj_for_empty, try typing dj_ and then auto-completing. It will behave the way you expect in that case.
BOTTOM-LINE: You auto-complete the templates into the editor based on the template name, not based on the template contents.
It appears that autocompletion has two sources: regular HTML tags (for which I can't find the definitions to change anywhere in Eclipse, sorry) and the templates themselves (which you correctly demonstrated in your comment with the screenshot).
Look at this image:
Instead of typing <t and triggering auto-complete, I typed t. You can see that there are entries with <> - indicating these are autocompletions based on the actual HTML tag - and entries with # - indicating these are autocompletions based on a template.
It appears templates are to be accessed by the name of the template. Notice that the template named table provides a complete <table> and not just the <table></table> that is autocompleted if you just type <tab and autocompletes.