Irina Rapoport
Irina Rapoport

Reputation: 1692

How Do I Create a Custom Block Helper in Spacebars?

I want to write a custom template like this:

{{# myTemplate }}
    <div> some HTML goes here </div>
{{/myTemplate }}

Is this even possible? If not, what's the next best thing?

Use case: a bunch of buttons on a side bar. Buttons have different HTML content: some have SVG, some font Awesome, one has a template, etc. All buttons have the same decoration that would be nice not to mix with the content.

Upvotes: 1

Views: 223

Answers (1)

Irina Rapoport
Irina Rapoport

Reputation: 1692

https://github.com/meteor/meteor/blob/devel/packages/spacebars/README.md

Custom Block Helpers

To define your own block helper, simply declare a template, and then invoke it using {{#someTemplate}} (block) instead of {{> someTemplate}} (inclusion) syntax.

When a template is invoked as a block helper, it can use {{> Template.contentBlock}} and {{> Template.elseBlock}} to include the block content it was passed.

Here is a simple block helper that wraps its content in a div:

<template name="note">   
    <div class="note">
       {{> Template.contentBlock}}   
     </div> </template> 

You would invoke it as:

{{#note}}   
    Any content here 
{{/note}} 

etc.

Upvotes: 1

Related Questions