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/

-- 
You received this message because you are subscribed to the Google Groups 
"web.py" 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/webpy?hl=en.

Reply via email to