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>
On Fri, Jan 29, 2010 at 12:45 PM, jlist9 <[email protected]> wrote:
> 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!
>
> On Thu, Jan 28, 2010 at 1:42 AM, Pablo Antonio <[email protected]> wrote:
>> 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)
>
> --
> 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.
>
>
--
Angelo Gladding
[email protected]
http://angelo.gladding.name/
--
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.