是否有可能从jinja2模板中访问对象的key_name? [英] Is it possible to access the key_name of an object from inside a jinja2 template?

查看:102
本文介绍了是否有可能从jinja2模板中访问对象的key_name?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在webapp框架中使用了python google app引擎,用jinja2模板替换了django模板。



我知道你可以从里面获得一个对象的key_name一个处理程序方法通过调用它像这样:

  goody = object.key()。name()

code>

但是可以从模板逻辑中访问key_name吗?这样的事情?

  {%for object in objectList%} 
{{object | key_name}}
{%endfor%}

显然这不起作用,但是有一些过滤器或隐藏属性我可以用它来获取应用引擎key_name,一个对象从模板内部存储起来,而不需要额外的处理器方法吗?

Jinja2允许你使用()来执行可调用命令,根据这里的文档: http://jinja.pocoo.org/docs/templates/#other-operators



所以,这应该工作:

  {%例如在instance_list%} 
{{instance.key()。 name()}}
{%endfor%}


I'm using python google app engine with the webapp framework, substituting jinja2 templates for django templates.

I know you can get the key_name of an object from inside a handler method by calling it like so:

goody = object.key().name()

But is it possible to access the key_name from within the template logic? Something like this?

{% for object in objectList %}
{{object|key_name}}
{% endfor %}

Obviously that doesn't work, but is there some filter or hidden attribute I can use to get at the app engine key_name an object is stored with from inside the template without extra fiddling inside the handler method?

解决方案

Jinja2 allows you to execute callables, just by using () according to the documentation here: http://jinja.pocoo.org/docs/templates/#other-operators

So, this should work:

{% for instance in instance_list %}
   {{ instance.key().name() }}
{% endfor %}

这篇关于是否有可能从jinja2模板中访问对象的key_name?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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