将自定义django模板标记转换为jinja格式

时间:2014-01-16 19:00:20

标签: django django-templates jinja2 django-custom-tags

我有一个问题,我试图将现有的自定义django模板标记实现转换为jinja2格式。到目前为止,我没有在哪里,我已经在我的项目的templatetag文件夹中实现了

from django import template
from django.utils.html import escape
from django.utils.safestring import mark_safe
from coffin import template as coffin_template

import logging

register = template.Library()


from answer.abtest.models import Goal, Subject, GoalRecord, Enrollment, Experiment, ExperimentReport

CTX_PREFIX = "__abtest__experiment__"

class ExperimentNode(template.Node):
    def __init__(self, exp_name, variants):
        self.exp_name = exp_name
        self.variants = variants

    def render(self, context):
        if "request" not in context:
            raise template.TemplateSyntaxError("Use of abtesttags requires the request context processor. Please add django.core.context_processors.request to your settings.TEMPLATE_CONTEXT_PROCESSORS.")

        request = context["request"]
        exp = request.experiments

        if not exp:
            raise template.TemplateSyntaxError("Use of abtesttags requires the abtest middleware. Please add abtest.middleware.ExperimentsMiddleware to your settings.MIDDLEWARE_CLASSES.")

        expvariant = exp.declare_and_enroll(self.exp_name, self.variants)
        context[CTX_PREFIX + self.exp_name] = expvariant

        return "" # "exp: %s - you are %s" % (self.exp_name, expvariant)

class HypNode(template.Node):
    def __init__(self, exp_name, exp_variant, nodelist):
        self.exp_name = exp_name
        self.exp_variant = exp_variant
        self.nodelist = nodelist

#         print ' ++ instantiated HypNode (%s, %s)' % (self.exp_name,
#                                                      self.exp_variant)

    def render(self, context):
#         print ' == rendering HypNode (%s, %s)' % (self.exp_name,
#                                                   self.exp_variant)

        ctxvar = CTX_PREFIX + self.exp_name

        if ctxvar not in context:
            raise template.TemplateSyntaxError("Experiment %s has not yet been declared. Please declare it and supply variant names using an experiment tag before using hyp tags.")

        if self.exp_variant == context[ctxvar]:
            return self.nodelist.render(context)
        else:
            return ""

        return "[%s==%s]"%(self.exp_variant, context[ctxvar])+self.nodelist.render(context)+"[/%s]"%self.exp_variant

        return "HypNode: exp_name=%s, exp_variant=%s" % (self.exp_name,
                                                         self.exp_variant)



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



@register.tag
def hyp(parser, token):
    try:
        tag_name, exp_name, exp_variant = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError, "%r tag requires exactly two arguments" % token.contents.split()[0]


#     print "*** hyp looking for next tag"
    #print "parser.tokens = %r" % [ t.contents for t in parser.tokens ]

    nodelist = parser.parse(("endhyp",))
    token = parser.next_token()

#     print " * hyp FOUND TOKEN %s" % token.contents
    parser.delete_first_token()
    #print "parser.tokens = %r" % [ t.contents for t in parser.tokens ]

    return HypNode(exp_name.strip("\"'"), exp_variant.strip("\"'"), nodelist)

我试过阅读jinja文档,但我无法弄清楚我是怎么做的。我试图将其更改为jinja模板结构的原因是,当我尝试在模板中使用模板标记时,我收到错误说明未知模板标记实验:

{# first declare the experiment and its variants #}
{% experiment "signuptext" variants "control,free,trial" %}

我尝试使用coffin_template,但这并没有真正帮助。

0 个答案:

没有答案