Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Allow 3rd party library access from within compiled templates #44

Closed
paularmstrong opened this Issue · 9 comments

4 participants

@paularmstrong

Scenario

Implementing a custom tag, a person may want to be able to access a third-party library from within compiled swig templates. Currently this is not possible due to the way the templates are compiled an executed.

Example

@Tatort79 wants to create a custom tag for i18next, but access to that library is restricted from compiled templates.

Solution

Allow the ability to register third-party dependencies via swig.init and allow them to be callable into the render method. See lines #71, #91, and #94

@paularmstrong paularmstrong was assigned
@Tatort79

Wow - I will give that one a try. Feedback coming ;-)

@paularmstrong

Great! Let me know if the documentation is clear as well.

@Tatort79

Will there be a new npm-release or have a get your latest dev out of github?

@paularmstrong

I'll be pushing later today. If you're testing it now, you can just clone the master branch from github.

@Tatort79

Ok, i cloned the master branch of github, but i still have a smal problem ;-) Where ist my mistake?

var parser  = require('swig/lib/parser'),
    helpers = require('swig/lib/helpers');

exports.t = function (indent) {

    var myArg = parser.parseVariable(this.args[0]),

    output = [];

    output.push(helpers.setVar('__myArg', myArg));
    output.push('_output += i18next(__myArg);');
    return output.join('');

};
exports.t.ends = false;

And in my server.js i have the swig.init like this :

var i18next = require('i18next');
 swig.init({ 
      extensions: {
        i18next: i18next
      },
      root: __dirname + '/views', 
      allowErrors: true,
      tags: require('./lib/swigtags.js')
});

This is the error i get :

ReferenceError: i18next is not defined
    at Object.<anonymous> (eval at createTemplate (/Users/xxx/development/yyy/node_modules/swig/index.js:72:14))
    at Object.render (/Users/xxx/development/yyy/node_modules/swig/index.js:92:27)
@paularmstrong

Instead of output.push('_output += i18next(__myArg);');
That should read output.push('_output += _ext.i18next.t(__myArg);');

I've updated the documentation to reflect this.

@Tatort79

It worked ;-)

But i need to use the "escapeVarName", to make it work :

exports.t = function (indent) {
var value = helpers.escapeVarName(this.args[0]);
output = [];
output.push('_output += _ext.i18next.t("'+value+'");');
return output.join('');
};
exports.t.ends = false;

Thank you very much for this feature. Now i can change my application to be multi-language.

@scull7

How would one get the second parameter passed to the i18next function as an object?

@Mamsaac

I ended up comming here for the same question as scull7. Is it possible, and how, to manage to get both parameters passed? I haven't fully grasped the extension tags yet :P

I ended up doing this: http://pastebin.com/8PJwN5Ju

To use it:
{% trans %}app.hello{% endtrans %}
{% trans somevar %}app.hello2{% endtrans %}

Works fine. somevar is for when app.hello2 has a variable in its text (somevar).

Now I just need to understand how to do the plural/singular tags, so a middle tag within trans. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.