我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.template.Variable()。
def value_from_settings(parser, token): bits = token.split_contents() if len(bits) < 2: raise TemplateSyntaxError("'%s' takes at least one " \ "argument (settings constant to retrieve)" % bits[0]) settingsvar = bits[1] settingsvar = settingsvar[1:-1] if settingsvar[0] == '"' else settingsvar asvar = None bits = bits[2:] if len(bits) >= 2 and bits[-2] == 'as': asvar = bits[-1] bits = bits[:-2] if len(bits): raise TemplateSyntaxError("'value_from_settings' didn't recognise " \ "the arguments '%s'" % ", ".join(bits)) if settingsvar not in settings.TEMPLATE_ALLOWABLE_SETTINGS_VALUES: raise TemplateSyntaxError("The settings Variable %s is not allowed to be acessed" % settingsvar) return ValueFromSettings(settingsvar, asvar)
def handle_var(value, context): """ Handle template tag variable """ # Resolve FilterExpression and Variable immediately if isinstance(value, FilterExpression) or isinstance(value, Variable): return value.resolve(context) # Return quoted strings unquoted # http://djangosnippets.org/snippets/886 stringval = QUOTED_STRING.search(value) if stringval: return stringval.group('noquotes') # Resolve variable or return string value try: return Variable(value).resolve(context) except VariableDoesNotExist: return value
def render(self, context): try: if '.' in self.field_var: base, field_name = self.field_var.rsplit('.', 1) field = getattr(Variable(base).resolve(context), field_name) else: field = context[self.field_var] except (template.VariableDoesNotExist, KeyError, AttributeError): return settings.TEMPLATE_STRING_IF_INVALID h_attrs = {} for k, v in iteritems(self.html_attrs): try: h_attrs[k] = v.resolve(context) except template.VariableDoesNotExist: h_attrs[k] = settings.TEMPLATE_STRING_IF_INVALID return field(**h_attrs)
def do_form_field(parser, token): """ Render a WTForms form field allowing optional HTML attributes. Invocation looks like this: {% form_field form.username class="big_text" onclick="alert('hello')" %} where form.username is the path to the field value we want. Any number of key="value" arguments are supported. Unquoted values are resolved as template variables. """ parts = token.contents.split(' ', 2) if len(parts) < 2: error_text = '%r tag must have the form field name as the first value, followed by optional key="value" attributes.' raise template.TemplateSyntaxError(error_text % parts[0]) html_attrs = {} if len(parts) == 3: raw_args = list(args_split(parts[2])) if (len(raw_args) % 2) != 0: raise template.TemplateSyntaxError('%r tag received the incorrect number of key=value arguments.' % parts[0]) for x in range(0, len(raw_args), 2): html_attrs[str(raw_args[x])] = Variable(raw_args[x + 1]) return FormFieldNode(parts[1], html_attrs)
def __init__(self, number, key, var_name): # Retrieve the page number. self.page_number_variable = None if number is None: self.page_number = 1 elif number.isdigit(): self.page_number = int(number) else: self.page_number_variable = template.Variable(number) # Get the queystring key. self.querystring_key_variable = None if key is None: self.querystring_key = settings.PAGE_LABEL elif key[0] in ('"', "'") and key[-1] == key[0]: self.querystring_key = key[1:-1] else: self.querystring_key_variable = template.Variable(key) # Get the template variable name. self.var_name = var_name
def style_attribute(attribute_name, package): mappings = { 'title': style_title, 'repo_description': style_repo_description, 'commits_over_52': style_commits, } as_var = template.Variable('package.' + attribute_name) try: value = as_var.resolve({'package': package}) except template.VariableDoesNotExist: value = '' if attribute_name in list(mappings.keys()): return mappings[attribute_name](value) return style_default(value)
def do_form_field(parser, token): """ Render a WTForms form field allowing optional HTML attributes. Invocation looks like this: {% form_field form.username class="big_text" onclick="alert('hello')" %} where form.username is the path to the field value we want. Any number of key="value" arguments are supported. Unquoted values are resolved as template variables. """ parts = token.contents.split(' ', 2) if len(parts) < 2: raise template.TemplateSyntaxError('%r tag must have the form field name as the first value, followed by optional key="value" attributes.' % parts[0]) html_attrs = {} if len(parts) == 3: raw_args = list(args_split(parts[2])) if (len(raw_args) % 2) != 0: raise template.TemplateSyntaxError('%r tag received the incorrect number of key=value arguments.' % parts[0]) for x in range(0, len(raw_args), 2): html_attrs[str(raw_args[x])] = Variable(raw_args[x+1]) return FormFieldNode(parts[1], html_attrs)
def render(self, context): try: template_pack = template.Variable('template_pack').resolve(context) except: template_pack = TEMPLATE_PACK try: form_attrs = template.Variable('form_attrs').resolve(context) except: form_attrs = {} return selectively_remove_spaces_between_tags( self.nodelist.render(context).strip(), template_pack, form_attrs.get('class', ''), )
def parse(cls, parser, token): tokens = token.split_contents() tokens.pop(0) # Pop the tag name. def make_variable(name): return template.Variable(name) def not_using(bit): return bit != "using" template_name_variables = map(make_variable, filter( not_using, takewhile(not_using, tokens))) hint_provider_variables = map(make_variable, filter( not_using, dropwhile(not_using, tokens))) return cls(template_name_variables, hint_provider_variables)