Access array element in ember template - ember.js

I have already tried this SO Post
But, i want to achieve the following:
Component
abc = [10,20,30,40,50]
cde = [0,1,2,3,4]
Template
{{#each cde as |num|}}
{{ abc.[num] }}
{{/each}}
Expected Output:
10 20 30 40 50
But, it doesn't output anything. What is wrong?

Try this,
{{#each cde as |num|}}
{{get abc (concat num '')}}
{{/each}}
I dont get the need for cde array,
each block comes with index, if needed you can use it.
{{#each abc as |num index|}}

Related

Iterate through custom array in ember js

I need to iterate through custom Array directly in the ember template like:
{{#each [1,2,3,4,5,6,7,8,9,10] as |num|}}
<div class="input-group-{{num}}">
.
.
.
</div>
{{/each}}
How can I do it? The above code doesn't render anything.
If you want to do this, just use a helper. You can either create an helper or use an existing one.
For example, by using ember-array-helper you can express it like that:
{{#each (array 1 2 3 4 5 6 7 8 9 10) as |num|}}
{{num}}<br>
{{/each}}
Or, use range helper of ember-composable-helpers. Such as:
{{#each (range 10 20) as |number|}}
{{! `number` will go from 10 to 19}}
{{/each}}

Loop through an array django - slightly more complex

I am using a JQuery chart library where you pass values a format like [0,2,5,9].
I have an array in my views where I currently access each index to return the value rest 1 = arr[0], rest 2 = arr[1] ... and then passing these values from the view into my HTML page and inserting the values for the chart like [res1,res2]. This is not feasible because I never know the size of the array so it'd be a constant manual approach of accessing the array. Is there a way I easily loop through each one?
Slight problem. Currently after accessing each index I convert value to an int. So I don't think I'd be able to do it via looping through the html page - it'd have to be done via views. Unless I can somehow call conversion function defined in the view in the html page?
--- views ---
array
-> returns multiple string values i.e. name = paul, name = john
-> paul = arr[0]
-> function(paul['']) converts it to a string.
--- html page ---
{{paul}}
ideally i'd like to do:
[rather than refer to each index here just loop through all array values.. but somehow calling my conversion function too on each value else what I want to do won't work. ]
{% for values in array %}
[insert each one here and call the convert int function from views]
I might misunderstood your question, but sounds like you just want to pass a list of string to the template, loop on each item in the list and do some conversion. I'm not really sure which step did you get stuck but the simplest way is to do everything in the views.py:
views.py
def view_func(request):
array = [{'Speed': 2, 'Height': 1, 'PersonID': 1}, {'Speed': 2, 'Height': 1, 'PersonID': 1}]
# do the conversion on each value in the list
converted_array = [int(i['speed']]) for i in array]
context = {'array': array, 'converted_array': converted_array}
template:
<!-- to loop on original array -->
{% for value in array %}
{{ value }}
{% endfor %}
<!-- to loop on the converted array -->
{% for value in converted_array %}
{{ value }}
{% endfor %}

How to render multiple templates using #each in a handlebars?

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

Kendo UI Grid Column Template

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>
);

meteor template compare/passing a variable

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.