How to use the twig.extend 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 radiocity / twig-html-loader / index.js View on Github external
}

    if (query.extend) {
      Twig.extend(query.extend);
    }

    if (typeof data === 'function') {
      data = data(this);
      if (typeof data !== 'object') {
        this.emitError('data parameter should return an object');
      }
    }

    const registry = [];

    Twig.extend((Twig) => {
      const defaultSave = Object.assign(Twig.Templates.save);
      // eslint-disable-next-line no-param-reassign
      Twig.Templates.save = function customSave(template) {
        if (template.path) {
          registry.push(path.normalize(template.path));
        }
        return defaultSave.call(this, template);
      };
    });

    const template = Twig.twig(options);
    const output = template.render(data);

    registry.forEach(this.addDependency);

    Twig.extend((Twig) => {
github radiocity / twig-html-loader / index.js View on Github external
}

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

    if (typeof data === 'function') {
      data = data(this);
      if (typeof data !== 'object') {
        this.emitError('data parameter should return an object');
      }
    }

    const registry = [];

    Twig.extend((Twig) => {
      const defaultSave = Object.assign(Twig.Templates.save);
      // eslint-disable-next-line no-param-reassign
      Twig.Templates.save = function customSave(template) {
        if (template.path) {
github zhansingsong / create-fes / packages / fes-scripts / config / loaders / twigLoader.js View on Github external
* 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会被过滤掉
  // template.base = join(process.cwd(), 'src', 'views');
  // 去掉缓存
  Twig.extend((T) => {
    if (T.Templates && T.Templates.registry) {
      T.Templates.registry = {}; // eslint-disable-line
    }
  });
  // 异步调用
  // 由于使用异步调用所有不能使用相对路径,需要使用绝对路径。
  getMockData(currentFilePath, (mockData) => {
    const output = template.render(mockData);
    callback(null, output);
  });
}
github radiocity / twig-html-loader / index.js View on Github external
const defaultSave = Object.assign(Twig.Templates.save);
      // eslint-disable-next-line no-param-reassign
      Twig.Templates.save = function customSave(template) {
        if (template.path) {
          registry.push(path.normalize(template.path));
        }
        return defaultSave.call(this, template);
      };
    });

    const template = Twig.twig(options);
    const output = template.render(data);

    registry.forEach(this.addDependency);

    Twig.extend((Twig) => {
      // eslint-disable-next-line no-param-reassign
      Twig.Templates.registry = {};
    });
    return output;
  } catch (e) {
    this.callback(e);
    return '';
  }
};
github zimmo-be / twig-loader / lib / loader.js View on Github external
(function compile(templateData) {
        // store all the paths that need to be resolved
        var resolveQueue = [];
        var resolve = function (value) {
            if (resolveQueue.indexOf(value) === -1 && !resolveMap[value]) {
                resolveQueue.push(value);
            }
        };

        Twig.extend(function (Twig) {
            var compiler = Twig.compiler;
            // pass values to the compiler, and return the compiler function
            compiler.module['webpack'] = compilerFactory({
                loaderApi: loaderApi,
                resolve: resolve,
                resolveMap: resolveMap,
                path: path
            });
        });

        tpl = Twig.twig({
            id: id,
            path: path,
            data: templateData,
            allowInlineIncludes: true
        });
github stefanullinger / grunt-twig-render / tasks / twigRender.js View on Github external
this.options.extensions.forEach(function(fn) {
      Twig.extend(fn);
    });
github shopware / platform / src / Administration / Resources / administration / src / core / factory / template.factory.js View on Github external
getTwigCache,
    disableTwigCache
};

/**
 * Holds a list with all registered component templates.
 * Including registered overrides.
 *
 * @type {Map}
 */
const templateRegistry = new Map();

/**
 * Extends the Twig core for compatibility.
 */
Twig.extend((TwigCore) => {
    /**
     * Remove tokens 2, 3, 4 and 8.
     * This tokens are used for functions and data output.
     * Since the data binding is done in Vue this could lead to syntax issues.
     * We are only using the block system for template inheritance.
     *
     * @type {Array}
     */
    TwigCore.token.definitions = [
        TwigCore.token.definitions[0],
        TwigCore.token.definitions[1],
        TwigCore.token.definitions[5],
        TwigCore.token.definitions[6],
        TwigCore.token.definitions[7],
        TwigCore.token.definitions[9],
        TwigCore.token.definitions[10]
github shopware / administration / Resources / app / administration / build / nuxt-component-library / lib / file-parser / twig-components / index.js View on Github external
const fs = require('fs');
const Twig = require('twig');
const pretty = require('pretty');

Twig.extend((TwigCore) => {
    /**
     * Remove tokens 2, 3, 4 and 8.
     * This tokens are used for functions and data output.
     * Since the data binding is done in Vue this could lead to syntax issues.
     * We are only using the block system for template inheritance.
     *
     * @type {Array}
     */
    TwigCore.token.definitions = [
        TwigCore.token.definitions[0],
        TwigCore.token.definitions[1],
        TwigCore.token.definitions[5],
        TwigCore.token.definitions[6],
        TwigCore.token.definitions[7],
        TwigCore.token.definitions[9],
        TwigCore.token.definitions[10]
github zimmen / gulp-twig / index.js View on Github external
}

        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);
            delete options.extend;
        }

        if (options.useFileContents) {
          var fileContents = file.contents.toString();
          twigOpts.data = fileContents
        }

        template = twig(twigOpts);

        try {
            file.contents = new Buffer(template.render(data));
        }catch(e){
            if (options.errorLogToConsole) {
                gutil.log(PLUGIN_NAME + ' ' + e);
                return cb();