Re: Passing variables to view

Stephan Semerad <[email protected]> Tue, 6 Jun 2017 14:13:08 -0700 (PDT)
Newsgroups gmane.comp.python.cherrypy
Message-ID <[email protected]>
Hi there. 

Best is you learn a templating tool (mako, jinja). I started rendering html 
in cherrypy at the beginning by doing the following:

import cherrypy

class app():

    @cherrypy.expose
    def index(self):
        number = 20.3
        string = 'hello world!'

        return """
        <!DOCTYPE html>
        <html>
          <head>
            <meta charset="utf-8">
            <title></title>
          </head>
          <body>
            <h1>this is a number: %d</h1>
            <h1>this is a string: %s</h1>
          </body>
        </html>
        """ % (number, string)

cherrypy.quickstart(app(), "/")

*but i do not recommend it for larger applications. *


On Sunday, December 18, 2016 at 10:39:40 PM UTC+1, Germán Rimoldi wrote:
>
> Hi Guys! 
>
> I'm getting started with CherryPy. I'm finding the learning curve pretty 
> steep for a newbie :(
>
> I have a very simple question: How do I pass variables to an html view?
>
> class Root: 
>     @cherrypy.expose
>     def index(self): 
>         c = cherrypy.thread_data.db.cursor() 
>         c.execute('select count(*) from Website') 
>         res = c.fetchone() 
>         c.close() 
>         open("index.html") 
> cherrypy.quickstart(Root())
>
> My index.html, being:
> <html>
>     <head></head>
>     <body>
>     Hello, you have %d records in your table %res[0]  
>     </body>
>  </html>
>
> How should I rewrite my index function, to render the variables to my html 
> file?
>

-- 
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cherrypy-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
To post to this group, send email to cherrypy-users-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
Visit this group at https://groups.google.com/group/cherrypy-users.
For more options, visit https://groups.google.com/d/optout.