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(","))
SocialQA's avatar
265
SocialQA
asked 2014-01-16 10:58:13 -0600, updated 2014-01-16 11:02:43 -0600
edit flag offensive 0 remove flag close merge delete

Comments

1

Could you point, please to the implementation of "experiment"? Is it a third-party django app?

Evgeny's avatar Evgeny (2014-01-16 11:00:54 -0600) edit

@Evgeny just updated the question thanks,

SocialQA's avatar SocialQA (2014-01-16 11:03:09 -0600) edit

@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

SocialQA's avatar SocialQA (2014-01-16 11:05:58 -0600) edit

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.

Evgeny's avatar Evgeny (2014-01-16 11:15:18 -0600) edit
add a comment see more comments