-1

I'm currently writing my own small templating engine in javascript.

Here's how a template would look like:

<!-- Defines the template for the ribbon. -->
{{TMPL:Import=tabs.tmpl; Name=Tabs}}
<div id="{{ribbonId}}" class="ribbon">

    <!-- All the different tabs are being rendered here. -->
    {{Render:Tabs}}
</div>

You might see that in the id of the div element, I would like to render the value of ribbonId of my object.

Now, I have an object ribbon, which is defined as following:

var ribbon = { ribbonId: "mainRibbon" }

Now, how can I get on this object the value of ribbonId, by it's name?

So, the templating engine would request something like:

var templateName = ribbonId;
alert(ribbon.templateName);

In the alert, I would like to receive 'mainRibbon'.

Is something like this possible in Javascript or jQuery?

Kind regards,

1

1 Answer 1

1

you cannot use dot notation when variable is used as key, you need to use bracket notation, instead of :

alert(ribbon.templateName);

do

var ribbon = { ribbonId: "mainRibbon" };
var templateName = "ribbonId"; //string
alert(ribbon[templateName]);

demo:: JsFiddle

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this was exactely which I'm looking for.
I've did it, I wasn't able to accept (needed to wait a couple of more minutes) but now the time has passed :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.