What are some ways to dynamically create Meteor Handlebars template helpers?
-
This is purely a programming question, however StackOverflow doesn't have an "ask to answer" feature, so I'm asking this here as well. I'm also curious whether Quora could work for this kind of questions or not. Q: I have a list of i18n translation strings within an object that I'm iterating over. Instead of creating a Template Helper for each string manually though, which would soon become redundant and repetitive, I would like to create the Helpers dynamically, within a loop, like so: for (var namespace in Meteor.i18nMessages) { for (var msg in Meteor.i18nMessages[namespace]){ //Template[namespace][msg] = __(namespace + "." + msg); // <- works but is not reactive Template[namespace][msg] = function() { // <- Doesn't work: always returns last value from object return __(namespace + "." + msg); } } } However when I do, I lose reactivity. How would one go about solving this? I'm a fan of best-practices and elegant code. http://stackoverflow.com/questions/16315122/dynamically-create-meteor-handlebars-template-helpers
-
Answer:
This solution is not pretty but it's working. for (var namespace in Meteor.i18nMessages) { var obj = {}; for (var msg in Meteor.i18nMessages[namespace]) { var str = 'obj["' + msg + '"] = function() { return __("' + namespace + '.' + msg + '"); }'; console.log(str); eval(str); } Template[namespace].helpers(obj); }
Kris Haamer at Quora Visit the source
Other answers
I won't show you any code, but cmather did that in mini-pages package: https://github.com/cmather/meteor-mini-pages/blob/master/lib/helpers.js It must be useful :) And it's reactive.
Gabriel H Pugliese
Right now, both Meteor router and mini page router are merged into Iron-router. Do use that. http://www.github.com/EventedMind/meteor-iron-router
Bozhao Yu
Related Q & A:
- How to dynamically create a PHP class name on the fly?Best solution by stackoverflow.com
- How to dynamically create object properties from an array in Javascript?Best solution by nfriedly.com
- How to create a underscore template dynamically?Best solution by Stack Overflow
- What are some ways to make extra money while in college?Best solution by Yahoo! Answers
- What are some ways to make money?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.