How to access global variables in a Meteor template without using an assistant?

I have all my image files served from another domain, and I put this hostname in a variable in Meteor.settings. Then, how can I access this variable in a Meteor template?

For example, in this template, what is the best way to replace img.example.com with a variable defined in Meteor.settings or some other global variables? I don’t think it’s a good idea to pass it to each template with helpers.

 <template name="products"> {{#each items}} <img src="http://img.example.com/{{id}}.png"> {{/each}} </template> 
+6
source share
5 answers

The only way to transfer data to your templates is through helpers. You can use the global helper :

 Template.registerHelper('imgExampleUrl', function() { return 'img.example.com'; }); 

Then you can use the global helper in many templates:

 <template name="products"> {{#each items}} <img src="http://{{imgExampleUrl}}/{{id}}.png"> {{/each}} </template> <template name="otherTemplate"> <img src="http://{{imgExampleUrl}}/{{id}}.png"> </template> 

Or, if you want to get the value of imgExampleUrl from settings.json

 Template.registerHelper('imgExampleUrl', function() { return Meteor.settings.public.imgExampleUrl; }); 

Your .json settings:

 { "public": { "imgExampleUrl": "img.example.com" } } 
+10
source

In your Js file do this. It can help you.

 Template.yourTemplateName.varNameYouhavetoaccess= function(){ return getYourGlobalValueHere; } 

And On the HTML page in the template, you can list the value {{varNameYouhavetoaccess}}

Or you can use Helper

In the JS file:

Template.nametag.helpers ({name: "Ben Bitdiddle"});

In HTML:

 <template name="nametag"> <p>My name is {{name}}.</p> </template> 
0
source

Assign a global function with return type

 if(Meteor.isClient){ getItems = function(){ //do your stuffs return items; } 

Your template

 <template name="products"> {{#each items}} <img src="http://{{imgExampleUrl}}/{{id}}.png"> {{/each}} </template> 

Assistants

 Template.products.helpers({ items : function(){ return getItems(); } }); 

You can use getItems() from anywhere

0
source

I know that this is not quite what the opera asked about, but this page appeared on Google when searching for “How to access Meteor settings from a template”.

I expanded the @Tomas Hromnik theme and made this global template helper:

helpers.js

 Template.registerHelper('meteorSettings', function(settings) { var setting = Meteor.settings.public; if (settings) { var eachSetting = settings.split('.'); for (i = 0; i < eachSetting.length; i++) { setting = setting[eachSetting[i]]; } } return setting; }); 

home.html

 <template name="home"> <!-- Basically the same as doing this: (except you can't do this) {{#if Meteor.settings.public.instagram.access_token}} --> {{#if (meteorSettings 'instagram.access_token')}} <div class="instagram"></div> {{/if}} <!-- You can also set Meteor.settings.public to a variable to access multiple settings --> {{#let settings=meteorSettings}} {{settings.instagram.access_token}} {{/let}} </template> 
0
source
 Template.registerHelper('var', name => { const data = Template.instance().data || {}; return data[name]; }); 

Inner template:

 {{var 'someVariableFromTemplate'}} 
0
source

Source: https://habr.com/ru/post/985837/


All Articles