django templatetags模板,将{{}}方法调用与模板标签上下文变量相结合 [英] django templatetags template , combine {{ }} method call with template tag context variable

查看:83
本文介绍了django templatetags模板,将{{}}方法调用与模板标签上下文变量相结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使模板标签的结果与另一个模板标签相关。用例如下。我有一个标题列表,其中包含我想在表格中显示的所有列+他们显示的模型的列+是否可见。

  LIST_HEADERS =(
('Title','title',True),
('First Name','first_name',True),
姓氏','last_name',True),
('Modified At','modified',False),

现在我有一个模板标签打印出所有的标题。因此,我想创建一个打印出表的正文的模板标签。因此,我想采取标题列表,并检查哪个标题是可见的,并希望相应地显示或隐藏我的价值。



因此,我创建了以下模板标签模板:

 < tr class ={%cycle odd,even%}> 
{%表头中的标题%}
{%if header.visible%}
< td>< a href ={{model_instance.get_absolute_url | escape}}> {{model_instance.title}}< / a>< / td>
{%else%}
< td style =visibility:hidden;>< a href ={{model_instance.get_absolute_url | escape}}> {{model_instance.title} }< / A>< / TD>
{%endif%}
{%endfor%}
< / tr>

你在那里看到值{{model_instance.title}}。这个值我想改变为model_instance.title,model_instance.first_name,model_instance.last_name,...在运行时。



因此,我可以搜索如何组合{ {model_instance}} with header.model_column。



model_column等于LIST_HEADERS中的第二个条目。因此,model_column将是title,first_name,..



因此解决方案将是类似[伪代码] {{model_instance.header.model_column}} [伪代码]



..所以我搜索一个方法,我如何可以组合一个django模板方法调用与django模板标签属性... huh ..听起来很疯狂:D



我希望我解释够了!可能有一个更容易解决我的问题。但这似乎对我来说非常通用和容易,并且会奏效。

解决方案

简化这个。



首先, Django模板语言实际上可以。这不是很多它可以参考变量,列表和字典。



如果您在查看函数中执行所有工作,这将更简单。


$ b $对于标题,field_name,在LIST_HEADERS中可见的
如果可见,则为


else: style =
show.append((style,title,getattr(object,field_name))
render_to_response(template,{'show_list':show,...},...)

在您的模板中

 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ {style}}>< a href =...> {{value}}< / a>< / td> 
{%endfor%}

的确,建议您从查看功能中删除LIST_HEADERS。

  show = [
(,'Title',object.title),
(,'First Name',object.first_name),
(,姓氏,object.last_name),
(可见性:隐藏,修改为,object.modified),
]
render_to_response(template,{'show_list':show,...},...)

我发现以上工作更容易使用,因为它是明确的,它在视图功能中。


i m trying to make the result of a template tag dependent from another template tag. the use case is the following. i have a headers list which contains all the columns i want to show in a table + the column of the model they are showing +whether they are visible or not.

LIST_HEADERS = (
    ('Title', 'title', True),
    ('First Name', 'first_name', True),
    ('Last Name', 'last_name', True),
    ('Modified At', 'modified', False),
)

now i have a templatetag which prints out all the headers. consequently i wanted to create a template tag which prints out the body of the table. therefore i want to take the headers list and check which header is visible and want to accordingly show or hide my value.

therefore i created the templatetag template below:

<tr class="{% cycle odd,even %}">
  {% for header in headers %}
  {% if header.visible %}
    <td><a href="{{ model_instance.get_absolute_url|escape }}">{{ model_instance.title }}</a></td>
  {% else %}
    <td style="visibility:hidden;"><a href="{{ model_instance.get_absolute_url|escape }}">{{ model_instance.title }}</a></td>
  {% endif %}
  {% endfor %}
</tr>

you see the value {{ model_instance.title }} there. this value i want to change to model_instance.title, model_instance.first_name, model_instance.last_name, ... at runtime.

thus i m searching a way how i can combine {{ model_instance }} with header.model_column .

model_column equals to the second entry in the LIST_HEADERS. Thus model_column would be title, first_name,..

thus the solution would be something like [pseudocode] {{ model_instance.header.model_column }} [pseudocode]

..thus i search a way how i can combine a django template method call with a django template tag attribute..huh.. sounds crazy :D

i hope i explained it good enough! probably there is a much easier solution to my problem. but this seems to me pretty generic and easy and would work.

解决方案

Simplify this.

First, read about the things the Django template language actually can do. It's not much. It can deference variables, lists and dictionaries.

It's simpler if you do all the "work" in your view function.

show = [ ]
for  title, field_name, visible in LIST_HEADERS:
     if visible: style= "visibility:hidden"
     else: style= ""
     show.append( (style, title, getattr(object,field_name) )
render_to_response( "template", { 'show_list': show, ... }, ... )

In your template

{% for style, name, value in show_list %}
<tr class="{% cycle odd,even %}">
    <td class="{{style}}"><a href="...">{{value}}</a></td>
{% endfor %}

Indeed, I'd suggest dropping LIST_HEADERS from your view function.

show = [ 
    ("", 'Title', object.title),
    ("",'First Name', object.first_name),
    ("",'Last Name', object.last_name),
    ("visibility:hidden",'Modified At', object.modified),
]
render_to_response( "template", { 'show_list': show, ... }, ... )

I find the above much easier to work with because it's explicit and it's in the view function.

这篇关于django templatetags模板,将{{}}方法调用与模板标签上下文变量相结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆