Site-wide base template

75 views
Skip to first unread message

iltriki

unread,
Jan 27, 2010, 5:28:48 PM1/27/10
to web.py
Hi there..
a simple question about site layout template.(http://tinyurl.com/
ybsz7ww)
I'm using a common layout with header, footer and sidebar and
different views inside of it.
How can i display dynamic content on layout?
Imagine a sidebar with the number of visitors of the site; is it
possible to pass a variable like 'visitor_numbers' to layout without
using global session?

thanks in advance
Michele

Branko Vukelic

unread,
Jan 27, 2010, 5:32:34 PM1/27/10
to we...@googlegroups.com

In a sub-template you say:

$var some_var: value

It becomes a property of the content variable that is passed to the
base layout. Then in your base layout:

$def with (content)

$content.some_var

--
Branko Vukelić

http://foxbunny.tumblr.com/
http://www.flickr.com/photos/16889956@N04/
http://www.twitter.com/foxbunny
http://github.com/foxbunny

Pablo Antonio

unread,
Jan 27, 2010, 6:39:33 PM1/27/10
to we...@googlegroups.com
Branko already answered, and he was very clear, but just in case
you need some example code, take a look at this thread:

Thanks,

--
Pablo A.
http://www.pablo-a.com.ar/

iltriki

unread,
Jan 28, 2010, 3:40:44 AM1/28/10
to web.py
> Branko already answered, and he was very clear, but just in case
> you need some example code, take a look at this thread:http://groups.google.com/group/webpy/browse_thread/thread/93afa0c8eb3...

Thank you guys..
do i need to use $var or can i just use variables that i get from def?

Imagine an index.html view with this:
$def with (visitor_numbers)

can i just use $context.visitor_numbers inside layout.html?

Another thing..
with this scenario, i need to replicate the call to
get_visitor_numbers method inside every classes of my controller, and
then pass the returned value to the views.
Is there a more elegant way in web.py to handle this?
Thanks

Michele

Branko Vukelic

unread,
Jan 28, 2010, 3:59:00 AM1/28/10
to we...@googlegroups.com
On Thu, Jan 28, 2010 at 9:40 AM, iltriki <ilt...@gmail.com> wrote:
>> Branko already answered, and he was very clear, but just in case
>> you need some example code, take a look at this thread:http://groups.google.com/group/webpy/browse_thread/thread/93afa0c8eb3...
>
> Thank you guys..
> do i need to use $var or can i just use variables that i get from def?
>
> Imagine an index.html view with this:
> $def with (visitor_numbers)
>
> can i just use $context.visitor_numbers inside layout.html?

$var is a bit misleading. It doesn't define a new variable, but a
$content attribute. You can also access that attribute from a regular
Python script:

t = render.temapate_that_defines_var()
t.new_attribute

So, if you want to pass something to the base template, you have to use $var.

There is another way:

def render_via_base(content, **kwargs):
render.base_template(content, **kwargs)

Then use it like this:

render_via_base(render.your_template(arg="for your template"),
arg="for the base template")

It's nothing special, just regular Python.

>
> Another thing..
> with this scenario, i need to replicate the call to
> get_visitor_numbers method inside every classes of my controller, and
> then pass the returned value to the views.
> Is there a more elegant way in web.py to handle this?
> Thanks
>
> Michele
>

> --
> You received this message because you are subscribed to the Google Groups "web.py" group.
> To post to this group, send email to we...@googlegroups.com.
> To unsubscribe from this group, send email to webpy+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/webpy?hl=en.

Pablo Antonio

unread,
Jan 28, 2010, 4:42:06 AM1/28/10
to we...@googlegroups.com
On Thu, Jan 28, 2010 at 12:40:44AM -0800, iltriki wrote:
> > Branko already answered, and he was very clear, but just in case
> > you need some example code, take a look at this thread:http://groups.google.com/group/webpy/browse_thread/thread/93afa0c8eb3...
>
> Thank you guys..
> do i need to use $var or can i just use variables that i get from def?
>
> Imagine an index.html view with this:
> $def with (visitor_numbers)
>
> can i just use $context.visitor_numbers inside layout.html?

Try it. :P

No, I think you can't. Someone correct me if I'm wrong.

$content.visitor_numbers asks for an attribute in content. Think of
templates as python functions (that's what they become after all). Your
index function receives a visitor_numbers argument, but does it have
an attribute called visitor_numbers? The way to set an attribute to a
template is by using $var.

>
> Another thing..
> with this scenario, i need to replicate the call to
> get_visitor_numbers method inside every classes of my controller, and
> then pass the returned value to the views.
> Is there a more elegant way in web.py to handle this?

You can avoid doing that.

You could do something like the following code. Be aware that I didn't
test it, and it looks like too much magic :) Here it is:

-- layout.html
$def with (content, visitor_numbers)
<p>Numbers: $visitor_numbers</p>
<p>$:content</p>

-- pages.py

import web

class RenderInContext:

def __init__(self, dir = 'templates/', base, **base_args):
self.render = web.template.render(dir)
self.base = base
self.base_args = base_args

def __getattr__(self, tpl):

def f(**tpl_args):
content = getattr(self.render, tpl)(**tpl_args)
return getattr(self.render, self.base)(content, **base_args)

return f

render_in_context = RenderInContext('templates/', base = 'layout',
visitor_numbers = '20')

render_in_context.index(foo = bar)

> Thanks

No problemo :)

Thanks,

--
Pablo Antonio (AKA crazy2k)
http://www.pablo-a.com.ar/

Greg Milby

unread,
Jan 28, 2010, 7:03:26 AM1/28/10
to we...@googlegroups.com
then pass it in as a global from teh code.py (or whatever you named it). 

i use my db connection this way so i can call it from any class and use the results on any page.

-----------------------
Visit My Sites!
www.syrbot.com | My Site
www.attackr.com | Latest Blog Posts



jlist9

unread,
Jan 29, 2010, 3:45:57 PM1/29/10
to we...@googlegroups.com
I actually tried your code and it worked well after I fixed a small issue:

return getattr(self.render, self.base)(content, **base_args)
-->
return getattr(self.render, self.base)(content, **self.base_args)

Thanks!

Angelo Gladding

unread,
Jan 29, 2010, 4:23:00 PM1/29/10
to we...@googlegroups.com
This should work as well — as simply as possible — using globals all
around. Don't add any more complexity than the statements `visitors +=
and -= 1` or your run the risk of no longer being thread-safe. But if
this is all you need..

## code.py

visitors = 0

render = Render('templates', base='base', globals={'visitors': visitors})

class Landing:
def GET(self):
return render.landing()

class SignIn:
def POST(self):
visitors += 1
return 'one more user eating my cycles'

class SignOut:
def POST(self):
visitors -= 1
return 'one less user eating my cycles'

##templates/base.html

$def with (document)
<html><body>
<article>$:document</article>
<footer>Currently $visitors visitors.</footer>
</body></html>

## templates/landing.html

$def with ()
<p>Welcome to the landing of my site.</p>

## output

<html><body>
<article><p>Welcome to the landing of my site.</p></article>
<footer>Currently 0 visitors.</footer>
</body></html>

> --
> You received this message because you are subscribed to the Google Groups "web.py" group.
> To post to this group, send email to we...@googlegroups.com.
> To unsubscribe from this group, send email to webpy+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/webpy?hl=en.
>
>

--
Angelo Gladding
ang...@gladding.name
http://angelo.gladding.name/

MLTrim

unread,
Jan 31, 2010, 11:53:14 AM1/31/10
to web.py
Thanks to everyone that have replied to this post.
I think i will go with Pablo Antonio solution.

Another solution is to use ajax from layout.html and get values from
exposed apis that talk in Json.
I had adopted this way before posting this question :).

Michele

iltriki

unread,
Feb 6, 2010, 4:01:42 AM2/6/10
to web.py
I have deployed my simple pet project yesterday, based on web.py.
I simply loved to work with this framework, very pythonic and flow
oriented :); this week i will introduce Pablo Antonio solution to
properly handle dynamic context on layout.html.
Check it out: http://tinyurl.com/yfk6nau.

thanks
Michele

Reply all
Reply to author
Forward
0 new messages