How to set a layout for a template using Blaze in Meteor?

I am dynamically building some templates and I am trying to set a layout on an html I generate using Blaze in Meteor, here is what I am currently doing

Blaze.toHTML(Blaze.With(data, function() { return Template.my_template; }));

This returns html from a template using the data in it. I tried searching in the documentation but I couldn’t find it, so is there a way to set a layout for this template ?

I am trying to achieve something like

<template name="layout">
    <!-- some styling -->
    {{> yield}}
</template

And use that with the generation of the html.

Thank you

You would normally use a template helper to return the html (you’ll need to ensure my_template is resolved):

Template.myTemplate.helpers({
  htmlStuff: function() {
    return Blaze.toHTML(Blaze.With(data, function() { return Template.my_template; }));
  }
});

and then in your template:

<template name="layout">
    <!-- some styling -->
    {{{htmlStuff}}}
</template>

Note the use of triple braces {{{ }}}

As usual with direct html injection, ensure you don’t expose your app to vulnerabilities.

The case is I am sending emails so I am having several templates: like userEmail, notificationEmail, registrationEmail and I am building those dynamically and I am trying to use the same layout for each of them

So I have something like

html = Blaze.toHTML(Blaze.With(data, function() { return Template.my_template; }));

Email.send({
  from: "from"
  to: "some@email.com",
  subject: "subject",
  html: html
});

That’s the use case so there’s no template helpers involved :slight_smile:

Nope I am trying to send emails with different templates using a single layout, so currently server side I have something like

html = Blaze.toHTML(Blaze.With(data, function() { return Template.my_template; }));
Email.send({
  from: "from",
  to: "to",
  subject: "subject",
  html: html
});

So for example I have userEmailTemplate, notificationEmailTemplate and some data I am generating but I would rather use one layout for all that’s what I am trying to achieve so using template helper is actually not a very good idea