简体   繁体   English

更改/取消注册Handlebars helper(Meteor)

[英]Change / unregister Handlebars helper (Meteor)

Once I register a helper function for Handlebars using Handlebars.registerHelper() , is it possible for me to change and/or remove the helper? 一旦我使用Handlebars.registerHelper()为Handlebars注册一个辅助函数,我是否可以更改和/或删除帮助程序? Can I just use registerHelper() again to overwrite the current helper, or is there such a thing as Handlebars.unregisterHelper() ? 我可以再次使用registerHelper()来覆盖当前的帮助器,还是有Handlebars.unregisterHelper()这样的东西? Or should I use a different approach if I need a helper to change during an application? 或者,如果我需要在应用程序中更改帮助程序,我应该使用不同的方法吗?

The use case for me is with the Iron Router plugin for Meteor . 我的用例是MeteorIron Router插件 I am using a layoutTemplate as the general structure of my page. 我使用layoutTemplate作为我的页面的一般结构。 I wanted to use a helper in the layout template right before I yield the main content of the page body (via a <template> , per se) so that each individual template can define its own page title but not have to specify the location in the page every time. 我想在生成页面主体的主要内容(通过<template>本身)之前在布局模板中使用帮助器,这样每个单独的模板都可以定义自己的页面标题,但不必指定位置。每次都是这个页面。 For example, my layout template could look like this: 例如,我的布局模板可能如下所示:

{{pageTitle}}
{{yield}}

And then in the .js file for the rendered template, I would use the following to fill in the {{pageTitle}} placeholder: 然后在渲染模板的.js文件中,我将使用以下内容填充{{pageTitle}}占位符:

Handlebars.registerHelper("pageTitle", function() {
    return "My Page Title";
};

Perhaps there is an alternative way to solve this problem. 也许有另一种方法可以解决这个问题。

What you can do is something like this 你能做的就是这样

Handlebars.registerHelper("pageTitle", function() {
    return Session.get('pt');
};

function changePageTitle(str){
    Session.set('pt', str);
}

Meteor, being reactive, should update the page when a session variable changes. 被动的Meteor应该在会话变量发生变化时更新页面。 When you switch to another page, simply run changePageTitle. 当您切换到另一个页面时,只需运行changePageTitle。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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