zimt28
zimt28

Reputation: 705

Meteor: Share Template

I'm building an app using Meteor with a really large form. I'm using almost the same html template twice, once for input and once for output, but the helpers are different.

As two templates are hard to maintain, my question is, if there is a way to share a html template or clone it, so that I can have the same template but different helpers and events?

Upvotes: 2

Views: 163

Answers (1)

saimeunt
saimeunt

Reputation: 22696

You could use a template argument for this purpose.

HTML

<template name="parent">
  {{> reusableTemplate mode="input"}}
  {{> reusableTemplate mode="output"}}
</template>

client/views/reusable-template/reusable-template.html

<template name="reusableTemplate">
  <form>
    <p>{{helper}}</p>
    <button type="submit">
      Submit
    </button>
  </form>
</template>

JS

client/views/reusable-template/lib/input.js

inputHelper=function (){
  console.log(this.mode);
  // specific input code
};

client/views/reusable-template/lib/output.js

outputHelper=function(){
  console.log(this.mode);
  // specific output code
};

client/views/reusable-template/reusable-template.js

Template.reusableTemplate.helpers({
  helper:function(){
    if(this.mode == "input"){
      inputHelper.call(this);
    }
    else{
      outputHelper.call(this);
    }
  }
});

Upvotes: 2

Related Questions