-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to pass data to layout? #4
Comments
@yuomtheara I guess, you should make template helpers and pass data from there |
Check here for a sample usage: https://github.com/banglashi/flow-router-regions-update/blob/master/regions-update.coffee |
I checked example above, but don't understand.
Pl example. |
Same problem. My current code is below:
If I use FlowLayout, where should I pass DATA object? Thanks a lot. |
Inside your see: <template name="foo_editor">
Id of the document: {{data._id}}
</template> |
Dear arunoda, Thanks your reply. I still make no sense. My old render methods:
Now change to FlowLayout:
I don't know how to pass 'var item...' object to the render method. Or, perhaps, I do this job incorrectly? Thanks. |
Okay. Now I got it. With flow, we don't provide easy ways for that. That's because it's wrong. On Sat, Mar 28, 2015 at 7:47 PM anyinfa [email protected] wrote:
|
I think I do this thing wrong way. |
@anyinfa
|
Thanks. This fixed my problem! |
Oh we can't pass data from the routee like |
@arunoda, I agree that the router should just be the router. But What if I wish to render different Template when clicking on a button, for example. Template.myTemplate.events({
'click .button': function(event, instance) {
FlowLayout.render('details', 'main', { itemId: this.itemId });
}
});
Template.details.onCreated(function() {
var id = this.data.itemId;
}); I wish to be able to render a different template, inside a region in the current layout. |
Of course, you can use it however you like. But, if you using it inside flow-router, you should not(can't) use any collection data. Just pass params and fetch data from the template layer and render them. |
But if i'm not mistaken, that currently isn't possible with |
I agree with @smeijer thanks! |
I'm still confused. Does that mean every page need to have a different template? That seems quite a lot. For example, I have a "header" template. The structure of the header section in all pages are pretty much the same, but different "buttons" are put to different pages. The exact button in each page can be derived from the route alone. I'd like to be able to have a common template for all headers, and then pass an array to specify the buttons to render based on the current page the user is in. How to achieve that with flow router? Do I need to create a separate template for the header of each page? This seems a lot of work. Creating an array based on route is much simpler than creating separate templates. Or do I still have a common template, but in the template helpers, conditionalize the "buttons" based on the route? It makes the rendering route aware. The code is not modularized as well. Any other solution? |
@sf-wind I'm trying to achieve the same kind of modularization. If we were able to pass arbitrary objects as a single parameter to the template, it would solve this problem. So what do you do to solve it? In my case, I have multiple navigations, and I do something like: // Route A
BlazeLayout.render('layout', {main: 'mainForRouteA', nav: 'navForRouteA'});
// Route B
BlazeLayout.render('layout', {main: 'mainForRouteB', nav: 'navForRouteB'}) while // Route A
BlazeLayout.render('layout', {main: 'mainForRouteA', nav: {template: 'nav', param: someObjectA}});
// Route B
BlazeLayout.render('layout', {main: 'mainForRouteB', nav: {template: 'nav', param: someObjectB}}); where |
By the way, the documentation (readme) can be improved with the Notes on Passing Data section from https://kadira.io/academy/meteor-routing-guide/content/rendering-blaze-templates since the readme does not make it very obvious that you can pass data. At least an example would be great. |
@serkandurusoy For nav and main, I create a wrapper header template for each page, and then put the object as a helper function to the wrapper header. Not ideal, but solvable. I have another issue with this, see: #30 For that, I ended up creating multiple layouts and handle the data passing from the nav and main in the layout template. So it doesn't really use dynamic template inclusion at all. I think it is a big inconvenience that no data can be passed between the dynamically included templates provided by BlazeLayout. |
Yes I agree. Sometimes it is just some static data and helps modularize templates. In my case I ended up passing in an arbitrary object as an extra parameter within the render function, and called necessary parts of that object from the data property of the dynamic template. Not very elegant, but seems (to me) better than maintaining cascaded templates. |
How to pass extra argument to the render function? I wasn't aware of that. Can you provide an example? |
<template name="layout">
<main>
{{> Template.dynamic template=main}}
</main>
<aside>
{{> Template.dynamic template=aside data=attrs.aside}}
</aside>
</template>
<template name="asideTemplate">
<ul>
{{#each items}}
<li>{{title}}: {{content}}</li>
{{/item}}
</ul>
</template> var aside1Data = {
items: [
{title: 'title1', content: 'content1'},
{title: 'title2', content: 'content2'}
]
}
BlazeLayout.render('layout', {main: 'mainTemplate', aside: 'asideTemplate', attrs: {aside: aside1Data}}) |
Sweet. Just have time to take a look. I hope it can handle reactive data, but even it if doesn't, it is still very useful. Thanks. |
I really like VladShcherbin's method! Is this the accepted way of doing it? The documentation on this (how to e.g. set up a route & layout with flow-router and blaze-layout for a blog post display) is very confusing and there's a lot of useless / misleading stuff out there. I keep finding stuff about "anti-patterns" and nothing on how to actually do it correctly. Don't tell us about the anti-patterns, just tell us the patterns... |
Ok, I'm getting a feel for it... using FlowRouter.getParam('slug') instead of VladShcherbin's Router.current().params.slug as explained here and then I really don't need to do anything with data in the BlazeLayout call or the router. |
Just wanted to add, you can add additional params to the route param object (as arunodo pointed out). Just modify the param object on the route action method:
Then just consume the params in template helpers and setup real data there (as pointed out previously). |
I tried the following code to pass data to a template and receive it in onCreated() but i cannot access the data. |
@jhreis how do you get the param after this? It doesn't seem to be working for me. |
how to pass data to layout?
The text was updated successfully, but these errors were encountered: