开发者

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

开发者 https://www.devze.com 2022-12-31 11:30 出处:网络
the method is: def printa(x): return x the response is: self.response.out.write(template.render(path, {\'printa\':printa}))

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开发者_StackOverflow社区 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 %}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消