My 2c: The ability for a template editor (ie a frontend developer) to
set variables in the global namespace opens up the potential for a
designer to clobber variables delivered to the template from the view.
The example you've provided is something that really falls to the
responsibility of the view to decide what is "the one"

On the other hand this would let frontend guys edit the template in
subtle ways without touching the view. The power to make significant
changes to the logic of the template is generally considered off
limits in django land.

Anyway, someone else my pipe up to correct me here :)

On Fri, Aug 27, 2010 at 10:21 AM, Yo-Yo Ma <baxterstock...@gmail.com> wrote:
> You absolutely cannot. The "with" statement puts the variable in the
> scope of the "with" statement only, not to mention that would not be
> very explicit at all.
>
> On Aug 26, 6:14 pm, "David P. Novakovic" <davidnovako...@gmail.com>
> wrote:
>> You can basically do this with the "with" 
>> statement:http://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=old...
>>
>> On Fri, Aug 27, 2010 at 10:06 AM, Yo-Yo Ma <baxterstock...@gmail.com> wrote:
>> > I'm sure this will be met with criticism, but there is a reason why
>> > just about all template languages allow the setting of variables. It
>> > allows you to do things like:
>>
>> > {% for thing in things %}
>> >    <li>{{ thing }}</li>
>> >    {% if thing.is_the_one %}
>> >        {% set_var the_one thing%}
>> >    {% endif %}
>> > {% endfor %}
>>
>> > <h1>{{ the_one.name }} is a really good thing. <a
>> > href="{{ the_one.get_absolute_url }}">Click here</a> to read more.</
>> > h1>
>>
>> > Instead of having to pass in a variable called "active_one" or
>> > something to that effect from your view. I've provided the
>> > implementation:
>>
>> > class SetVarNode(template.Node):
>> >    def __init__(self, var_name, raw_var_value):
>> >        self.var_name = var_name
>> >        self.variable = template.Variable(raw_var_value)
>>
>> >    def render(self, context):
>> >        context[self.var_name] = self.variable.resolve(context)
>> >        return ''
>>
>> > @register.tag
>> > def set_var(parser, token):
>> >    """
>> >    Example Template:
>>
>> >    {% set_var first_name "Roger" %}
>>
>> >    {{ first_name }}
>>
>> >    Outputs:
>>
>> >    Roger
>> >    """
>> >    try:
>> >        tag_name, var_name, raw_var_value = token.split_contents()
>> >    except ValueError:
>> >        raise template.TemplateSyntaxError(
>> >            '{0} requires 2 arguments.'.format(token.split_contents()
>> > [0])
>> >        )
>> >    return SetVarNode(var_name, raw_var_value)
>>
>> > --
>> > You received this message because you are subscribed to the Google Groups 
>> > "Django developers" group.
>> > To post to this group, send email to django-develop...@googlegroups.com.
>> > To unsubscribe from this group, send email to 
>> > django-developers+unsubscr...@googlegroups.com.
>> > For more options, visit this group 
>> > athttp://groups.google.com/group/django-developers?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Django developers" group.
> To post to this group, send email to django-develop...@googlegroups.com.
> To unsubscribe from this group, send email to 
> django-developers+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/django-developers?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-develop...@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to