EmberJS, String interpolation in a helper call [duplicate] - templates

This question already has an answer here:
Concatenation string with variable in emblem.js
(1 answer)
Closed 7 years ago.
I have this helper call in my template:
{{my-helper id='myID'}}
And I want to make the id param value a bit more dynamic, for example:
{{#each charts as |chart|}}
{{my-helper id='chart' + chart.id}}
{{/each}}
How I can I use string interpolation and concatenation to create a value for a param in a helper call?

You can use the concat helper to do this:
{{#each charts as |chart|}}
{{my-helper id=(concat 'chart' chart.id)}}
{{/each}}
It's not ES2015 String interpolation, but it will do what you want.

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}}

Concatenation string with variable in emblem.js

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

How To: Add Style in OSCommerce tep_image function [duplicate]

This question already has an answer here:
Adding class attribute to osCommerce tep_image function
(1 answer)
Closed 8 years ago.
Oscommerce function tep_image:
This is the code:
tep_image(DIR_WS_IMAGES . "partners_grey_top2.png", "ksa shopping", "395", "36");
How to add style?
The fifth argument of the tep_image function in a stock version of osCommerce accepts custom parameters. If you want to add, say, a CSS class like class="product_image", you would alter your function to look like this
tep_image(DIR_WS_IMAGES . "partners_grey_top2.png", "ksa shopping", "395", "36", 'class="product_image"');

how to get model field type for validation in django [duplicate]

This question already has answers here:
how to get field type string from db model in django
(3 answers)
Closed 9 years ago.
I have tried:
field = model._meta.get_field_by_name(field_name)[0]
my_type = field.get_internal_type
This does not work as it gives back some bound method like:
<bound method URLField.get_internal_type of <django.db.models.fields.URLField:my_url>>
I want something I can world with like CharField, URLField, DoubleField, etc.
How can I get something like URLField so that I can validate if the url is well formed for example?
In Python, if you want to call a function or method, you have to use parentheses around the arguments. If there are no arguments, you just use empty parentheses.
What you're doing is just referencing the method itself, not calling it.
Here's a simplified example:
>>> def foo():
... return 3
>>> foo()
3
>>> foo
<function __main__.foo>
You can't "extract" the result from the function; you have to call it.*
So, in your example, just change the second line to this:
my_type = field.get_internal_type()
* OK, in this case, because the function just always returns a constant value, you could extract it by, e.g., pulling it from the source or the func_code.co_consts tuple. But obviously that won't work in general; if you want to get, say, the value of datetime.now(), it's not stored anywhere in the now method, it's generated on the fly when you call the method.

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.