Building Data-Driven Web Apps with Pyramid and SQLAlchemy Transcripts
Chapter: Introducing the Pyramid framework
Lecture: Building block: Dynamic HTML Templates

Login or purchase this course to watch this video and the rest of the course contents.
0:00 The final building block that we're going to talk about here are templates. Templates are super important. They let us define in an HTML-like fashion
0:11 the general markup for our site and then add little bits of dynamic behavior to it. There's actually three choices we can use
0:19 for the template language. We can use Mako, we can use Jinja2, or we can use Chameleon templates. This is a Chameleon template here,
0:30 and I'll talk later about why I think Chameleon is the right choice. But let's just see how this works.
0:35 It's kind of the same for all the template languages. So what we're going to do is we saw that we passed a model over that had packages, right.
0:44 One of the keys in the model was packages, and the value was actually a list of packages. We're going to use tal:repeat and I'm going to say
0:53 for every package in packages, I want to replicate this HTML block, including the DIV that has the tal:repeat on it.
1:02 So we'll have a DIV containing a hyperlink in a span for every package. And then for the hyperlink we need to set the URL,
1:10 so within this little repeat loop we can say p.url, p.name and so on. And then we're going to show the summary but only if the summary exists.
1:20 Like if there's an empty summary or it's not set, then we're not going to show it. So you can see that if we want to take some sort of text
1:27 or anything and get a string version of it and just plunk it into the page, we'll use dollar curly expression.
1:34 We can do these conditions with tal:condition and so on. Often when we take this and actually pass that data to it and render it,
1:43 that pyramid render it, it's going to look like this: requests, Http For Humans, boto3, AWS API and SQLAlchemy and so on.
1:50 Cause that was the model of the three packaged requests Boto3 and SQLAlchemay passed along to this template. So that's what its going to look like.
1:59 Unstyled and ugly of course, we're going to work on that later.


Talk Python's Mastodon Michael Kennedy's Mastodon