class ArgumentType(TestTag): age = ttag.IntegerArg(required=False, named=True) name_ = ttag.StringArg(required=False, named=True) url = ttag.ModelInstanceArg(model=models.Link, required=False, named=True) date = ttag.DateArg(required=False, named=True) time = ttag.TimeArg(required=False, named=True) datetime = ttag.DateTimeArg(required=False, named=True) flag = ttag.BooleanArg() def output(self, data): order = 'name age url date time datetime'.split() values = [six.text_type(data[x]) for x in order if x in data] if 'flag' in data: values.append('flag_is_set') return u' '.join(values)
class Placeholder(ttag.Tag): """ Outputs an ``<img>`` tag containing an inline base64 encoded placeholder image. For example:: {% placeholder 100x30 "some text" %} The text is optional. Some optional keyword arguments are available which can be used to adjust the output image: ``background`` The background color of the placeholder image. ``border`` The border color (and also that of the 'x' lines crossing the image). ``text_color`` The color of the text. ``text_size`` The size of the text. ``class`` The HTML class to use for this image tag. ``no_antialias`` Don't antialias the final image. """ size = SizeArg() text = ttag.Arg(required=False) background = ttag.Arg(keyword=True, required=False) border = ttag.Arg(keyword=True, required=False) text_color = ttag.Arg(keyword=True, required=False) text_size = ttag.IntegerArg(keyword=True, required=False) class_ = ttag.Arg(keyword=True, required=False) no_antialias = ttag.BooleanArg() def clean(self, cleaned_data): cleaned_data['antialias'] = not cleaned_data.pop('no_antialias', None) return cleaned_data def output(self, data): img = StringIO() html_class = data.pop('class', None) create_placeholder(**data).save(img, 'PNG') img.seek(0) return mark_safe('<img alt="%s" src="data:image/png;base64,%s" %s/>' % (data.get('text') or '', img.read().encode('base64'), html_class and ' class="%s"' % html_class or ''))
class Render(AsTag): """ A block tag that renders its contents to a context variable. Here is an example of using it with a ``blocktrans`` tag:: {% render as name %} <a href="{{ profile.get_absolute_url }}">{{ profile }}</a> {% endrender %} {% blocktrans %}Logged in as {{ name }}{% endblocktrans %} Here is an example of a simple base template that leverages this tag to avoid duplication of a page title:: {% render as title %} {% block title %}The page title{% endblock %} {% endrender %} <html> <head><title>{{ title }}</title></head> <body> <h1>{{ title }}</h1> {% block body %}{% endblock %} </body> By default, the tag strips the output of leading and trailing white space. To avoid this, use the ``no_strip`` argument:: {% render no_strip as target %} ... {% endrender %} """ no_strip = ttag.BooleanArg() class Meta: block = True def as_value(self, data, context): output = self.nodelist.render(context) if 'no_strip' not in data: output = output.strip() return mark_safe(output)
class BaseFormTag(FormOptionsTag, ConfigMixin): with_ = ttag.KeywordsArg(required=False, named=True) only = ttag.BooleanArg() using = TemplateArg(required=False, named=True) extends = TemplateArg(required=False, named=True) def __init__(self, parser, *args, **kwargs): super(BaseFormTag, self).__init__(parser, *args, **kwargs) if 'using' in self._vars and 'extends' in self._vars: raise template.TemplateSyntaxError("Can't provide both 'using' " "and 'extends'.") using_inline = 'using' in self._vars and not self._vars['using'] if using_inline or 'extends' in self._vars: nodelist = parser.parse([self._meta.end_block]) parser.delete_first_token() if using_inline: self._vars['using'] = template.Template('') self._vars['using'].nodelist = nodelist else: self.blocks = dict([(n.name, n) for n in nodelist.get_nodes_by_type(BlockNode)]) def clean(self, data, context): data = super(BaseFormTag, self).clean(data, context) form_template = data.get('using') or data.get('extends') if not form_template: if not self._meta.default_template: return data form_template = self.get_config('%s_template' % self._meta.name, data, context) or self._meta.default_template if isinstance(form_template, basestring): form_template = get_template(form_template) data['template'] = form_template return data def get_extra_context(self, data): return {} def get_block_context(self, form_template, blocks): block_context = BlockContext() # Add the block nodes from this node to the block context. block_context.add_blocks(blocks) # Add the template's nodes too if it is the root template. for node in form_template.nodelist: # The ExtendsNode has to be the first non-text node. if not isinstance(node, template.TextNode): if not isinstance(node, ExtendsNode): blocks = dict([(n.name, n) for n in form_template.nodelist\ .get_nodes_by_type(BlockNode)]) block_context.add_blocks(blocks) break return block_context def render(self, context): data = self.resolve(context) if 'only' in data or self.get_config('only', data, context): sub_context = template.Context() for key, value in context.keys: if key in ('form', CONFIG_KEY): sub_context[key] = value else: sub_context = context extra_context = self.get_extra_context(data) extra_context.update(self.get_config('with', data, context) or {}) if 'with' in data: extra_context.update(data['with']) # Update (i.e. push) context in preparation for rendering. sub_context.update(extra_context) context.render_context.push() blocks = getattr(self, 'blocks', self.get_config('extends_blocks', data, context)) if blocks: context.render_context[BLOCK_CONTEXT_KEY] = \ self.get_block_context(data['template'], blocks) output = data['template']._render(sub_context) # Clean up context changes. context.render_context.pop() sub_context.pop() return output