2011-10-31 56 views
3

我有一个国际化的Django 1.3网站,并希望这样做:在包括模板标签Django的变量替换

{% include "snippets/button.html" with button_text=_("Logout {{ user.username }} now") %} 

而且snippets/button.html看起来是这样的:

<button 
    type="{{ button_type|default:_('submit') %}" 
    class="all_my special classes" 
    {% if button_title %} title="{{ button_title }}"{% endif %}> 
    <span class=ui-button-text>{{ button_text|default:_("Submit") }}</span> 
</button> 

的唯一途径,我可以看到这样做是这样的:

{% include "snippets/button.html" with button_text="Logout "|add:user.username|add:" now" %} 

但这是不可接受的,因为字符串将需求转化为包括发生变量替换的地方。我已经看到Interpolate Django template include variable但这不包括这种用法。

回答

0

我认为在这种情况下你最好的选择是将已经翻译的字符串添加到你的上下文中。

在你views.py

... 
'button_text': _("Logout {} now").format(user.username), 
... 

然后在你的模板:

{% include "snippets/button.html" with button_text=button_text %}