2010-12-08 487 views
154

我想連接字符串中的Django模板標籤一樣如何連接django模板中的字符串?

{% extend shop/shop_name/base.html %} 

這裏shop_name是我變,我想與路徑的其餘部分來連接這一點。
假設我有shop_name=example.com

而且我想結果延長shop/example.com/base.html

回答

307

使用帶:

{% with "shop/"|add:shop_name|add:"/base.html" as template %} 
{% include template %} 
{% endwith %} 
0

不能在Django模板進行變量操作。 你有兩個選擇,要麼寫自己的模板標籤,要麼在視圖中執行此操作,

+0

我的要求是隻在模板中做這樣的視圖選項是沒有幫助的。我也嘗試通過自定義模板標籤,但{%loading ....%}標籤後應該{%load concat%}。所以我現在怎麼做? – Ahsan 2010-12-08 10:35:50

+0

編寫一個接受字符串格式和參數的extended_extends標籤。 – 2010-12-08 11:33:03

+0

可以請你給我一個如何編寫默認的自定義標籤的例子嗎? – Ahsan 2010-12-08 11:43:02

1

看一看add filter

編輯:你可以鏈接過濾器,所以你可以做「shop /」| add:shop_name | add:「/ base.html」。但是這是行不通的,因爲它取決於模板標籤在參數中評估過濾器,而擴展則不是。

我想你不能在模板中做到這一點。

+0

這是不行的。我想在路徑中間添加我的變量。 – Ahsan 2010-12-08 10:53:46

+0

添加過濾器只根據django docs – Ahsan 2010-12-08 11:18:56

+0

總結不連接文檔說「可以強制爲整數的字符串將被求和」。其他字符串連接在一起。但是,這並不重要,因爲你不能使用過濾器:( – 2010-12-08 13:27:27

0

extends沒有這方面的設施。將整個模板路徑放在上下文變量中並使用它,或者複製存在的模板標籤並進行相應的修改。

1

從文檔:

這個標籤可以以兩種方式使用:

  • {% extends "base.html" %}(帶引號)使用字面值「base.html文件」作爲父模板的名字延長。
  • {% extends variable %}使用變量的值。如果變量的計算結果爲字符串,Django將使用該字符串作爲父模板的名稱。如果變量評估爲一個Template對象,Django將使用該對象作爲父模板。

所以好像你不能使用過濾器來操縱參數。在調用視圖中,您必須實例化祖先模板或使用正確的路徑創建一個字符串變量,並將其與上下文一起傳遞。

10

我已經改變了文件夾層次

/shop/shop_name/base.html /shop_name/shop/base.html

,然後在下面會工作。

{% extends shop_name|add:"/shop/base.html"%} 

現在它能夠擴展base.html頁面。

66

不要爲字符串使用add,你應該定義一個定製的標籤是這樣的:

創建一個文件:<appname>\templatetags\<appname>_extras.py

from django import template 

register = template.Library() 

@register.filter 
def addstr(arg1, arg2): 
    """concatenate arg1 & arg2""" 
    return str(arg1) + str(arg2) 

,然後用它作爲@Steven說

{% with "shop/"|addstr:shop_name|addstr:"/base.html" as template %} 
    {% include template %} 
{% endwith %} 

理由要避開add

按照docs

這個過濾器會第一試着將其轉換兩個值,以整數... 字符串可以強制轉換爲整數將總結,不連接 ...

如果兩個變量碰巧是整數,結果將是意想不到的。

1

@錯誤的答案基本上是正確的,你應該使用模板標籤。不過,我喜歡,我可以用它來執行任何類似這種操作的較籠統的模板標籤:

from django import template 
register = template.Library() 


@register.tag(name='captureas') 
def do_captureas(parser, token): 
    """ 
    Capture content for re-use throughout a template. 
    particularly handy for use within social meta fields 
    that are virtually identical. 
    """ 
    try: 
     tag_name, args = token.contents.split(None, 1) 
    except ValueError: 
     raise template.TemplateSyntaxError("'captureas' node requires a variable name.") 
    nodelist = parser.parse(('endcaptureas',)) 
    parser.delete_first_token() 
    return CaptureasNode(nodelist, args) 


class CaptureasNode(template.Node): 
    def __init__(self, nodelist, varname): 
     self.nodelist = nodelist 
     self.varname = varname 

    def render(self, context): 
     output = self.nodelist.render(context) 
     context[self.varname] = output 
     return '' 

,然後你可以在你的模板中使用這樣的:

{% captureas template %}shop/{{ shop_name }}/base.html{% endcaptureas %} 
{% include template %} 

正如註釋所提到的,這個模板標籤對整個模板中可重複使用的信息特別有用,但需要邏輯和其他東西來填充模板,或者需要重新使用通過模塊在模板之間傳遞的數據的情況:

{% captureas meta_title %}{% spaceless %}{% block meta_title %} 
    {% if self.title %}{{ self.title }}{% endif %} 
    {% endblock %}{% endspaceless %} - DEFAULT WEBSITE NAME 
{% endcaptureas %} 

然後:

<title>{{ meta_title }}</title> 
<meta property="og:title" content="{{ meta_title }}" /> 
<meta itemprop="name" content="{{ meta_title }}"> 
<meta name="twitter:title" content="{{ meta_title }}"> 

信用爲captureas標籤是由於這裏:https://www.djangosnippets.org/snippets/545/

1

我發現與{% with %}標籤的工作是相當麻煩。相反,我創建了以下模板標籤,它應該可以在字符串和整數上使用。

from django import template 

register = template.Library() 


@register.filter 
def concat_string(value_1, value_2): 
    return str(value_1) + str(value_2) 

然後在頂部使用加載模板標籤在你的模板如下:

{% load concat_string %} 

然後,您可以使用它的方式如下:

<a href="{{ SOME_DETAIL_URL|concat_string:object.pk }}" target="_blank">123</a> 

我個人認爲這與其合作更加清潔。

0

參考Concatenating Strings in Django Templates

  1. 對於早期版本的Django的:

    {{ 「瑪麗有隻小」 |的StringFormat: 「:S羔羊」 }}

「瑪麗有隻小羊羔」。

  • 否則:

    {{ 「瑪麗有一點」 |添加: 「羊羔。」 }}

  • 「瑪麗有隻小羊羔」。