Building data-driven web apps with Pyramid and SQLAlchemy Transcripts
Chapter: Introducing the Pyramid framework
Lecture: Building block: Dynamic HTML Templates
0:00 The final building block
0:01 that we're going to talk about here are templates.
0:03 Templates are super important.
0:05 They let us define in an HTML-like fashion
0:10 the general markup for our site
0:12 and then add little bits of
0:14 dynamic behavior to it.
0:16 There's actually three choices we can use
0:18 for the template language.
0:20 We can use Mako,
0:22 we can use Jinja2,
0:23 or we can use Chameleon templates.
0:26 This is a Chameleon template here,
0:29 and I'll talk later about why
0:31 I think Chameleon is the right choice.
0:32 But let's just see how this works.
0:34 It's kind of the same for all the template languages.
0:37 So what we're going to do is we saw that we passed
0:39 a model over that had packages, right.
0:43 One of the keys in the model was packages,
0:45 and the value was actually a list of packages.
0:50 We're going to use tal:repeat and I'm going to say
0:52 for every package in packages,
0:55 I want to replicate this HTML block,
0:58 including the DIV that has the tal:repeat on it.
1:01 So we'll have a DIV containing a hyperlink
1:04 in a span for every package.
1:06 And then for the hyperlink
1:07 we need to set the URL,
1:09 so within this little repeat loop
1:10 we can say p.url,
1:12 p.name and so on.
1:14 And then we're going to show the summary
1:17 but only if the summary exists.
1:19 Like if there's an empty summary or it's not set,
1:21 then we're not going to show it.
1:22 So you can see that if we want to take some sort of text
1:26 or anything and get a string version of it
1:28 and just plunk it into the page,
1:30 we'll use dollar curly expression.
1:33 We can do these conditions with tal:condition
1:36 and so on.
1:37 Often when we take this
1:39 and actually pass that data to it and render it,
1:42 that pyramid render it,
1:43 it's going to look like this:
1:44 requests, Http For Humans, boto3, AWS API
1:48 and SQLAlchemy and so on.
1:49 Cause that was the model of the three packaged requests
1:53 Boto3 and SQLAlchemay passed along to this template.
1:56 So that's what its going to look like.
1:58 Unstyled and ugly of course,
2:00 we're going to work on that later.