我想在Django模板标签中连接一个字符串,比如:
{% extend shop/shop_name/base.html %}
这里shop_name是我的变量,我想将它与rest of path连接起来。
假设我有shop_name=example.com,我想要结果扩展shop/example.com/base.html。
我想在Django模板标签中连接一个字符串,比如:
{% extend shop/shop_name/base.html %}
这里shop_name是我的变量,我想将它与rest of path连接起来。
假设我有shop_name=example.com,我想要结果扩展shop/example.com/base.html。
当前回答
使用:
{% with "shop/"|add:shop_name|add:"/base.html" as template %}
{% include template %}
{% endwith %}
其他回答
我已经更改了文件夹层次结构
/shop_name/shop/base.html
下面也可以。
{% extends shop_name|add:"/shop/base.html"%}
现在它能够扩展base.html页面。
在我的情况下,我需要concatenate发送一个参数连接到simple_tag的字符串,我不需要与,这节省了2行:
{% method firstParam "stringSecondParam="|add:valueSecondParam thirdParam as result %} 在这种情况下,问题的解决方案将是:"string="|add:object
您不需要编写自定义标记。只需要对相邻的变量求值。
"{{ shop name }}{{ other_path_var}}"
看看添加过滤器。
编辑:你可以链式过滤器,所以你可以做"shop/"|add:shop_name|add:"/base.html"。但这是行不通的,因为它由模板标记来计算参数中的过滤器,而extends则不行。
我想在模板中不能这样做。
这个怎么样!我们有first_name和last_name,我们希望将它们分开显示为空格“”。
{% with first_name|add:' '|add:last_name as name %}
<h1>{{ name }}</h1>
{% endwith %}
我们实际做的是:first_name + ' ' + last_name