How to use swig

Comprehensive swig code examples:

How to use swig.prototype:

142
143
144
145
146
147
148
149
150
151
152
    layer.load(id);
    return this.compileFile(pathname, options);
};


// 扩展swig内置函数,用于提供bigpipe支持
Swig.prototype._w = Swig.prototype._widget = function (layer, subTemplate, attr, options) {
    var self = this;
    var id = typeof subTemplate === 'function' ?
        (options.filename || options.resolveFrom + '_' + attr.id) :
        subTemplate;

How to use swig.run:

3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
* @example
* $ swig compile ./mytpl.html --wrap-start="var mytpl = " > mytpl.js
* @example
* <script src="mytpl.js"></script>
* <script>
*   swig.run(mytpl, {});
*   // => "rendered template..."
* </script>
*
* @param  {function} tpl       Pre-compiled Swig template function. Use the Swig CLI to compile your templates.

How to use swig.precompile:

3700
3701
3702
3703
3704
3705
3706
3707
3708
3709

/**
 * Pre-compile a source string into a cache-able template function.
 *
 * @example
 * swig.precompile('{{ tacos }}');
 * // => {
 * //      tpl: function (_swig, _locals, _filters, _utils, _fn) { ... },
 * //      tokens: {
 * //        name: undefined,

How to use swig.setExtension:

57
58
59
60
61
62
63
64
65
  } else {
    return translations && tag && language && translations[tag] && translations[tag][language] ? translations[tag][language] : defaultTranslation;
  }
}

swig.setExtension('i18n', function(ctx, tag, default_translation) {
  var language = ctx.i18n ? ctx.i18n.language : 'unknown';
  return translate(tag, language, default_translation);
});

How to use swig.compile:

38
39
40
41
42
43
44
45
46
47
48
    filename: data.path,
  });
}


swigRenderer.compile = function(data, locals) {
  return swig.compile(data.text, {
    filename: data.path,
  });
};

How to use swig.setTag:

70
71
72
73
74
75
76
77
78
79
    translation = translation.replace(new RegExp(key, "g"),val);
  });
  return translation;
});

swig.setTag('i18n', function(str, line, parser, types) {
  var handlingTag   = true,  // When we start we're handling the TAG
      endIsSafe     = true,  // We can call end catch all
      handlingSubs  = false, // We are handling substitutions if there is more found after TAG
      accumulatedJS = '';    // This is all of the accumulated javascript

How to use swig.Swig:

996
997
998
999
1000
1001
1002
1003
1004
1005
if (!context) {
  throw new Error('Hypr requires a context to be set');
}
this.context = context;

this.engine = new swig.Swig({
  cache: this.cache || 'memory',
  locals: this.context.locals,
  loader: this.loader || swig.loaders.memory(context.templates, '/')
});

How to use swig.invalidateCache:

40
41
42
43
44
45
46
47
48
49

/*
var watch = require('watch')
watch.createMonitor(__dirname + '/views', function(monitor) {
  monitor.on("changed", function(file) {
    swig.invalidateCache();
    ejs.clearCache();
    jade.cache = {};
    hbs.cache = {};
  });

How to use swig.renderFile:

166
167
168
169
170
171
172
173
174
175
    varControls: ['{=', '=}'] //Angular uses the same set ( {{ and }} ), and we don't want swig interpreting angular syntax.
});

var indexPromise = Q.defer();
fileWritePromises.push(indexPromise.promise);
swig.renderFile(path.resolve(__dirname, '../../', './resources/index.html'), structure, function (err, output) {
    if (err) {
        console.log(err)
        indexPromise.reject(err);
    }

How to use swig.setFilter:

62
63
64
65
66
67
68
69
70
71
swig.setExtension('i18n', function(ctx, tag, default_translation) {
  var language = ctx.i18n ? ctx.i18n.language : 'unknown';
  return translate(tag, language, default_translation);
});

swig.setFilter('i18n', function(tag, language, defaultTranslation, replacementPairs) {
  var translation = translate(tag, language, defaultTranslation);
  _.each(replacementPairs, function(val, key) {
    translation = translation.replace(new RegExp(key, "g"),val);
  });

How to use swig.render:

31
32
33
34
35
36
37
38
39
40
41
42


  return compile.join('\n');
}, forTag.ends, true);


function swigRenderer(data, locals) {
  return swig.render(data.text, {
    locals,
    filename: data.path,
  });
}

How to use swig.setDefaults:

157
158
159
160
161
162
163
164
165
166
    }));
    fse.copySync(path.resolve(__dirname, '../../', './resources/css'), baseFolder + '/resources/css');
    lessResolver.resolve();
}.bind(this));

swig.setDefaults({
    locals: {
        config: structure
    },
    varControls: ['{=', '=}'] //Angular uses the same set ( {{ and }} ), and we don't want swig interpreting angular syntax.

How to use swig.loaders:

999
1000
1001
1002
1003
1004
1005
1006
1007
1008
this.context = context;

this.engine = new swig.Swig({
  cache: this.cache || 'memory',
  locals: this.context.locals,
  loader: this.loader || swig.loaders.memory(context.templates, '/')
});

Object.keys(filters).forEach(function(name) {
  self.engine.setFilter(name, filters[name](self));

How to use swig.compileFile:

12
13
14
15
16
17
18
19
20
21
22
23
var macros = require('./lib/macros');


// Precompile templates
var JST = {
  index: swig.compileFile(__dirname + '/templates/index.swig'),
  appcache: swig.compileFile(__dirname + '/templates/appcache.swig'),
};


// Set bootup time as the cache busting hash for the app cache manifest
var bootupTime = Date.now();