Python for Entrepreneurs Transcripts
Chapter: Build web apps with Pyramid: Applied web development
Lecture: Concept: Shared layout

Login or purchase this course to watch this video and the rest of the course contents.
0:02 Let's review the three major steps you take to create a shared layout, It starts by taking a template and converting it to a macro
0:13 that will be the shared layout that defines the overall look and feel of the website. Next, we are going to capture this macro in the controller
0:20 and we are going to make it available to all of the views, the other dependent page templates
0:25 Finally, on most page templates we are going to use fill slot and stuff that individual unique data that makes up that particular page
0:32 into those slots and let the rest of it be filled by the overall macro. So step one, we begin by taking just the standard Chameleon template
0:40 created by the starter project, and we converted it to a macro, here you can see we are saying metal:define-macro and we give it a name,
0:47 in this case I wanted to call it layout. Notice we are applying that to the HTML tag so the entire page,
0:52 and then we define several slots that the dependent pages can use to stick in and populate the page,
0:59 here I've highlighted the main content where all the data goes but because of these slots are optional,
1:06 it's fine to put additional places of which basically adds additional flexibility
1:10 for your dependent views, in this case we made a place at the end of the layout so that we could add additional JavaScript files in the right location
1:18 if the dependent page needed to. Step two, in the dependent pages, like here home/, we are going to consume it,
1:27 so all you put in this page basically is use macro and within the use macro, we are going to fill the individual slots, so we are going to say
1:36 we are going to fill the content and whatever the page contents are, we put that right there, and we are also going to in this case be using
1:43 an additional JavaScript file that is not included into other pages, so we are going to include it, here we are going to use AngularJS on this one page
1:51 so we are going to include that script file along with maybe some others that aren't shown on the screen.
1:56 Now remember, to wire those together, we have to capture that somewhere
1:59 and then make it available, because we are using this idea of a controller based,
2:03 it's super easy to put it in this one place and it becomes available to our entire website. So that takes basically three steps:
2:09 One, we are going to get the renderer from the, now remember, we have to use the "web application name:the path",
2:18 that's basically the package name in Python, then we are going to get the implementation,
2:23 and we are going to go to the implementation say I'd like to get the macro called layout,
2:27 that's what we called it, and we are just going to store that on a local variable called self.layout, that makes it available to the dependent views
2:33 by using macro view.layout. Now reasonable question is that seems like a lot of work, you have to juggle those things, and whatnot,
2:42 is there just a packages you can include that does this magic? Well, yes, there actually is a package you can include called Pyramid Layout.
2:48 But if you go back and look what we did, there is literally nine lines of code that weren't just already boilerplate HTML
2:55 and if you go read through here, it's not particularly simple, I mean, what they are doing is great, but I feel like it's just overkill
3:03 and there is too much going on here, so you can pick whichever makes you happy,
3:06 we are going to use this simple macro-driven mechanism that we've adopted here but if you want to use this package, you can as well.

Talk Python's Mastodon Michael Kennedy's Mastodon