Append templates programatically


Hey everyone

I was trying to do this, but it didn’t work

for (i = 0; i < noplayers; i++)
$(’#playerslotrow’).append( ‘{{> playercard}}’ );

and I want to avoid outright typing out the whole template in html within the javascript
I tried to lookup some handlebars syntax, and I got something like this:

for (i = 0; i < noplayers; i++)
$(’#playerslotrow’).append(template({name: addplayer}));

which gives a lot of ‘template is not defined’ errors
but I feel like im close.
How can i call templates I’ve already built in javascript and append them to a div in my html?
preferably using jquery.


Is there any reason you can’t just have the iteration done directly in Blaze?


{{#each players}}
  {{> player }}
{{/each }}

If players has nothing in it, it won’t render anything. You just need the players helper to reactively update at the appropriate time and it will add the template for each record.


Well as far as i know, #each only works with helpers. Im changing the DOM after every change event on a particular select element.


In general when working with Blaze it’s a bad idea to change the DOM but sometimes it’s necessary. For example you see that with maps implementations.

Here is the advised way of working with libraries:

And then, to render the template with the data in it use:, so that will give something like (untested):

$('#playerslotrow').append(Blaze.toHTMLWithData('playerCard', {name: 'John Doe'}));


OK, but I’m still not clear why you couldn’t be couldn’t be updating some reactive data source in the events that would then automatically trigger the refresh of the Dom by fetching new values from your helper.

Just to be clear, I’m not insisting the above is 100% workable for your particular scenario, just trying to find a solution that lets Blaze do all the heavy lifting and leaves you with a more Blaze-like way of solving the problem. It will usually lead to cleaner, less-verbose code.


Agree with that @peter.roehlen if blaze can handle it that’s in general the best solution.


Thanks guys.

I should note though that there is no data to manipulate; the ‘select’ element literally just has options 1-6, which corressponds to the amount of times that template is supposed to render on the page.


This might be one way of doing it. You can determine better if it works for you or not:



  {{> main}}

<template name="main">
  <!--Select number of players-->
  <label>Number of players:</label>
  <select id="playerCount">
    <option value="0">None</option>
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>

  <!--Render players-->
  {{#each players}}
    {{> player}}

<template name="player">
  <p>Hello player {{playerNumber}}</p>

Helpers/event handlers

import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';

import './main.html';

Template.main.onCreated(function mainOnCreated() {
  // Player count starts at 0
  this.playerCount = new ReactiveVar(0);

  players() {
    let players = [];
    const playerCount = Template.instance().playerCount.get();
    for (var index = 0; index < playerCount; index++) {
        playerNumber: index + 1
    return players;
  'change #playerCount'(event, instance) {
    // set number of players to selection


There’s no need to have the array elements contain anything like I’m doing in the above - ie it could just be an array of nulls I think. But it might be useful to have some sort of index property in each element.


Yeah I changed these lines and it still works. You just don’t get any way of identifying players this way:

    for (var index = 0; index < playerCount; index++) {