On Wed, Sep 18, 2013 at 8:43 PM, Faustino Aguilar Quirós < [email protected]> wrote:
> Hello web.py community: > > My problem is in the layout template: > > According to the cookbook <http://webpy.org/cookbook/layout_template> this > should > work: > > *app.py (In "app.py" no problem)* > > import web > > app = web.auto_application() > > render = web.template.render('templates/', base='layout', globals={}) > > class example1(app.page): > > def GET(self): > return render.example1() > > class example2(app.page): > > def GET(self): > return render.example2() > > if __name__ == '__main__': > app.run() > > ** > *layout.html* > > $def with (content) > > <html> > <head> > *$if content.title:* > <title>$content.title</title> > </head> > > <body> > > $:content > > </body> > > </html> > > *example1.html* > > > $var title: This is title. > <h3>Hello, world</h3> > > > *example2.html* > ** > > <h1>hello, world.</h1> > > > And relatively works because *"example1.html"* is rendered successfully, > but *"**example2.html**"* did not. > > When rendering *"example2.html**"* shows an error saying that the variable > *"title"* does not exist, but it should work, because I specify in * > "layout.html"* that *"**$if content.title"* > ** > Then looking at the methods available for the object *"content"* found it > *"content.keys ()"*, then fix my "*layout.html"* code: > > > > *layout.html* > > $def with (content) > > <html> > <head> > *$if 'title' in content.keys():* > <title>$content.title</title> > </head> > > <body> > > $:content > > </body> > > </html> > > And it works well! > > I propose this solution to not having to be declaring the same variable in all > templates, but can correct me if they have something better > You can also do: $if "title" in content: .. Basically the response of a template is like a dictionary. You can do everything that you can do with a dictionary. Anand -- You received this message because you are subscribed to the Google Groups "web.py" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/webpy. For more options, visit https://groups.google.com/groups/opt_out.
