Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

{% trans "string" as my_translated_string %} is not rendering content in template

Django 1.4 doc says that you can get translated strings into "vars" to be used in different places or to be used as arguments in template tags or filters using the following syntax:

{% trans "String" as my_translated_string %}

<h1>{{ my_translated_string }}</h1>

https://docs.djangoproject.com/en/1.4/topics/i18n/translation/#trans-template-tag

I'm doing it in that way, however the defined var is never rendering the content. Below my template code:

{% extends "default_layout.html" %}

{% load i18n %}

{% trans "My page title" as title %}

{% block meta_title %}{{ title }}{% endblock %}

{% block content %} 
    <h1>{{ title }}</h1>
{% endblock %}

Of course "title" is being rendered empty in both cases.

Am I missing something ?

Thank you.

like image 364
tufla Avatar asked Feb 10 '13 13:02

tufla


1 Answers

As okm said you need to define the variable within the block in which you want to use it, and the scope of that variable is also within the block:

{% extends "default_layout.html" %}
{% load i18n %}

{% block meta_title %}
{% trans "My page title" as title %}
{{ title }}
{% endblock %}

{% block content %}
{% trans "My page title" as title %}
    <h1>{{ title }}</h1>
{% endblock %}
like image 62
Aamir Rind Avatar answered Sep 25 '22 00:09

Aamir Rind