RESTful and HTTP APIs in Pyramid Transcripts
Chapter: Web application building blocks
Lecture: Pyramid building block: Templates

Login or purchase this course to watch this video and the rest of the course contents.
0:01 The final building block that I want a touch on just a little bit but we won't focus very much if at all on it in this course, are templates.
0:09 So, we've seen that we can have renderers like json, and that will turn our response in from say a Python dictionary into a json object
0:19 and that's great for services, but if we want to have some kind of documentation page
0:24 that we want to have maybe the service embedded inside of our website and our web also has sort of HTML type features,
0:31 then we will use one of these templates, and Pyramid supports a number of languages, this one happens to be Chamelon, I think Chameleon is
0:38 by far the cleanest, simplest, most HTML like and HTML friendly of all the various temple languages,
0:46 but you could configure it to pick whichever one you want, and the idea is we're going to take one of those models and we're going to pass it here,
0:53 and we saw before we were sending off to the xml, some kind of xml renderer this model that has an albums list in it and within each item in that list,
1:04 we had an album with a url title and a has preview. So if we pass that to this template,
1:10 we would be able to generate a report or display of them like this, so maybe we want to have like a real simple HTML page,
1:17 it says here's the contents, and it says what we want to do is for each album I want to have a div that contains two URLs,
1:25 right so we can use tal:repeat, we can say we're going to repeat basically do a for in loop for every a to find in variable a in albums,
1:34 we are going to generate a copy of this div and then within there, we're going to crab out the url and the title
1:42 you do that with dollar curly value and put whatever Python expression you want in there and then close that off,
1:48 and you can also have conditional, so not all of the albums have previews and we don't want to show the preview link if there's no preview available,
1:55 so we'll say only show that second link tal:condition if a.has preview. Alright so expressions are written with curly parentheses expression
2:05 the conditionals are done with tal:condition. Now, if we were to actually execute this, we want to send that model over to it,
2:13 obviously it's unstyled, there's no css here, so we come up looking pretty dull, but you would get something basically like this,
2:20 alright buy digital age kids, that one has a preview, buy freedom songs that one has a preview,
2:26 buy make money- that one has no preview, so it doesn't show up.

Talk Python's Mastodon Michael Kennedy's Mastodon