Yield regions in layouts in the new version of Iron Router have gotten quite a bit better. So in this video, I'd like to show you how to use yield regions. Let's say that when we click on a particular article, I'd like to be able to show some breadcrumbs up here at the top. But this is really something that the layout should define, because the layout will be for the overall application.
But when I go to a particular blog article, I'd like to fill in the breadcrumbs up here with the article title and maybe some other things. So this is a good use of yield regions. Before we get started, let's go back to the main page here.
And I'm going to add the Bootstrap packet so we get a little bit of styling. I'll type meteor add bootstrap. And then we'll hop over to the HTML for the app. And you can see that the application looks a little bit different now that we've added the Bootstrap library with its associated .less files.
OK, next what I want to do is to go into the layout template that we've been using. And I want to add a special yield region to this layout. And that will let me define an area in the layout that I later want to inject some other template into.
So in this case, what I want to do is create a yield region called breadcrumbs. First I'll create the enclosing HTML inside of the layout. And so I'll create an unordered list with a class of breadcrumb that'll tell Bootstrap to style this properly.
And then inside of there, I'm going to create something called a yield region. So I'll use the yield keyword and then give it a name of breadcrumbs. You can see over in the browser that it created this breadcrumb region at the top. And what I want to be able to do is to render some content inside of this, depending upon the article that I've actually clicked on or if I'm on the home page.
What I can do is go inside of the template that's going to be rendered for this route. So in this case, that'll be the blog template. And I can use a new helper called content for. And we're going to use it with block-like expression using this pound symbol.
And then I name the yield region that the content is actually for. So in this case, I'm going to provide some content for the breadcrumbs region. And we'll close it off like this. And now, whatever content I put inside of here will get injected automatically into the breadcrumbs region.
So for example, if I create a list item and we say home and I press Save, the breadcrumb area over on the right will now have this HTML inside. Then I've gone ahead and replaced that home text with a link to the home page. And so when we're on the home page, if we click Home, it'll take us right back to this home page.
I've clicked on the first blog article. And so far, you can see the breadcrumbs are empty. So what I'm going to do is copy this piece of code here. And we'll go down to the actual article page, and I'll paste this in and save the document.
And now our article page looks just like the home page, with this home page link here. And one thing to notice is that all of this HTML here automatically goes into a special region called the main region. So if you ever hear that vocabulary, you'll know what it means.
It goes into the main area of the page. And then everything else are called named regions. And the name of this particular region that we're putting this content into is called breadcrumbs.
But notice we don't have to use a content for main here. Just the rest of this HTML automatically goes into that main region. OK. So now what we want to do is to update the breadcrumbs for this page so that it actually has the blog article title after the home.
So we can add another list item inside of our content for block. The data context for the template is still good inside of this block. So I can use this expression to grab the title from the data context. And when I press Save, you'll see the title show up in the breadcrumb area.
And let's do one last thing, which is to create a span. And this will give me a nice Bootstrap-style divider. Great. So now we have breadcrumbs. And the breadcrumb changes depending upon the article that we're actually on.
So here we are inside of the blog show route. And what I want to do is call the render method. And as an option, I can provide an option called to and then provide the name of a yield region. So let's say that instead of this, I can render out the article breadcrumbs template and render that into the breadcrumbs yield region.
So if you want to have programmatic access over which templates get rendered into the region, you can do it right in your route function like this. And you can call the render function as many times as you want, rendering different templates into different regions. For example, I might have a footer or any other number of regions.