Add variable to partial name in descriptors (mustache)

I use steering wheels with the grunt assembly, let's say I have a page with a data field called a "page", can I pass this data field to a partial name (in another partial)?

For instance:

  • The page has a 'page' field with a value of 'dashboard-one'
  • The partial "sidebar" that has the layout of the package calls up another partial {{ sidebar-*dashboard-one* }} , where the dashboard is one of the above variables, which is passed from the page
  • This means that every page that needs a sidebar can have a partial name associated with it, such as sidebar-dashboard-one, sidebar-dashboard-two, etc.
+4
source share
1 answer

You cannot pass a variable as a partial name in the usual way. There are two possible solutions:

1) If you know all the possible names, you can statically generate a flat template and use it the way you want.

Initial data:

 { "names": [ { "name": "dashboard-one" }, { "name": "dashboard-two" } ] } 

Static Pattern:

 {{#each names}} \{{#if name.{{name}} }} \{{> sidebar-{{name}} }} \{{/if}} {{/each}} 

will produce:

 {{#if dashboard-one }} {{> sidebar-dashboard-one }} {{/if}} {{#if dashboard-two }} {{> sidebar-dashboard-two }} {{/if}} 

Then you pass the name "dashboard-one" in the context of the template:

 { "name": "dashboard-one" } 

and execute a partial sidebar-dashboard-one .

2) Write a user assistant. Handlebars internally use the Handlebars.VM.invokePartial method to invoke partial ones. See how it works .

0
source

Source: https://habr.com/ru/post/1482388/


All Articles