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
0:04 but we won't focus very much if at all on it in this course, are templates.
0:08 So, we've seen that we can have renderers like json,
0:11 and that will turn our response in from say a Python dictionary into a json object
0:18 and that's great for services,
0:20 but if we want to have some kind of documentation page
0:23 that we want to have maybe the service embedded inside of our website
0:27 and our web also has sort of html type features,
0:30 then we will use one of these templates,
0:32 and Pyramid supports a number of languages,
0:35 this one happens to be Chamelon, I think Chameleon is
0:37 by far the cleanest, simplest, most html like and html friendly
0:42 of all the various temple languages,
0:45 but you could configure it to pick whichever one you want,
0:47 and the idea is we're going to take one of those models
0:50 and we're going to pass it here,
0:52 and we saw before we were sending off to the xml,
0:54 some kind of xml renderer this model that has an albums list in it
1:00 and within each item in that list,
1:03 we had an album with a url title and a has preview.
1:06 So if we pass that to this template,
1:09 we would be able to generate a report or display of them like this,
1:13 so maybe we want to have like a real simple html page,
1:16 it says here's the contents, and it says what we want to do is
1:19 for each album I want to have a div that contains two urls,
1:24 right so we can use tal:repeat, we can say we're going to repeat
1:28 basically do a for in loop for every a to find in variable a in albums,
1:33 we are going to generate a copy of this div
1:36 and then within there, we're going to crab out the url and the title
1:41 you do that with dollar curly value and put
1:44 whatever python expression you want in there and then close that off,
1:47 and you can also have conditional, so not all of the albums have previews
1:50 and we don't want to show the preview link if there's no preview available,
1:54 so we'll say only show that second link tal:condition if a.has preview.
1:59 Alright so expressions are written with curly parentheses expression
2:04 the conditionals are done with tal:condition.
2:07 Now, if we were to actually execute this,
2:10 we want to send that model over to it,
2:12 obviously it's unstyled, there's no css here,
2:14 so we come up looking pretty dull,
2:16 but you would get something basically like this,
2:19 alright buy digital age kids, that one has a preview,
2:22 buy freedom songs that one has a preview,
2:25 buy make money- that one has no preview, so it doesn't show up.