Skip to content

Custom tags#359

Open
zverev wants to merge 3 commits into
janl:masterfrom
zverev:customtags
Open

Custom tags#359
zverev wants to merge 3 commits into
janl:masterfrom
zverev:customtags

Conversation

@zverev

@zverev zverev commented Feb 20, 2014

Copy link
Copy Markdown

Hello,

I've tried to make an internationalization for our project using mustache, but didn't find a rather simple way to do it. So i've thought about using custom tag for this task. For example {{i some text}}.
Does this idea corresponds to the philosophy of mustache.js?

Best regards,
Alexander

@dasilvacontin

Copy link
Copy Markdown
Collaborator

Could you explain, give an use example? If I understand correctly, for internationalisation, couldn't you just pass an object with a different set of strings?

@phillipj

phillipj commented Jan 6, 2015

Copy link
Copy Markdown
Collaborator

This isnt just targeted at internationalization is it? As it enables extensibility it resembles a simple plugin system IMO. Could it be something to work on related to plugins / pragmas as mentioned in #406?

@zverev

zverev commented Jan 7, 2015

Copy link
Copy Markdown
Author

@dasilvacontin
According to this pull-request:

var translationsHash = {
    firstName: "First name",
    lastName: "Last name"
};

Mustache.addCustomTag('i', function(contents, value) {
    return translationsHash[contents];
});

Template may look like this:

<div class="user">
    <div class="user-firstName">{{i lastName}}: {{firstName}}</div>
    <div class="user-lastName">{{i firstName}}: {{lastName}}</div>
</div>

If we use this approach, we don't need to pass strings at all.

@phillipj
Actually Handlebars has this feature (helpers), so we switched to it. Now I'm not sure, that mustache.js really need custom tags.

@dasilvacontin

Copy link
Copy Markdown
Collaborator

@zverev, thanks for the template example.

Couldn't you just pass a different data/model object or a modified one to Mustache.render?

@zverev

zverev commented Jan 28, 2015

Copy link
Copy Markdown
Author

@dasilvacontin
Yes, I certainly could. But this method allows to avoid it. Also in my case I have some global translations, that are used by multiple components (e.g. button titles such as 'ok', 'cancel'). Without 'i' tag I would have to pass them manually.
Sorry for the late reply.

@dasilvacontin

Copy link
Copy Markdown
Collaborator

No worries. I see what you mean.

We certainly need some kind of Plugin API. Having a Plugin API would make solving this issue something trivial. I think we should discuss how we approach adding a Plugin API in a separate issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants