Template.registerHelper vs. Template.template.helpers


#1

Hi,

I registered this helper to set the title as a variable:

Template.registerHelper('setTitle', function() {
    get = getproduct();
    document.title = 'Logins: ' + get.cname;
});

But why couldn’t I put this function in my Template.loginstemplate.helpers({}) ? Or could I? Just trying to understand the difference between the two. Thanks.


#2

It makes a global helper available in all templates. Normally you want to make helpers just for a specific template.

The use case for this one could be for example the status of the logged in user, a standard conversion you need across your app etc.

Can you show you layout with the body and title tag? Then we can be more specific on your case.


#3

Thanks. Actually, I just wanted to set the title based on a variable defined from Flow Router, and I only have 1 template at the moment that outputs different tabular data based on the URL.

<template name="mainLayout">

<head>
    <title>Logins Dashboard</title>
    <link rel="icon" sizes="16x16 32x32" href="/favicon.ico?v=2">
</head>

<body style="background-color:#F2F2F2;">
    <div class="main">
        {{>Template.dynamic template=content}}
    </div>
</body>
</template>


<template name="loginstemplate">
{{#if ready}}
{{setTitle}}
<div class="header">
    <img src="/logo.png" alt="logo" />
    <h3> <b> {{name}} </b> &nbsp;&nbsp;&nbsp; Logged in: <strong> {{countusers}} </strong> &nbsp;&nbsp;&nbsp; Active: <strong> {{countactiveusers}} </strong> &nbsp;&nbsp;&nbsp; <strong>{{activepercentage}}</strong> </h3>
</div>
<div>{{> tabular table=TabularTables.Logins selector=selector class="table table-bordered" width="100%"}} </div> 
{{/if}}
</template>

In this scenario, I imagine I could’ve actually just used a template helper instead of global one, correct me if I’m wrong.