9

I'm trying to create Django custom tags with Google App Engine but for some reason it does not work all the time. I believe my tags are correctly registered as Django is parsing them but the render method is never called. The strangest thing is that my tags work when placed inside a for loop {% for ... %} but never outside.

Here's the code:

in django/mytags.py

from django import template from google.appengine.ext import webapp register = webapp.template.create_template_register() # This works all the time @register.simple_tag def hello_world(): return u'Hello world' @register.tag('foo') def foo(parser, token): return FooNode() class FooNode(template.Node): def __init__(self): self.foo = 'foo' def render(self, context): return self.foo 

in main.py

from google.appengine.ext.webapp import template template.register_template_library('django.mytags') ... self.response.out.write(template.render('main.html', template_values)) 

in main.html

{% foo %} {% for item in items %} {% foo %} 

and the result:

<django.mytags.FooNode object at 0x000000001794BAC8> foo foo foo ... 

This is driving me insane. I suspect putting my tag in a for loop forces the node to be rendered (where it should have been done already).

8
  • Just a thought, but what if you add a __unicode__() function to the FooNode class? Commented Jan 12, 2013 at 20:39
  • @frb - Adding __unicode__() would just hide the problem in this case. I need the render() method to be called with the context as parameter to be able to do more than just returning a string. Commented Jan 12, 2013 at 21:11
  • @nhuon: But I thought render() always needs to return a string (even if it's empty)? P.S. I don't know why this is happening. It seems __init__() is not been called. Commented Jan 19, 2013 at 10:18
  • @stellarchariot - What I meant was that I need to use the context that is passed to the render() call to be able to select the right string to return. If the outcome of render was always the same I guess I would not bother myself with tags. As for __init__(), it's being called so the node is created but not rendered. Commented Jan 20, 2013 at 3:01
  • dig into your server and look at the code that has been auto-generated to implement the jsp page. it will be there somewhere. reading the code will probably explain what is happening. Commented Jan 21, 2013 at 0:39

2 Answers 2

1

You need to add string representation for you class

class FooNode(template.Node): def __init__(self): self.foo = 'foo' def render(self, context): return self.foo def __unicode__(self): return 'string to put in template' 
Sign up to request clarification or add additional context in comments.

2 Comments

Works fine now with the string representation
Actually, I still have the same issue. It works in for loops, not outside.
0

Didn't you forget to add {% load mytags %}? (Should be used, per custom tag docs)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.