Hey Armin, thanks for the quick reply!

On Mon, Mar 21, 2011 at 5:27 AM, Armin Ronacher <[email protected]
> wrote:

> Hi,
>
> On 2011-03-21 5:16 AM, Joshua Bronson wrote:
>
>> I noticed jinja2/defaults.py sets "dict" to "lambda **kw: kw" in its
>> DEFAULT_NAMESPACE. Is there a reason why it's not just set to dict?
>>
> dict() behaves differently from Python version to Python version and some
> objects expose some of their inner workings to that function and break the
> sandbox if enabled.


I suspected there was some good reason. Now I'm curious how you could use
the built-in dict to break something. :)

Also the use case of an expanded dict is limited, one better passes
> different functions to the environment that do not need hackery.
>
>  I'd like to do {{ url_for(request.endpoint, **dict(request.view_args,
>> foo=bar) }} in a template (overriding any "foo" value in the current
>> page's view_args with "bar"), but I can't with the phony dict function.
>>
> You are free to override that function with the builtin dict type. However
> for that particular use case I recommend something like this:
>
> def modified_url_for(**updates):
>    args = request.view_args.copy()
>    args.update(updates)
>    return url_for(request.endpoint, **args)
> app.jinja_env.globals['modified_url_for'] = modified_url_for
>
> And the in the template:
>
>    <a href="{{ modified_url_for(page=2) }}">Page 2</a>
>

Thanks for the recommendation. Using a modified_url_for function is totally
reasonable. I'd still use the more succinct **dict(request.view_args,
**updates) from inside that function (where we're not sandboxed, and which
works in all Python versions I'm targeting), unless there's some other
reason not to.

Pocoo love!

-Josh

-- 
You received this message because you are subscribed to the Google Groups 
"pocoo-libs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pocoo-libs?hl=en.

Reply via email to