Python for Entrepreneurs Transcripts
Chapter: Build web apps with Pyramid: Part 1
Lecture: Building Block: Templates

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Here is the template and this template language is called Chameleon.
0:05 It's really clean and nice and it uses what's called template attribute language, so TAL.
0:11 And often whenever you see the code that has to do with this template language you'll see "tal:" in the attributes,
0:18 the other way that it works with code is you say "$" and put on curly braces and within there you can write basically arbitrary Python code,
0:26 arbitrary Python expressions at least. So what we want to do is we want to take some albums,
0:32 here we passed a dictionary or a model that has a key called "albums",
0:36 and that album is a list, so we want to loop over it, so we say tal:repeat="a albums".
0:42 I like to say something like "a in albums" or something like that, but it's just a albums, no "in".
0:47 So what it's going to do is this div that has the tal:repeat= is going to be repeated
0:52 for as many times as there are albums and each time through a will have a different value.
0:56 Then we want to go and say "a is one of these individual albums, let's say that URL for buying it is going to be a.url" and the title,
1:04 the text we want for the link is "buy", whatever the title is, so "a.title".
1:09 So values that pull out the text or look at the string representation of data passed in,
1:14 will be passed within, there is "$" expression and like I said, these can be methods,
1:19 these can just be values and so on, we can also do tal conditional, which is really nice, this is our "if" statement in the template language,
1:27 so we only want to show "preview music" if there is some kind of preview available, remember, some of our albums have previews, some of them didn't.
1:35 So, here we can say tal:condition and it will completely leave this segment, or this node out of the URL, if has_preview is falsie.
1:45 Now if we run this and we give it a set of albums, we can come out looking something like this, assuming we've given it basically zero style.
1:53 But that gives you a sense of how this template will render, what we give it.


Talk Python's Mastodon Michael Kennedy's Mastodon