简体   繁体   English

如何在google-app-engine模板中启用方法

[英]How to enable a method in template of google-app-engine

the method is: 方法是:

def printa(x):
    return x

the response is: 响应是:

self.response.out.write(template.render(path, {'printa':printa}))

the html is: 的HTML是:

{{ printa 'sss'}}

I want to show 'sss' in my page , 我想在页面中显示“ sss”,

so how to do this , 那么如何做到这一点,

updated 更新

I create a templatetags folder, and 2 py file: 我创建一个templatetags文件夹和2个py文件:

templatetags 
     |--------__init__.py
     |--------tags.py

in tags.py is: 在tags.py中是:

#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: 的HTML是:

{% 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: 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): 然后在html中(a是我发送到模板的变量):

{{a|printa:"dwqdq"}}

{% printb %}

woring: 担心:

don't use load: 不要使用负载:

{% load sometags %}

Using the default webapp template system (which is actually Django 0.96), you can't do this. 使用默认的webapp模板系统(实际上是Django 0.96),您将无法执行此操作。 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. 无论您实际上要做什么,注册自定义过滤器都可能是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM