Last active
December 14, 2015 09:09
-
-
Save mekarpeles/5063189 to your computer and use it in GitHub Desktop.
Waltz render example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NOTES for DOCS
gn: how would i do this with waltz? http://webpy.org/cookbook/template_import
me: Close
By default, waltz gives you 2 template objects
You can import them by doing:
12:00 AM
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
You can add a keyword argument called "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:
Then within your template... Just use derp
gn: o i drr
me:
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:
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?
or can you just do:
12:12 AM
gn: from random import Word
looks similar to taht
me:
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