Template as value of a variable - How to?

Hi,
I would like to display a template in dependence to conditions.

Like:

<head>
  <title>LoadTemplate</title>
</head>

<body>
  <h1>Welcome to Meteor!</h1>
  {{> select}}
  <script>
  if (condition = true){
    document.write('{{> hello}}');
  } else{
    document.write('{{> info}}');
  }
  </script>
</body>

<template name="hello">
  <button>Click Me</button>
  <p>You've pressed the button {{counter}} times.</p>
</template>

<template name="info">
  <ul>
    <li><a href="https://www.meteor.com/try" target="_blank">Do the Tutorial</a></li>
    ...
  </ul>
</template>

But it only returns the raw text ā€˜{{> hello}}ā€™. Not the template. How to transfoerm the value of a template as a variable?

You donā€™t have to do such ugly things when youā€™re using Blaze :slight_smile:

Try this:

<body>
  <button>Click Me</button>
  <p>You've pressed the button {{counter}} times.</p>
  {{#if counter}} <!--Non-zero values count as "truthy"-->
    {{>hello}}
  {{else}}
    {{>info}}
  {{/if}}
</body>

(I tried to adapt it so that it would work with your standard example app)

1 Like

Yes, it works. Thanks so far for your quickly and usefully awnser.

Does exist an onload event? I want to depends it to a select dropdown. Maybe IĀ“m ready for today and just didnĀ“t see how easily it isā€¦ I canĀ“t get itā€¦

This is sort of the equivalent for onLoad in Blaze:

Template.myTemplate.onRendered(function(){
  //Do stuff
});

If you want to initialize jQuery elements and such, thatā€™s usually where to do it. However, you usually want to avoid working in an ā€œimperativeā€ style if you can. Meteor is all about doing things in a more ā€œdeclarativeā€ way. I suggest going through a good tutorial before trying to get things working on your own, so you donā€™t do things the hard way.

1 Like