在模板declorations之外使用Meteor Template变量

时间:2022-11-04 19:40:59

I'd like to use variables related to specific Meteor Templates outside of the Template scope. Is this possible?

我想在模板范围之外使用与特定Meteor模板相关的变量。这可能吗?

For example:

main.html:
<head>
<title>{{title}}</title>
</head>

<template name="Layout">
... layout code ...
{{> yield content}}
</template>

<template name="Home">
... code ...
</template>

<template name="Page2">
... code ...
</template>

Using Iron Router, a given template may resolve to a different route. For each route, I'd like the title to change to reflect the route.

使用Iron Router,给定模板可以解析为不同的路由。对于每条路线,我希望改变标题以反映路线。

Currently, when a variable resolves in a template, it still doesn't resolve outside of the template--so the result is that the page's title ends up being "{{title}}".

目前,当变量在模板中解析时,它仍然无法在模板外部解析 - 因此结果是页面的标题最终为“{{title}}”。

1 个解决方案

#1


0  

Currently you can not render variables like that outside the template. However you could do something like this.

目前,您无法在模板外部呈现类似的变量。但是你可以这样做。

    Template.body.onRendered(() => {
         Tracker.autorun(() => {
            document.title = Router.current().route.getName();
         });
    });

#1


0  

Currently you can not render variables like that outside the template. However you could do something like this.

目前,您无法在模板外部呈现类似的变量。但是你可以这样做。

    Template.body.onRendered(() => {
         Tracker.autorun(() => {
            document.title = Router.current().route.getName();
         });
    });