0

I am using IronRouter in my project which has content in different languages. To display message codes I have used UI.registerHelper, as follows:

UI.registerHelper('loadMessageCode', function(message) {
    //Logic in here to load string inside a template
    return 'sample-string-in-a-language';
});

My content is loaded from a json file, with strings being specified in different languages as follows. This gets added into a Mongo.Collection when the app starts.

{
    "name": "Pages",
    "items": [
        {
            "title": {
                "en": "About Us",
                "de": "Über"
            },
            "slug": {
                "en": "about",
                "de": "über"
            },
            "content": {
                "en": "......"
                "de": "......"
            }

        }
    ]
}

When generating the links for my content from within my template, I am using IronRouters pathFor function, which I have used before to generate the link. This worked as follows:

<a href="{{pathFor 'content' _page_slug=this.slug}}" title="{{title}}">
    {{title}}
</a>

This worked fine before I refactored the structure of my Json file. Now what I want to do is something along the lines of the following:

<a href="{{pathFor 'content' _page_slug=<Use my helper function to dig out the slug>}}" title="{{title}}">
    {{title}}
</a>

My question is, can this be done and if so, how?

matfin
  • 499
  • 4
  • 15

1 Answers1

0

Instruction for Meteor 0.9.x.

Create additional helper in your template :

JS

Template.template_name.helpers({
    translated_slug: function () {
      // get context data
      var data = Template.currentData();

      // retrieve slug
      var slug = data.slug || "default slug";

      // reuse global helper
      return UI._globalHelpers.loadMessage(slug)
    }
  });

HTML

<a href="{{pathFor 'content' _page_slug=translated_slug}}" title="{{title}}">
    {{title}}
</a>
Kuba Wyrobek
  • 5,273
  • 1
  • 24
  • 26
  • Thanks for the answer. It would be ideal to be able to call the UI helper function directly as opposed to calling it again in the template but with that said, your solution worked very nicely so thanks again. – matfin Sep 17 '14 at 14:00