Is there a way to pass variables into templates in Meteor?

前端 未结 9 2110
深忆病人
深忆病人 2020-12-01 01:37

I\'ve been experimenting with Meteor and ran into something I couldn\'t figure out. For fun, I was trying to make a slot machine. I had the following HTML:

&         


        
相关标签:
9条回答
  • I wanted to leave this as a comment, because it's just a clarification on Joc's answer, but couldn't, so here it is with plus the example I worked with.

    Only ONE argument can be passed to the template :

    {{> singleItemInfo arg1}}
    

    this argument must be an object such as :

    {
        number: 1,
        number2: 2,
        numberComposite: {
            subnum1: 10,
            subnum2: 20
        }
    };
    

    the argument values can be accessed via their keys, and the scope can be switched to get the subitems with the

    {{#with numberComposite}}

    Here's the full code for the example :

    <html file>

    <body>
        {{ itemsView }}
    </body>
    
    <template name="itemsView">
        {{> singleItemInfo arg1}}
    </template>
    
    <template name="singleItemInfo">
        arg1 = {{ number }}
        arg2 = {{ number2 }} 
        {{#with numberComposite}}
            subArg1 = {{ subnum1 }}
            subArg2 = {{ subnum2 }}
        {{/with}}
    </template>
    

    <javascript file>

    Template.itemsView.arg1 = {
        number: 1,
        number2: 2,
        numberComposite: {
            subnum1: 10,
            subnum2: 20
        }
    };
    

    OUTPUT:

    arg1 = 1 arg2 = 2 subArg1 = 10 subArg2 = 20 
    
    0 讨论(0)
  • 2020-12-01 02:36

    Turns out there is another way.

    I was trying to find out how to do this by googling various searches and found this question but nothing that suited my purpose. TomUnite's answer works unless you want to put the nested templates in different places in the parent template.

    So after much searching I found 'an' answer in the meteor codebase. (Not saying it's the definitive answer but it does work)

    <template name="slots">
      {{> slot one}}
      <div>..something else</div>
      {{> slot three}}
      {{> slot two}}
    </template>
    
    <template name="slot">
      <div class="slot">
        <div class="number"><span>{{number}}</span></div>
        <div class="divider"></div>
      </div>
    </template>
    

    As you see we can specify the template instances in any order. The second parameter is actually a variable that should be defined, so:

    Template.slots.one = {
      number: 1
    }
    Template.slots.two = {
      number: 2
    }
    Template.slots.three = {
      number: 3
    }
    

    This could be made into more succinct code with a loop or maybe using the underscore.js function _.extend on the slots object. Also, We can pass multiple fields of data into these objects.

    0 讨论(0)
  • 2020-12-01 02:36

    Better Answer:

    The two solutions that are available to making a template context sensitive under the new Blaze layout are:

    1) Passing arguments to the template directly

    {{> contextSensitiveTemplate  context_1='x' context_2='y' }}
    

    2) Using a helper in the template that understands the context. Call the helper like this:

    {{ contextHelperName ../.. .. this }}
    

    And

    Template.contextSensitiveTemplate.contextHelperName = function(parent_template, current_template, current_value_inside_each_loop) {
      return context_dependent_value_or_html     
    }
    
    0 讨论(0)
提交回复
热议问题