Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Context value / variable not rendered inside blocktrans template tag

Tags:

I've a context processor which adds objects (i.e. site) to the template context but the value is not rendered inside the {% blocktrans %} template tag. Outside the template tag, the value prints just fine.

<h1>{% trans "About" %} {{ site.domain }}</h1> <!-- works -->

{% blocktrans %}
   {{ site.domain }} <!-- doesn't work -->
{% endblocktrans %}

How do I get the object's attribute / variable to render inside {% blocktrans %}?

like image 554
Franck Avatar asked Jan 25 '12 17:01

Franck


People also ask

What is Blocktrans in Django?

The point of blocktrans is to allow the text around the placeholders to be translated. It won't translate anything inside {{...}} . If you look at the generated .po file, you'll see that the following template code: {% blocktrans %}This is my variable: {{variable}}{% endblocktrans %}

What is context in Render?

What is the render context? The class RenderContext is automatically created by the graphics service. It is a collection of data that needs to be passed to every Render method. The render context should contain all information required to render an object or to perform a rendering step.

What is context in Django template?

A context is a variable name -> variable value mapping that is passed to a template. Context processors let you specify a number of variables that get set in each context automatically – without you having to specify the variables in each render() call.


1 Answers

Interpolated variables cannot be dotted expressions - you need something like this:

{% blocktrans with site_domain=site.domain %}{{ site_domain }} is a ...{% endblocktrans %}

See also:

  • Django documentation: blocktrans template tag
  • Question: django blocktrans and i18n in templates
like image 142
AdamKG Avatar answered Sep 28 '22 19:09

AdamKG