如何在谷歌应用程序引擎的模板中启用方法 [英] how to enable a method in tamplate of google-app-engine

查看:175
本文介绍了如何在谷歌应用程序引擎的模板中启用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  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

tags.py是:

  #from django导入模板
from google.appengine.ext.webapp导入模板

register = template.Library()

def printa():
return'ssss'
register.simple_tag(printa)

html是:

  {%载入标签%} 
{%printa%}

但显示错误,错误是:

  TemplateSyntaxError:'tags'不是有效的标签库:无法从django.templatetags.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%} 


解决方案

使用默认的webapp模板系统(实际上是Django 0.96),你不能这样做。您需要将程序逻辑放在程序文件中,而不是放在您的模板中,因此您无法将参数传递给您的变量。



您不说虽然,你实际上想要做的是什么;我假设你不会真的想要打印某些东西,因为你可以将这些东西放在模板中,而不需要打印任何功能。对于您实际尝试执行的操作,请注册自定义过滤器可能是你要找的。

the mothed is :

def printa(x):
    return x

the reponse 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 ,

thanks

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 ?

thanks

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 %}

解决方案

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.

这篇关于如何在谷歌应用程序引擎的模板中启用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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