0

I have a component in my templating engine (that works well so far) that kind of needs to take an argument.

The component is a navigation on a website. In the CMS there can be many navigations (main, footer, side) but I can't get the logic for Mustache right.

My template `top.mustache':

<div id="wrap-top">
  <div id="top">
    {{> #navigation }} 
        {{ main }} 
    {{> /navigation }}
  </div>
</div>

And then navigation.mustache is just:

<div class="nav">
  {{ #navigation }}
</div>

In PHP I'm rendering it as follows:

$mustache->render("top", [
  "navigation" => [
    "main" => "HTML HERE"
  ]
];

The thing is that I don't want to create multiple tags like navigation_main or navigation_footer because the code is the same. The links may be different but that's handled in PHP where HTML is being generated.

The key is to not have to create mustache files for the names of the navigations. How do I put this all together? Just for the purpose of the question, let's say I do this:

{{ #navigation }}
  {{ main }}
{{ /navigation }}

<-- some other html -->
{{ #navigation }}
  {{ footer }}
{{ /navigation }}

And the result would be:

  <div class="nav"><-- links from main here --></div>
  <-- some other html -->
  <div class="nav"><-- links from footer here --></div>

The reason is that the names of navigations can vary. In PHP I can prepare the array with all available options but how to use only one partial template?

Moseleyi
  • 2,585
  • 1
  • 24
  • 46

0 Answers0