Passing parameters to base layout not working

324 views
Skip to first unread message

Dexter

unread,
Oct 28, 2012, 4:36:15 PM10/28/12
to we...@googlegroups.com
Hi

I am currently using  a base template like
render = web.template.render('templates/', base='layout')

I would like to pass some arguments to layout.htm
I know this can be done as (http://webpy.org/cookbook/layout_template)


templates/index.htm
---------------------------------------
$var title: This is title. <h3>Hello, world</h3>

---------------------------------------

and access content.title in the layout page


My problem is I am trying to pass a form Dropdown object to index.htm and want that to be rendered in layout.htm

I am not able to get it right can anyone plz help with the syntax

My code

templates/index.htm
---------------------------------------------
$def with (data)
$var title: Overview

<br />

$var dropdown: $:data.dropdown

---------------------------------------------

templates/layout.htm
---------------------------------------------
$def with (content=None,)

$content.dropdown.render()
---------------------------------------------

And this does not work

Andrey Kuzmin

unread,
Oct 28, 2012, 5:56:30 PM10/28/12
to we...@googlegroups.com
Try this:

$var dropdown = data.dropdown

Dexter

unread,
Oct 29, 2012, 11:22:10 AM10/29/12
to we...@googlegroups.com
Worked like a charm ... so was supposed to use = instead of :

Does : pass a string and = a python object ?

Cheers
PK

Dexter

unread,
Oct 29, 2012, 11:39:59 AM10/29/12
to we...@googlegroups.com
Also, is it possible to send parameter directly to the base layout ?

Andrey Kuzmin

unread,
Oct 29, 2012, 12:14:15 PM10/29/12
to we...@googlegroups.com
Only if you init render without base and access layout directly like this: render.layout(render.page(**page_args), **layout_args)

Another option would be enabling access to web.ctx in templates by passing it in template globals and then set ctx properties in your controller methods.

Dexter

unread,
Oct 31, 2012, 9:15:56 AM10/31/12
to we...@googlegroups.com


On Monday, 29 October 2012 21:44:15 UTC+5:30, Andrey Kuzmin wrote:
Only if you init render without base and access layout directly like this: render.layout(render.page(**page_args), **layout_args)

Another option would be enabling access to web.ctx in templates by passing it in template globals and then set ctx properties in your controller methods.

This second method sounds good ... So is it only globals that the template can have access to ?
Cant I access session parameters ?

like set the option in session.options and access it inside template. I know I can check this instead of asking ... just wanted to know if this is a preferred method of doing thing ?
 

Andrey Kuzmin

unread,
Oct 31, 2012, 11:37:49 AM10/31/12
to we...@googlegroups.com
Yes you can pass session to template globals and use it in templates. 
But everything stored in session will be saved between requests.
If you need it only during particular request then you should store data in web.ctx.

Dexter

unread,
Nov 3, 2012, 1:00:13 AM11/3/12
to we...@googlegroups.com
Thanks ... that helps

Dexter

unread,
Nov 10, 2012, 10:34:43 AM11/10/12
to we...@googlegroups.com
Is it unpythonic or unwepyish if I set a variable I need in base layout in template_globals and pass it like this
        fullrender = web.template.render('templates/',base='menu',globals=template_globals)

Cheers
PK

Andrey Kuzmin

unread,
Nov 10, 2012, 11:34:02 AM11/10/12
to we...@googlegroups.com
Depends on the context of fullrender, if you create in only once in your application, then its ok.
But if you want to do this in every request, then I think it is a bad idea.
Reply all
Reply to author
Forward
0 new messages