How to use the twig.cache function in twig

To help you get started, we’ve selected a few twig examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github zimmo-be / twig-loader / lib / loader.js View on Github external
var Twig = require("twig");
var path = require("path");
var async = require("async");

var compilerFactory = require("./compiler");
var getOptions = require("./getOptions");
var utils = require('./utils');

Twig.cache(false);

// shared resolve map to store includes that are resolved by webpack
// so they can be used in the compiled templates
var resolveMap = {};

module.exports = function (source) {
    var loaderApi = this;
    var loaderAsyncCallback = this.async();
    var context = loaderApi.rootContext || loaderApi.options.context;
    this.cacheable && this.cacheable();

    // the path is saved to resolve other includes from
    var path = require.resolve(loaderApi.resource);

    // this will be the template id for this resource,
    // this id is also be generated in the copiler when this resource is included
github zhansingsong / create-fes / packages / fes-scripts / config / loaders / twigLoader.js View on Github external
function loader() {
  // async
  const callback = this.async();
  this.addContextDependency(join(process.cwd(), 'src', 'views'));
  this.addContextDependency(join(process.cwd(), 'src', 'mock'));
  const { getMockData, ...others } = loaderUtils.getOptions(this);
  Twig.cache(false);
  this.cacheable && this.cacheable();  // eslint-disable-line
  const currentFilePath = require.resolve(this.resource);
  /**
   * not use source, but use path. so as to support base dir.
   * 为了与后端保持一致,开启 base,开启绝对路径的使用
   */
  const template = Twig.twig({
    id: currentFilePath, // id is optional, but useful for referencing the template later
    base: join(process.cwd(), 'src', 'views'),
    // data: source,
    // allowInlineIncludes: true,
    async: false,
    path: currentFilePath,
    ...others,
  });
  // 使用data参数时,base会被过滤掉
github NightlyCommit / twing / tasks / benchmark.js View on Github external
.add('twig.js', async function () {
        Twig.cache(false);

        let template = Twig.twig({
            autoescape: true,
            data: indexSource,
            allowInlineIncludes: true
        });

        Twig.twig({
            autoescape: true,
            id: 'include',
            data: includeSource
        });

        await template.renderAsync({foo: 'bar'});
    })
    // add listeners
github stefanullinger / grunt-twig-render / tasks / twigRender.js View on Github external
if (!isFunction(fn)) {
        grunt.fail.fatal('"' + name + '" needs to be a function!');
      }
      Twig.extendFunction(name, fn);
    }.bind(this));

    // apply defined filters
    Object.keys(this.options.filters).forEach(function(name) {
      var fn = this.options.filters[name];
      if (!isFunction(fn)) {
        grunt.fail.fatal('"' + name + '" needs to be a function!');
      }
      Twig.extendFilter(name, fn);
    }.bind(this));

    Twig.cache(this.options.cache);
  }
github radiocity / twig-html-loader / index.js View on Github external
const query = utils.getOptions(this) || {};
    let data = query.data || {};
    const templateFile = require.resolve(this.resource);
    const options = {
      path: templateFile,
      data: source,
      async: false,
      debug: Boolean(query.debug || false),
      trace: Boolean(query.trace || false),
      allowInlineIncludes: true,
      rethrow: true,
      namespaces: normalizeNamespaces(query.namespaces),
    };

    if (query.cache !== true) {
      Twig.cache(false);
    }

    if (query.functions) {
      Object.entries(query.functions).forEach(([name, fn]) => Twig.extendFunction(name, fn));
    }

    if (query.filters) {
      Object.entries(query.filters).forEach(([name, fn]) => Twig.extendFilter(name, fn));
    }

    if (query.tests) {
      Object.entries(query.tests).forEach(([name, fn]) => Twig.extendTest(name, fn));
    }

    if (query.extend) {
      Twig.extend(query.extend);
github zimmen / gulp-twig / index.js View on Github external
template;

        if (options.debug !== undefined) {
            twigOpts.debug = options.debug;
        }
        if (options.trace !== undefined) {
            twigOpts.trace = options.trace;
        }
        if (options.base !== undefined) {
            twigOpts.base = options.base;
        }
        if (options.namespaces !== undefined) {
            twigOpts.namespaces = options.namespaces;
        }
        if (options.cache !== true) {
            Twig.cache(false);
        }

        if (options.functions) {
            options.functions.forEach(function (func) {
                Twig.extendFunction(func.name, func.func);
            });
        }

        if (options.filters) {
            options.filters.forEach(function (filter) {
                Twig.extendFilter(filter.name, filter.func);
            });
        }

        if(options.extend) {
            Twig.extend(options.extend);
github unic / estatico / helpers / twig.js View on Github external
'use strict';

var path = require('path'),
	glob = require('glob'),
	fs = require('fs'),
	Twig = require('twig');

Twig.cache(false);

module.exports = {
	render: function(template, data) {
		return Twig.twig({
			allowInlineIncludes: true,
			data: template,
			rethrow: true
		}).render(data);
	},

	registerIncludes: function(config, cache) {
		config.forEach(function(pattern) {
			glob.sync(pattern).forEach(function(file) {
				var id = path.relative('./source', file),
					changed = fs.statSync(file).mtime,
					content;