2

how to render the contents of a template .. i can;t pass through this point :/ thanks for your help

example :

<body>
  {{> dash}}
</body>

<template name="dash">
<div id="example2" class='example_block'> 
    <form name = "frm">
        <table>
            <tr>
                <td>Template Name
                <td>:
                <td><input type="text" name = tname class = "tname">
      </frm>
       <div class='demo'>
            <input type='button' value='Click Here to Create Window' class="btn"/> 
        </div> 
    </div>
<div id = "window_block8" style="display:none;"></div>
</template>

<template name="t1">
    try1
</template>

<template  name="t2">
    try2 
</template>

   //client.js
 Template.dash.events({
'click input.btn' : function(){
    var temp = document.frm.tname.value ;
    Session.set("template" , temp);
            $('body').append(Meteor.render(Template[Session.get("currentTemplate")]()));
     }
 });

some thing like this.. but that code wont work on me

1 Answer 1

5

Meteor.render(Template.try) returns a document fragment which you can insert into your page with jQuery or vanilla JS.

e.g

Template.dash.aw = function() {
    document.body.appendChild(Meteor.render(Template.try));
}

Note that you can use array-style notation if the template name is variable:

document.body.appendChild(Meteor.render(Template[Session.get("currentTemplate")]));

Alternatively, if you only want to return a variable and not an entire template:

Template.try.var = function() {
    return 'Hello';
}
Template.dash.aw = function() {
    return Template.try.var();
}
// Template.dash.aw = Template.try.var = function() {
//    return 'Hello';
// }

The commented function is probably inadequate, since you want some logic in Template.dash.aw

Sign up to request clarification or add additional context in comments.

8 Comments

thanks bro .. :) .. but what i want is to get the data inside the template name = try ...
Sorry, the code didn't format properly - see the first two examples
Are there any errors in the console log? Could you post a js fiddle?
Have a look at my updated answer. Note the lack of parentheses after Template.try now - Meteor.render takes a function, not a html string
I don't think this works anymore. Please update the answer for Meteor versions after Blaze was introduced.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.