简体   繁体   中英

How to render template values using javascript - Meteor

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

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

eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM