Skip to content

Instantly share code, notes, and snippets.

@mekarpeles
Last active December 14, 2015 09:09
Show Gist options
  • Save mekarpeles/5063189 to your computer and use it in GitHub Desktop.
Save mekarpeles/5063189 to your computer and use it in GitHub Desktop.
Waltz render example
from waltz import render, setup
import derp
urls = ('/?', 'Index')
env = {"derp": derp}
app = setup.dancefloor(urls, globals(), env=env)
class Index:
def GET(self):
"""assume you have a file templates/base.html which
is your layout or template for your website and
a content file templates/index.html. This will
render templates/index.html and place it within
templates/base.html
"""
return render().index() # you can pass page specific vars into index(--> here <--)
if __name__ == "__main__":
app.run()
@mekarpeles
Copy link
Author

NOTES for DOCS

gn: how would i do this with waltz? http://webpy.org/cookbook/template_import

render = waltz.template.render('templates', globals={'Player':Player})

me: Close

By default, waltz gives you 2 template objects

You can import them by doing:
12:00 AM

from waltz import render, slender

By default, render will take a file within the templates/ directory and inject it within a file called templates/base.html

So base.html is like a template

gn: i want to add a global to templates basically

me: Yup

When you create the application
12:01 AM

app = waltz.setup.dancefloor(urls, globals(), ...)

You can add a keyword argument called "env"

env = {"add2": lamdba x: x+2}
app = waltz.setup.dancefloor(urls, globals(), env=env)

12:02 AM
All the "keys" within env are accessible from within any template rendered by render and slender

gn: say i have import Derp, how can i make Derp a global to templates?
12:03 AM

me:

env = {"derp": Derp}
app = waltz.setup.dancefloor(urls, globals(), env=env)

Then within your template... Just use derp

gn: o i drr

me:

$derp

gn: isee

i have to have $derp at the top? lol

me: Nope
12:04 AM
Automatically imported

gn: k

me: The top -- i.e. $def with ()

is only for vars you're passing in to a specific template

So you don't actually have to type:

render = web.template.render('templates', globals={'stat':status})

gn: ah i see
12:05 AM
me: You get that render object for free from waltz

gn: i see now

me: Here's an example
12:07 AM
gn: k
12:09 AM
me: https://gist.github.com/mekarpeles/5063189

There's a full example
12:10 AM
gn: i see

me: The template (index.html) will have access to derp as well as anything else in 'env'
12:11 AM
gn: ok so let's say derp has a function that gives the number of letters in a word

me: So Derp is a class?

gn: yea

me: Does it need to be instantiated before you can call its function?

x = Derp()
x.whatever()

or can you just do:

Derp.whatever()

12:12 AM
gn: from random import Word

# Only get the stats:
p = Word("gavin")
print(p.length)

looks similar to taht

me:

env = {"derp": lambda x: Derp(x).length()}

12:13 AM
I'd do that.

derp is a function which takes a parameter x.

Given a parameter x, it constructs a class from Derp and calls the length function

If the lambda seems scary, you can re-writ ite like this...
12:14 AM

def length(x):
    return Derp(x).length()

env = {"derp": length}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment