Skip to content


Subversion checkout URL

You can clone with
Download ZIP


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

paularmstrong opened this Issue · 9 comments

4 participants



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.


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


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

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


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


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


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


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');
      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)

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.


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.


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


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:

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.