First time here? Check out the FAQ!

Revision history  [back]

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(","))

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(","))