Reputation: 755
I have some helper functions I want to access from my templates, but having trouble accessing these. Using Jinja2 with Coffin (which is great, except I can't seem to access my global functions).
I register my objects in jinja.py
from coffin.template import Library
register = Library()
@register.object(name='tester')
def tester():
return 'TESTING YAA!!'
print (register.jinja2_globals)
My template tester.html looks like this:
{{ tester() }}
But this is what happens:
>>> from apps import jinja
{'tester': <function tester at 0x3133938>}
>>> jinja.tester()
'TESTING YAA!!'
Import works fine and the function registers correctly but can't access with render_to_response or render_to_string:
>>> from coffin.template.loader import render_to_string as rts
>>> rts('tester.html')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/pydev/drew/iame/lib/python2.6/site-packages/Coffin-0.3.7.dev-py2.6.egg/coffin/template/loader.py", line 55, in render_to_string
return template.render(context_instance)
File "/pydev/drew/iame/lib/python2.6/site-packages/Coffin-0.3.7.dev-py2.6.egg/coffin/template/__init__.py", line 56, in render
return super(Template, self).render(**context)
File "/pydev/drew/iame/lib/python2.6/site-packages/jinja2/environment.py", line 894, in render
return self.environment.handle_exception(exc_info, True)
File "/pydev/drew/iame/iame_cms/apps/templates/tester.html", line 1, in top-level template code
{{ tester() }}
UndefinedError: 'tester' is undefined
But isn't accessible in my template :(
Upvotes: 1
Views: 1373
Reputation: 32067
For your library to be loaded, it must be in a standard location, namely where Django expects such libraries:
https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#code-layout
i.e., something like my_app/templatetags/customlib.py
.
Upvotes: 2