title: Mustache Dynamically set variable
body: <p>I need help in using a javascript variable set dynamically as my mustache template variable. Here is the problem. My JSON looks like this</p>
  
  <pre><code>jsonData = {
  "recipemodules" : {
      "enchiladas" : [
          {
              "title" : "Chicken Enchiladas",
              "thumbnailurl" : "http:www.google.com/image",
              "recipelink" : "location to recipe",
              "credit" : "some credit"
          },
          {
              "title" : "Chicken Enchiladas",
              "thumbnailurl" : "http:www.google.com/image",
              "recipelink" : "location to recipe",
              "credit" : "some credit"
          }
      ],
      "roastchicken" : [
          {
              "title" : "Chicken Enchiladas",
              "thumbnailurl" : "http:www.google.com/image",
              "recipelink" : "location to recipe",
              "credit" : "some credit"
          },
          {
              "title" : "Chicken Enchiladas",
              "thumbnailurl" : "http:www.google.com/image",
              "recipelink" : "location to recipe",
              "credit" : "some credit"
          }
      ],
      "salmon" : [
          {
              "title" : "salmon ",
              "thumbnailurl" : "http:www.google.com/image",
              "recipelink" : "location to recipe",
              "credit" : "some credit"
          },
          {
              "title" : "Chicken Enchiladas",
              "thumbnailurl" : "http:www.google.com/image",
              "recipelink" : "location to recipe",
              "credit" : "some credit"
          }
  
      ]
  }
  </code></pre>
  
  <p>};</p>
  
  <p>Then what i am doing next is... going to a webpage...grabing the URL and based on that URL, I set a variable.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var theCurrentURL = window.location.href;
  var finalJSONobject = "";
  
  switch(theCurrentURL) {
   case "www.google.com/something1":
    finalJSONobject = "enchiladas";
    break;
   case "www.google.com/something2":
    finalJSONobject = "roastchicken";
    break;
   case "www.google.com/something3":
    finalJSONobject = "salmon";
    break;
   default:
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>Finally... here is what my mushtache template looks like</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>// create template
  template = '{{#enchiladas}}&lt;div class="mri-container"&gt;&lt;img src="{{thumbnailurl}}" /&gt;&lt;/div&gt;{{/enchiladas}}';
   // parse template
   Mustache.parse(template);
   // render template
   rendered = Mustache.render(template, jsonData.recipemodules);
        
   // inject template to DOM
   carousel.html(rendered);</code></pre>
  </div>
  </div>
  </p>
  
  <p>Now it runs fine when i use {{#enchiladas}}{{/enchiladas}} ... but this is not what i want. i want to be able to use a variable name instead of enchiladas.</p>
  
  <p>if you look at my switch statement... it returns finalJSONobject as a string. i want my template to look like </p>
  
  <pre><code>template = '{{#finalJSONobject}}&lt;div class="mri-container"&gt;&lt;img src="{{thumbnailurl}}" /&gt;&lt;/div&gt;{{/finalJSONobject}}';
  </code></pre>
  
  <p>But this does not work. The mustache template is using jsonData.recipemodules as its data. Therefore jsonData.recipemodules.enchiladas will work... but i want to dynamically set "enchiladas" which is set on my switch statement. I want to use my dynamically set variable in the template. i want to use jsonData.recipemodules.finalJSONobject so that based on the page... i look the data i want.</p>
  
  <p>Please help me! thank you so much.</p>
  
  <p>Konstantin</p>