I have one array of objects inside this objects I have some list of id's and using this id's I have to render multiple templates using #each loop for handlebars.
Simple this statement will return me the list of containing names of template.
{{#each data in sections1}}
{{data.id}}
{{/each}}
Output :
A B C D E F G H I J K L M N O P Q R S T U V W X
Consider this all A to Z are different template names respectively
Now I'm trying to render all these templates using #each loop in handlebar But I don't know the exact syntax.
For example can we do like this way ?
{{#each data in sections1}}
{{render data.id}} //Here I'm getting error Because of it is not exact syntax
{{/each}}
Maybe you should consider using partials, naming them with the proper name based on data.id value. Then use them like: {{partial data.id}}
DEMO
Related
I can get the size of a slice in a template like this
{{$size := len .Things}}
And I can index a slice in a template like this:
{{index .Things 4}}
But
{{index .Things $size}}
gives an "out of range" error because indexing a slice is zero-based.
Do I have do all the function defining things or is there arithmetic available I can use?
I.e. how do I do this https://stackoverflow.com/a/22535888 but in a golang template.
Defining a minus function: https://stackoverflow.com/a/24838050/10245
There is no arithmetic available by default, but you can add this functionality using a FuncMap.
Here's how to add an "add" function that covers this case and other scenarios:
t := template.Must(template.New("").Funcs(template.FuncMap{
"add": func(a, b int) int { return a + b },
}).Parse(theTemplate)
Use it in the template like this:
{{index .Things (add $size -1)}}
In go templates could be viewed as more "passive" than in other languages or some frameworks. The idea of passive views is that they do not include much logic but get passed all their data in.
So you could pass "LastThing" to your template, which has the last Thing assigned.
This does not answer your question directly but this is just an alternative you could consider.
Define variables out of the loop (range):
{{$i:=""}}
{{$el:=""}}
{{range $i,$el = $.Sorteados}}{{end}}
<span >{{$i}} - {{$el}} </span>
So,use them after the loop.
[{'age': 1}] being returned something alike to this after a query. This is in my view. Now in my HTML I want to refer to that value returned - in this case '1' if {{}} > 1 ... do something but it cannot do this because Could not parse the remainder: '{{age}}' from '{{age}}'. However if I define a random integer value in my view set it as i.e. 10 I can very easily refer to this variable in my html no problem. I'm presuming it's because it's printing'age' rather than just returning the result from the database. Any solutions to this ? I'm thinking about creating a definition which returns it into a string
You want {% if number > 2 %}. You use {{ number }} to include a variable in the template, but you don't use the double braces inside a template tag.
I need in Emblem.js to transmit to i18n helper concatenated string constant with variable value, How can i do it?
each item in model.items
div
t "dict.{{item}}"
returns error
Missing translation for key "dict.{{item}}"
If you're using Handlebars 1.3+, you can use a subexpression. First, write a string concatenation helper:
Ember.Handlebars.helper('concat', function (a, b) {
return a + b;
});
Then use it like this (sorry, I don't know Emblem so I'm going to use the normal stache syntax):
{{t (concat 'dict.' item)}}
Classes having one or more students. I wanted to display multiple classes in rows and student names in that particular class as comma separated values. I have used following code but it doesn't work. It is giving runtime error as
CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement.
`columns.Bound(c => c.Students).Template
( m =>
#<text>
#foreach (var student in m.Students)
{
<li> #{ #student.Name; }</li>
}
</text>
);`
The right syntax is this:
columns.Bound(c => c.Students).Template(#<text>
<ul>
#foreach (var student in item.Students)
{
<li>#student.Name</li>
}
</ul>
</text>);
use this
<li> #{ #item.student.Name; }</li>
instead of
<li> #{ #student.Name; }</li>
To get the comma-separated values in the column, I did this:
columns.Bound(s => s.Students).Title("Students").Template(#<text>
#String.Join(",", (from g in item.Students select g.name))
</text>
);
I want to do what should be a simple compare but I have been stuck for the longest time on this:
I have a template that calls a list template, each list is a new column.
For the first column, I do NOT want certain fields displayed. Each column has a variable {{type}}
I want to do either:
a) pass type to the list template so that list can render based on type equals something
or
b) do an compare on type before I call list (in an 'each' loop) - e.g.
{{#if $eq myVar "test"}}
Show this text
{{/if}}
Neither approach works.
This seems to be the simplest thing but I am scratching head and unable to find examples.
thank you
Max
Unsure if this does what you want to achieve, it would help if you post some of your HTML. But to do what you want in b) couldn't you do:
{{#if isEqual myVar "test"}}
...
{{/if}}
and
Template.example.helpers({
isEqual: function(var1, var2) {
return var1 === var2;
}
});
It is probably easier to use collection.find({type: someType}) to just give your template what it should show. Make someType a session variable and you are done.
This sort example shows this pattern.