Example of introducing tags
I have added a module with templetags and due to jinja its really hard to figure out how i can simply do something like this:
{% experiment "signuptext" variants "control,free,trial" %}
{% if contributors and settings.SIDEBAR_MAIN_SHOW_AVATARS %}
{% include "widgets/contributors.html" %}
{% endif %}
because moment I try to add {% experiment "signuptext" variants "control,free,trial" %} it complains saying:
Request Method: GET
Request URL: http://localhost:8001/questions/
Django Version: 1.4.10
Exception Type: TemplateSyntaxError
Exception Value:
Encountered unknown tag 'experiment'.
Exception Location: /../../../../../askbot/templates/main_page/sidebar.html in template, line 6
Python Executable: /../..//bin/python
Python Version: 2.7.5
Here is the implementation of the experiment:
@register.tag
def experiment(parser, token):
try:
tag_name, exp_name, variants_label, variantstring = token.split_contents()
except ValueError:
raise template.TemplateSyntaxError, '%r tag requires exactly three arguments, e.g. {% experiment "signuptext" variants "control,free,trial" %}' % token.contents.split()[0]
return ExperimentNode(exp_name.strip("\"'"), variantstring.strip("\"'").split(","))
Comments
Could you point, please to the implementation of "experiment"? Is it a third-party django app?
@Evgeny just updated the question thanks,
@Evgeny This is an implementation i did since no body answer my question in askbot forum. So here is what I am trying to do I posted the question in stackoverflow. http://stackoverflow.com/questions/21142591/django-lean-introducing-new-template-tags since I couldn't figure out above mention problem I ended up building a simple version but same issue. So I guess if you can provide an answer to stackoverflow question that would be great. Or if you can provide the solution for above thats fine too. THanks
Sorry, I can't answer your question directly right now, but here you don't have a choice but read the Jinja2 documentation about the template tags and filters. If you can replace that implementation as template filter, then that will be easy in Jinja2.