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