How can we use(reuse) one template inside another template


#1

I have made one Template and its helper. now I want to use that template inside another template to reuse the helper functionality and html.
is it possible?

<template name = "some_template">
     // some code
</template>

<template name = "another_template">
    //Events are working but helpers.
     {{>some_template}}
</template>

#2

A helper is just a function. You can define a function in another file, import it into a template and assign it to your template’s helper map.


#3

I have made an object

file: one.js
someObject ={ function one(){ // }, function two(){ // } }
Template.someTemplate.helpers(someObject);

file: one.html
<template name="someTemplate"> // </template>

file: two.html
<template name="another_template"> {{>someTemplate }} </template>


#4

You’re response is highly ambiguous. I’m not sure if you understand what I’m trying to convey or not.


#5

I understood what you said, i just asked whether what i wrote is a possible workaround or not.


#6

You could use an object if you want to use all the same helpers for both templates, but i suspect that isn’t the case…

I would personally use something like so…

// shared-helpers.js
export const someHelper = function() {
    //do something awesome here
}
// some-template.js
import { someHelper } from './shared-helpers.js';

Template.someTemplate.helpers({
    someHelper,
});
// another-template.js
import { someHelper } from './shared-helpers.js';

Template.anotherTemplate.helpers({
    someHelper,
});

#7

I am just trying to create a common reusable component so that I don’t have to define

Template.anotherTemplate.helpers({
    someHelper,
});

I just want to call template like this

<template name = 'another_template'>
      {{>someTemplate}}
</template>

This is working perfectly for events but cannot call helpers of someTemplate in this file.


#8

Is it the helper you want to embed, or the entire template?

Have you tried a global helper?
// /imports/startup/client/shared-helpers.js
Template.registerHelper(“one”, myHelperFunc);


#9

I didn’t know about the global helper. I have made a file shared-helpers.js, now how can I access global helper in my code.
I want to use the entire template as a part of another template.


#10

Global helper worked for me thanks for the help.


#11

Thanks for the reply now i got it what you were trying to say. The global helpers worked for me.
:thinking::grinning: