thanks in advance
Michele
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
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
$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.
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/
return getattr(self.render, self.base)(content, **base_args)
-->
return getattr(self.render, self.base)(content, **self.base_args)
Thanks!
## 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/
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
thanks
Michele