Raza
Raza

Reputation: 300

Mustache template concatenation of expression with some string

How can we concatenate an expression value with some string and evaluate the resulting expression?

If we have one object prefix and I wanted to concatenate its value with some string. e.g

{{prefix"Output"}}

I would like to first evaluate the prefix value and then concatenate it with the "Output" string.

So the resulting expression may be something like "UnitOutput" where Unit is the value of the prefix.

Lastly I want the concatenated expression from above to be evaluated as well in that case resulting value will be the value of "UnitOutput".

Upvotes: 2

Views: 5527

Answers (4)

BLASTOR
BLASTOR

Reputation: 316

You can also use a function : {{ getValue(variable) }} In your element, just declare your function : getValue: function(var) { return var + "Output";}

Upvotes: 1

martypdx
martypdx

Reputation: 3712

With RactiveJS it's simply:

{{ this[prefix + "Output"] }}

See http://jsfiddle.net/236bsky0/. this refers to current context or root, but can be another reference if you like.

Upvotes: 1

The keyword prefix did not work for me. I am assuming it is a reserved keyword. I will use pref in this example.

Html

<paper-input value="{{pref}}"></paper-input>
<span>Output: {{result}}</span>

Javascript

Polymer({
    pref: '',
    result: 'Output',
    prefChanged: function(oldValue, newValue) {
        this.result = newValue + 'Output';
        // evaluate result here
    }
}

Or you could use Polymers observer pattern

Html

<paper-input value="{{xyz.prefix}}"></paper-input>
<span>Output: {{result}}</span>

Javascript

Polymer({
    result: '',
    observe: {
        'xyz.prefix': 'validate'
    },
    validate: function(oldValue, newValue) {
        this.result = newValue + 'Output';
    },
    ready: function() {
        this.xyz = {
            prefix: 'test'
        }
    }
});

If you're observing Arrays please refer to observe-js

Upvotes: 1

Enjoyted
Enjoyted

Reputation: 1151

doing like

"{{variable}} Output"

should work

Upvotes: 1

Related Questions