如何在 google-app-engine 模板中启用方法
方法是:
def printa(x):
return x
响应是:
self.response.out.write(template.render(path, {'printa':printa}))
html是:
{{ printa 'sss'}}
我想在我的页面中显示'sss',
那么如何做到这一点,
更新
我创建一个templatetags文件夹和2个py文件:
templatetags
|--------__init__.py
|--------tags.py
在标签中.py 是:
#from django import template
from google.appengine.ext.webapp import template
register = template.Library()
def printa():
return 'ssss'
register.simple_tag(printa)
html 是:
{% load tags%}
{% printa %}
但它显示错误,错误是:
TemplateSyntaxError: 'tags' is not a valid tag library: Could not load template library from django.templatetags.tags, No module named tags
为什么?
怎么了?
答案是:
tags.py:
from google.appengine.ext import webapp
register = webapp.template.create_template_register()
@register.filter
def printa(value,y):
return 'url:%s' % y
@register.tag
def printb(x,y):
return str(x.__dict__) +'dddddddddddddddddddddddddddddddd'+ str(y.__dict__)
#return x
#register.tag('printb',do_pagednav)
然后在html中(a是我发送到模板的变量):
{{a|printa:"dwqdq"}}
{% printb %}
woring:
不要使用加载:
{% load sometags %}
the method is:
def printa(x):
return x
the response is:
self.response.out.write(template.render(path, {'printa':printa}))
the html is:
{{ printa 'sss'}}
I want to show 'sss' in my page ,
so how to do this ,
updated
I create a templatetags folder, and 2 py file:
templatetags
|--------__init__.py
|--------tags.py
in tags.py is:
#from django import template
from google.appengine.ext.webapp import template
register = template.Library()
def printa():
return 'ssss'
register.simple_tag(printa)
the html is:
{% load tags%}
{% printa %}
but it show error, and the error is:
TemplateSyntaxError: 'tags' is not a valid tag library: Could not load template library from django.templatetags.tags, No module named tags
why?
what's wrong?
answer is:
tags.py:
from google.appengine.ext import webapp
register = webapp.template.create_template_register()
@register.filter
def printa(value,y):
return 'url:%s' % y
@register.tag
def printb(x,y):
return str(x.__dict__) +'dddddddddddddddddddddddddddddddd'+ str(y.__dict__)
#return x
#register.tag('printb',do_pagednav)
and then in html (a is a variable i send to the template):
{{a|printa:"dwqdq"}}
{% printb %}
woring:
don't use load:
{% load sometags %}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用默认的 webapp 模板系统(实际上是 Django 0.96),你不能这样做。您应该将程序逻辑放在程序文件中,而不是放在模板中,因此您无法将参数传递给变量。
不过,你并没有说出你实际上想做什么;我假设您实际上并不想打印某些内容,因为您可以将该内容放入模板中而不需要函数并打印。无论您实际想要做什么,注册 自定义过滤器可能就是您正在寻找的。
Using the default webapp template system (which is actually Django 0.96), you can't do this. You're expected to put the program logic in the program files, not in your templates, so you can't pass arguments to your variables.
You don't say what you're actually trying to do, though; I assume you don't literally want to print something, since you can just put that something in the template without a function and it prints. For whatever you're actually trying to do, registering a custom filter might be what you're looking for.