options = core.Options( LazyMVA('values'), 'as', arguments.Argument('varname', required=False, resolve=False), ) def render_tag(self, context, values, varname): if self not in context.render_context: context.render_context[self] = itertools_cycle(values) cycle_iter = context.render_context[self] value = cycle_iter.next().resolve(context) if varname: context[varname] = value return value register.tag('ct_cycle', Cycle) controls = [ ( "{% for x in sequence %}{% cycle '1' two %}{% endfor %}", "{% for x in sequence %}{% ct_cycle '1' two %}{% endfor %}", {'two': 2, 'sequence': range(50)} ), ( "{% for x in sequence %}{% cycle '1' two as varname %}{{ varname }}{% endfor %}", "{% for x in sequence %}{% ct_cycle '1' two as varname %}{{ varname }}{% endfor %}", {'two': 2, 'sequence': range(50)} ), ] pool.register(Cycle, controls=controls)
dj_tpl = template.Template('{% firstof a b c d %}') ctx = template.Context({'b': 'b'}) return ct_tpl, dj_tpl, ctx register = template.Library() class Firstof(core.Tag): name = 'ct_firstof' options = core.Options(arguments.MultiValueArgument('values'), ) def render_tag(self, context, values): for value in values: if value: return smart_unicode(value) return u'' register.tag('ct_firstof', Firstof) controls = [ ('{% firstof a b c d %}', '{% ct_firstof a b c d %}', { 'b': 'b' }), ('{% firstof a b c d %}', '{% ct_firstof a b c d %}', {}), ] pool.register(Firstof, controls=controls)
class With(core.Tag): name = 'ct_with' options = core.Options( arguments.Argument('value'), 'as', arguments.Argument('varname', resolve=False), blocks=['endwith'], ) def render_tag(self, context, value, varname, endwith): context.push() context[varname] = value output = endwith.render(context) context.pop() return output register.tag('ct_with', With) controls = [ ('{% with input|upper as output %}{{ output }}{% endwith %}', '{% ct_with input|upper as output %}{{ output }}{% endwith %}', { 'input': 'input' }), ] pool.register(With, controls=controls)
{'sequence': range(0)} ), ( "{% for x in sequence %}{{ forloop.counter }}: {{ x }}{% endfor %}", "{% ct_for x in sequence %}{{ forloop.counter }}: {{ x }}{% endfor %}", {'sequence': range(50)} ), ( "{% for x in sequence %}{{ forloop.counter }}-{{ x }}:{% for y in sequence %}{{ forloop.counter }}-{{forloop.parentloop.counter }}{{ y }}{% endfor %}{% endfor %}", "{% ct_for x in sequence %}{{ forloop.counter }}-{{ x }}:{% ct_for y in sequence %}{{ forloop.counter }}-{{forloop.parentloop.counter }}{{ y }}{% endfor %}{% endfor %}", {'sequence': range(50)} ), ( "{% for x,y in sequence %}{{ forloop.counter }}: {{ x }}-{{ y }}{% endfor %}", "{% ct_for x,y in sequence %}{{ forloop.counter }}: {{ x }}-{{ y }}{% endfor %}", {'sequence': [('a', 1), ('b', 2), ('c', 3), ('d', 4)]} ), ( "{% for x,y in sequence %}{{ forloop.counter }}: {{ x }}-{{ y }}{% endfor %}", "{% ct_for x y in sequence %}{{ forloop.counter }}: {{ x }}-{{ y }}{% endfor %}", {'sequence': [('a', 1), ('b', 2), ('c', 3), ('d', 4)]} ), ( "{% for x in sequence %}{{ forloop.counter }}: {{ x }}{% empty %}empty{% endfor %}", "{% ct_for x in sequence %}{{ forloop.counter }}: {{ x }}{% empty %}empty{% endfor %}", {'sequence': Iterable()} ), ] pool.register(For, controls=controls)
register = template.Library() class With(core.Tag): name = 'ct_with' options = core.Options( arguments.Argument('value'), 'as', arguments.Argument('varname', resolve=False), blocks=['endwith'], ) def render_tag(self, context, value, varname, endwith): context.push() context[varname] = value output = endwith.render(context) context.pop() return output register.tag('ct_with', With) controls = [ ('{% with input|upper as output %}{{ output }}{% endwith %}', '{% ct_with input|upper as output %}{{ output }}{% endwith %}', {'input': 'input'}), ] pool.register(With, controls=controls)
def get_performance_suite(): # pragma: no cover ct_tpl = template.Template('{% ct_firstof a b c d %}') dj_tpl = template.Template('{% firstof a b c d %}') ctx = template.Context({'b': 'b'}) return ct_tpl, dj_tpl, ctx register = template.Library() class Firstof(core.Tag): name = 'ct_firstof' options = core.Options( arguments.MultiValueArgument('values'), ) def render_tag(self, context, values): for value in values: if value: return smart_unicode(value) return u'' register.tag('ct_firstof', Firstof) controls = [ ('{% firstof a b c d %}', '{% ct_firstof a b c d %}', {'b':'b'}), ('{% firstof a b c d %}', '{% ct_firstof a b c d %}', {}), ] pool.register(Firstof, controls=controls)
def get_performance_suite(): # pragma: no cover ct_tpl = template.Template('{% ct_now "jS o\f F" %}') dj_tpl = template.Template('{% now "jS o\f F" %}') ctx = template.Context({}) return ct_tpl, dj_tpl, ctx register = template.Library() class Now(core.Tag): name = 'ct_now' options = core.Options( arguments.Argument('format_string'), ) def render_tag(self, context, format_string): from datetime import datetime from django.utils.dateformat import DateFormat df = DateFormat(datetime.now()) return df.format(format_string) register.tag('ct_now', Now) controls = [ ('{% now "jS o\f F" %}', '{% ct_now "jS o\f F" %}', {}), ] pool.register(Now, controls=controls)
arguments.Argument('varname', required=False, resolve=False), ) def render_tag(self, context, values, varname): if self not in context.render_context: context.render_context[self] = itertools_cycle(values) cycle_iter = context.render_context[self] value = cycle_iter.next().resolve(context) if varname: context[varname] = value return value register.tag('ct_cycle', Cycle) controls = [ ("{% for x in sequence %}{% cycle '1' two %}{% endfor %}", "{% for x in sequence %}{% ct_cycle '1' two %}{% endfor %}", { 'two': 2, 'sequence': range(50) }), ("{% for x in sequence %}{% cycle '1' two as varname %}{{ varname }}{% endfor %}", "{% for x in sequence %}{% ct_cycle '1' two as varname %}{{ varname }}{% endfor %}", { 'two': 2, 'sequence': range(50) }), ] pool.register(Cycle, controls=controls)