Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
你能把一个动态变量传递到Meteor blaze模板中吗?_Meteor_Meteor Blaze - Fatal编程技术网

你能把一个动态变量传递到Meteor blaze模板中吗?

你能把一个动态变量传递到Meteor blaze模板中吗?,meteor,meteor-blaze,Meteor,Meteor Blaze,我看到了对这篇文章的回应: 这篇文章只展示了如何将静态值传递到blaze模板中。请参见以下示例-我正在尝试将user.username传入模板cookieTemplate <template name="userTemplate"> <p> Wellcome {{user.username}} </p> {{#each user.profile.cookies}} {{> cookieTemplate username={{user.us

我看到了对这篇文章的回应:

这篇文章只展示了如何将静态值传递到blaze模板中。请参见以下示例-我正在尝试将user.username传入模板
cookieTemplate

<template name="userTemplate">
  <p> Wellcome {{user.username}} </p>
  {{#each user.profile.cookies}}
    {{> cookieTemplate username={{user.username}} }}
  {{/each}}
</template>

<template name="cookieTemplate">
  {{username}} has cookie {{this.cookieName}}
</template>

Wellcome{{user.username}

{{{#each user.profile.cookies} {{>CookietTemplate用户名={{user.username}} {{/每个}} {{username}}有cookie{{this.cookieName}}

我知道我可以通过
Session.set('username',blah)
来实现这一点,但我想知道是否可以将动态变量传递到模板中?

是的,您可以而且可能已经这样做了

通常,如果您是通过自动发布或发布订阅获取数据,则该数据被视为反应性数据。每次集合中有更新时,此更新都将推送到模板


对于您的情况,如果您通过
currentUser
Meteor.user()
获得数据,情况也会一样。

一个简单的解决方案就是将用户对象传递给模板

{{> cookieTemplate user=user }}
并在模板中使用它作为

{{user.username}}
或者写下一些助手来创建具有以下相关属性的数据对象:

Template.userTemplate.helpers({
  get_user: function() {
    return { username: Template.instance().user.username }
}
然后使用

{{> cookieTemplate user=get_user }}
并使用

{{user.username}}
我做了一些类似的东西(助手函数)作为这个线程的MeteorPad

也许对你有帮助

干杯, 汤姆