0

Has anyone found a way to dynamically render partials?

I found this link for mustache.js but I am unsure how to implement this in php or if there is even away to do this in php. Even if it was the boolean flag method in the example in the link.

The basics of this would be to choose the partial based on the view file. Making the partial choosing dynamic and not have to specify it in the template.

I have tried using this exact method with only failures.

Content from Link:

View:

{
  items: [
    { type: 'image', url: 'Some URL', is_image: true },
    { type: 'text', content: 'Some text', is_text: true }
  ]
}

Template:

base.mustache

{{#items}}
  {{#is_text}}
    {{>text}}
  {{/is_text}}
  {{#is_image}}
    {{>image}}
  {{/is_image?}}
{{/items}}

text.mustache

<p>{{content}}</p>

image.mustache

<p><img src="{{url}}"/></p>

...can be replaced with:

View:

{
  items: [
    { partial: 'text', content: 'Some text' },
    { partial: 'image', url: 'Some URL' }
  ]
}

Template:

base.mustache

{{@items}}

text.mustache

<p>{{content}}</p>

image.mustache

<p><img src="{{url}}"/></p>
Zack Arnett
  • 239
  • 2
  • 6
  • 19

1 Answers1

0

Looking down the page a bit on the link you posted I saw that the op submitted a ticket on dynamic partials as a mustache spec. Looking through there I found the following posted by thelucid, which accomplishes the goal, though not as pretty as the method you and groue are hoping for.

{
  items: [
    { url: 'Some URL',      html: function() { return '{{>image}}'; } },
    { content: 'Some text', html: function() { return '{{>text}}'; } },
  ]
}

base.mustache
{{#items}}
  {{{html}}}
{{/items}}

text.mustache
<p>{{content}}</p>

image.mustache
<p><img src="{{url}}"/></p>

I believe the html function can be replaced with just a string of the same value, but I don't have a setup where I can test that right now.

Unfortunately the last comment other than a couple people expressing interest in the thread was in January of 2013, so I'm going to go out on a limb and say at this point is seems unlikely that anything will come of it.

D. Kendall
  • 316
  • 2
  • 9