Search code examples
meteormeteor-blazespacebars

Blaze: template inclusion with dynamic content


I have something quite simple to solve, but couldn't find anything in the doc.

I want to create a reusable component with a title, and when I click on it, it open/close its content. Basically a collapsible accordion.

The only problem is content can vary.

Is something like the following code exists in Blaze ?

{{>Accordion title="a title"}}
    <p>a custom paragraph</p>
{{/Accordion}}

{{>Accordion title="another title"}}
    <ul>
        <li>list 1</li>
        <li>list 2</li>
    </ul>
{{/Accordion}}

and would render into:

<div class="accordion">
    <div class="title">a title</div>
    <div class="content">
        <p>a custom paragraph</p>
    </div>
</div>

<div class="accordion">
    <div class="title">another title</div>
    <div class="content">
        <ul>
            <li>list 1</li>
            <li>list 2</li>
        </ul>
    </div>
</div>    

Is it possible ? Or do I have to create a separate Template for the content and call it with {{> Template.dynamic}}


Solution

  • Look for custom block helpers (other source):

    <template name="header">  
      <header>
        {{#if ready}}
          {{> Template.contentBlock}}
        {{else}}
          {{> Template.elseBlock}}
        {{/if}}
      </header>
    </template>
    
    <template name="examplePage">  
      {{#header ready=Template.subscriptionsReady}}
        Example Page
      {{else}}
        Loading...
      {{/header}}
    </template>