Template context processors

81 views
Skip to first unread message

Fredrik de Vibe

unread,
Jan 16, 2013, 9:18:53 PM1/16/13
to chica...@googlegroups.com
Django has a feature called template context processors (see
https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATE_CONTEXT_PROCESSORS),
that provides the possibility of adding template variables on a global
scale. This allows for, for instance, providing the request variable
to all templates without having to supply it in all controllers.

In https://github.com/evanmiller/ChicagoBoss/pull/180, a similar
approach is proposed for CB, but after some debate, the preferred
solution is to provide this feature using controller callbacks akin to
'before_'. Using the name context_ is one suggestion, what about
render_vars_?

To provide equivalent functionality to Django's template context
processors, a way of avoiding duplication across controllers would be
preferable, and in the discussion in the pull request, inheritance is
suggested. I believe this could be a good way to go.

So, does anybody have opinions on this? Use-cases or other issues?

One issue that comes to mind is that if inheritance were to solve the
duplication problem for this _and_ the 'before_' callback, afaict,
erlang does not support multiple inheritance (extending multiple
modules). This means that if one wants to inherit this functionality
as well as 'before_', one would need to put them both in the same
module. Now, controllers as compiled by CB's own compiler, so there
might be more flexibility here than I'm aware of. Opinions?


--
- Fredrik

Fredrik de Vibe

unread,
Jan 17, 2013, 8:41:39 PM1/17/13
to chica...@googlegroups.com
Fredrik de Vibe <fdv+...@ifi.uio.no>
writes:

> Django has a feature called template context processors (see
> https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATE_CONTEXT_PROCESSORS),
> that provides the possibility of adding template variables on a global
> scale. This allows for, for instance, providing the request variable
> to all templates without having to supply it in all controllers.
>
> In https://github.com/evanmiller/ChicagoBoss/pull/180, a similar
> approach is proposed for CB, but after some debate, the preferred
> solution is to provide this feature using controller callbacks akin to
> 'before_'. Using the name context_ is one suggestion, what about
> render_vars_?

What about this approach?

https://github.com/fdevibe/ChicagoBoss/commit/52c764da9e0786697b5e8ca00acfcf9882ae705a


--
- Fredrik

Evan Miller

unread,
Jan 19, 2013, 7:46:25 PM1/19/13
to ChicagoBoss
I'd love to hear others' opinions on this... the basic idea is to make some variables globally available so you don't have to do stuff like

foobar('GET', []) ->
   UserVars = my_lib:get_user_vars(Req),
   OtherVars = ...,
   {ok, UserVars ++ OtherVars}.

Instead you'd do

inject_vars_(_) ->
   my_lib:get_user_vars(Req).

foobar('GET', []) ->
   OtherVars = ...,
   {ok, OtherVars}.

And the combined variable list would be available in the template.




--
- Fredrik

--





--
Evan Miller
http://www.evanmiller.org/

Fredrik de Vibe

unread,
Jan 19, 2013, 8:47:41 PM1/19/13
to chica...@googlegroups.com
Fredrik de Vibe <fdv+...@ifi.uio.no>
writes:

[ --8<-- ]
...which was seriously wrong. This one should hopefully be
correct-ish:

https://github.com/fdevibe/ChicagoBoss/commit/2b5c22aedf41c4efaa8c55accaa26cad0879e1a1

FWIW, I've run some preliminary tests, and I can't see any noticeable
performance penalty, at least when it comes to the time it takes to
process requests.


- Fredrik

Evan Miller

unread,
Jan 21, 2013, 6:52:47 PM1/21/13
to ChicagoBoss
I like this. The function might need some more context though, such as

inject_vars_(Action, Method, Tokens, AuthInfo)

That way you could provide log-in information from the before_ filter and perform more complex pattern-matching.

What do others think?

Evan
 


- Fredrik

--


Peter Yuen

unread,
Mar 10, 2014, 11:55:42 AM3/10/14
to chica...@googlegroups.com
so is this function implemented? if so how can we use it ???

Fredrik de Vibe

unread,
Mar 10, 2014, 2:55:07 PM3/10/14
to chica...@googlegroups.com
Peter Yuen <requi...@msn.com> writes:

> so is this function implemented? if so how can we use it ???

Hm, I had forgotten about this approach. Recently, I was asked to clean
up the original pull request (which was not per controller, but global),
and in doing so, I changed it quite a bit. See
https://groups.google.com/forum/#!topic/chicagoboss/c7l-8cJiPN0

So if you have opinions about either approach, post them here or in the
other thread, and depending on the community's (and Jesse's) opinions,
I'd be happy to make a pull request of modify it either way.


- Fredrik

> On Tuesday, January 22, 2013 7:52:47 AM UTC+8, Evan Miller wrote:
>>
>> On Sat, Jan 19, 2013 at 7:47 PM, Fredrik de Vibe <fdv+...@ifi.uio.no<javascript:>
>> > wrote:
>>
>>> Fredrik de Vibe <fdv+...@ifi.uio.no <javascript:>>
Reply all
Reply to author
Forward
0 new messages