I use gulp for precompiling .hbs.
The code inside the blocks must be inserted into several places. The problem here is that instead of the right value, this is assigned undefined:
Cannot read property 'match' of undefined
{{#>simpleName}}
{{getSimpleName (lookup ../this #key)}}
{{/simpleName}}
<ul class='{{list-votes}}'>
{{#each list-items}}
<hr/>
<li>
{{#key}}
{{#each this}}
<figure class='{{ #simpleName}}'>
<img class="{{getSimpleName (lookup ../this #key)}}__img" src="./images/{{getSimpleName .}}.png" alt="{{.}}" width="48" height="48"/>
<figcaption class="title header__title">{{.}}</figcaption>
</figure>
{{/each}}
</li>
{{/each}}
</ul>
getSimpleName - helper js-function, it returns a changed string.
Can't I use {{getSimpleName (lookup ../this #key)}} inside partial blocks?
You can use this inside a Partial View. Below is my code and it works.
MyList:
const list = [
{
name: "A:FN",
ln: "A:LN"
},
{
name: "B:FN",
ln: "B:LN"
}
]
Master View (list.handlebars):
{{#each list}}
{{> sample}}
From Master: LN: {{getSimpleName this.ln}} <hr/>
{{/each}}
Partial View (sample.handlebars):
From Partial: Name: {{getSimpleName this.name}} <br/>
Final Result:
From Partial: Name: A:FN:ADDED
From Master: LN: A:LN:ADDED
------------------
From Partial: Name: B:FN:ADDED
From Master: LN: B:LN:ADDED
------------------
I hope you are having a specific problem with lookup.
Alternatively you can achieve this with inline partials:
{{#*inline "sample"}}
From Partial: Name: {{getSimpleName this.name}} <br/>
{{/inline}}
{{#each list}}
{{> sample }}
From Master: LN: {{getSimpleName this.ln}} <hr/>
{{/each}}
Related
I'm trying to use the lookup function in handlebars to pass a dynamic partial, but it's not quite working for me.
My data is:
{
"sections": [
"about",
"when",
"where"
]}
Template looks like:
{{#each copy.sections}}
<section class='section' id='section-{{this}}' aria-labelledby='hed-{{this}}'>
<h2 class='section__hed' id='hed-{{this}}'>{{this}}</h2>
{{> graphic/(lookup . 'this') }} // Need to render this dynamically
</section>
{{/each}}
Basically I'd like the lookup to resolve as:
{{> graphic/about}}, {{> graphic/when}}, and {{> graphic/where}}
If your data is:
var copy = {
"sections": [
"about",
"when",
"where"
]};
You only need to specify {{#each sections }} instead of {{#each copy.sections}}:
{{#each sections}}
<section class='section' id='section-{{this}}' aria-labelledby='hed-{{this}}'>
<h2 class='section__hed' id='hed-{{this}}'>{{this}}</h2>
graph/{{ this }}
</section>
{{/each}}
I couldn't make sense out of your code {{> graphic/(lookup . 'this') }}, it does not have a valid syntax, so I assumed what you are trying to do is graph/{{ this }}.
Usually I just do {{pathFor 'dashboard'}}
But what if I get the path 'dashboard' from template helper as {{name}}
I can't do this{{pathFor '{{name}}'}} but I need to!
It's a submenu which looks like:
{{#each connectedChannels}}
{{> submenuChannel}}
{{/each}}
<template name="submenuChannel">
<li>
<span class="sidebar-text">{{name}}</span>
</li>
</template>
It should work if you remove the quotes and braces around name:
<a href="{{pathFor name}}">
I have a list template (#each) in a package that I plan to use across many different collections. Since the template is in a package they are not easily customizable. So I figured this was a great example to use Template.dynamic. Everything works except passing data.
.. I pull the data into the routed page and manipulate the data to match the dynamic template.
Template.usersIndex.helpers({
items: function() {
var users = Meteor.users.find({}).fetch();
var items = users.filter(function(user) {
return user;
}).map(function(user){
return {
name: user.profile.name,
description: user.emails[0].address,
tidbit: "hello"
};
});
return items
}
});
... the data passes perfectly to the usersIndex template.
<template name="usersIndex">
<div id="gc-users-index-navbar">
<h2>Title</h2>
</div>
<div id="gc-users-index" class="inner-content">
{{> Template.dynamic template="strataIndexItem" data="items" }}
</div>
</template>
... But no dice, the dynamic template is rendered but no data.
<template name="themeIndex">
<div class="list-group">
{{#each items }}
<div class="list-group-item">
<div class="row-content">
<div class="least-content">{{tidbit}}</div>
<h4 class="list-group-item-heading">{{name}}</h4>
<p class="list-group-item-text">{{description}}</p>
</div>
</div>
<div class="list-group-separator"></div>
{{/each}}
</div>
</template>
You pass data as string?
{{> Template.dynamic template="strataIndexItem" data="items" }}
You should pass data as variable, without ""
{{> Template.dynamic template="strataIndexItem" data=items }}
Also check if your strataIndexItem template is named strataIndexItem:
<template name="strataIndexItem">
...
</template>
rather a theoretical question - how can I render recursive templates in Meteor? For example, displaying a comment on comment with unlimited number of comment sub-levels so that HTML would be diplayed as the following?
<section>
some text
<section>
nested text
<section>
further nested text and sections
.....
</section>
</section>
</section>
In my case I pass to the "tree" template a mongoDB document and this document can have unlimited number of sub-content levels. My example below doesn't work the way I want.
<template name="tree">
<div class="wrapper" style="border:1px solid red">
<ul>
{{#each getStructure}}
<li>
{{#each content}}
<ul>
<li>
<a class="item">{{text}}</a>
<!-- TODO: this stuff needs to be recursive.
{{#if sub_content}}
<ul>
{{#each sub_content}}
<li>
<a class="item">{{text}}</a>
{{#if sub_content}}
....
{{/if}}
</li>
{{/each}}
</ul>
{{/if}}
</li>
</ul>
{{/each}}
</li>
{{/each}}
</ul>
</div>
</template>
A simplified example of recirsuve, say you had a post sample template of:
<template name="post">
{{post_text}}
{{#each comments}}
{{>comment}}
{{/each}}
</template>
and a post helper of:
Template.post.helpers({
comments: function() {
return CommentCollection.find({post_id: this._id, parent_id: {$exists: 0}});
}
});
I would create a template for the comment layout and provide a helper in that for sub-comments, depending on your data structure something like the following:
<template name="comment">
{{comment_text}}
{{#each sub_comment}}
{{> comment}}
{{/each}}
</template>
and then the helper along the lines of:
Template.comment.helpers({
sub_comments: function() {
return CommentCollection.find({parent_id: this._id});
}
});
This would recursively produce the comments template for each sub-comment then roll back up the tree to the next #each and then print that comment and all of its sub-comments etc.
[
{name:"foo",last:"bar",age:100},
{name:"baz",last:"foobar",age:200},
]
Is there a way on how to iterate that to a meteor template using {{#each}} helper like
<template name='boo'>
{{#each objectInArray}}
<span> {{namehere}} </span>
{{/each}}
</template>
You can do it like this:
<template name="boo">
{{#each objectInArray}}
<span> {{foo}} </span>
<span> {{last}} </span>
<span> {{age}} </span>
{{/each}}
</template>
Template.boo.objectInArray = function() {
return [
{name:"foo",last:"bar",age:100},
{name:"baz",last:"foobar",age:200},
]
}