How to use the nunjucks.runtime function in nunjucks

To help you get started, we’ve selected a few nunjucks 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 wellcometrust / wellcomecollection.org / server / extensions / component-jsx.js View on Github external
run(_/* context */, name, model = {}, children = []) {
    try {
      const childrenComponents = children
        .filter(_ => _)
        .map(c => {
          const Component = components[c.name](c.model);
          return React.cloneElement(Component, { key: c.name });
        });

      const Component = components[name];
      const modelWithChildren = Object.assign({}, model, {children: childrenComponents});
      const instantiatedComponent = React.createElement(Component, modelWithChildren);
      const html = ReactDOMServer.renderToString(instantiatedComponent);
      return new nunjucks.runtime.SafeString(html);
    } catch (e) {
      console.error(e);
      throw e;
    }
  };
};
github eggjs / egg-view-nunjucks / lib / environment.js View on Github external
constructor(app) {
    const fileLoader = new FileLoader(app);
    super(fileLoader, app.config.nunjucks);

    this.app = app;

    this[LOAD_FILTER]();

    // monkey patch `escape` with `app.helper.escape` provided by `egg-security` for better performance
    nunjucks.lib.escape = app.Helper.prototype.escape;

    // http://disse.cting.org/2016/08/02/2016-08-02-sandbox-break-out-nunjucks-template-engine
    const originMemberLookup = nunjucks.runtime.memberLookup;
    nunjucks.runtime.memberLookup = function(...args) {
      const val = args[1];
      if (val === 'prototype' || val === 'constructor') return null;
      return originMemberLookup(...args);
    };

    this.ViewHelper = createHelper(app, this.filters);
  }
github The-Politico / generator-politico-interactives / generators / bundler-webpack / templates / server / nunjucks-settings.js View on Github external
const safe = require('nunjucks').runtime.markSafe;
const marked = require('marked');

marked.setOptions({ smartypants: true });

module.exports = {
  markdownFilter: (str, kwargs) => {
    // strip outer <p> tags?
    const strip = typeof kwargs === 'undefined' ?
      false : kwargs.strip || false;
    return !strip ? safe(marked(str)) :
      safe(marked(str).trim().replace(/^</p><p>|&lt;\/p&gt;$/g, ''));
  },
};
</p>
github frctl / fractalite / packages / app / src / render-extension.js View on Github external
// Based on https://github.com/zephraph/nunjucks-markdown

const { SafeString } = require('nunjucks').runtime;

module.exports = function Render() {
  this.tags = ['render'];

  this.parse = function(parser, nodes, lexer) {
    const tok = parser.nextToken();

    // Parse the tag and collect any arguments
    const args = parser.parseSignature(null, true);
    parser.advanceAfterBlockEnd(tok.value);

    // If arguments, return the fileTag constructed node
    if (args.children.length > 0) return new nodes.CallExtensionAsync(this, 'fileTag', args);

    // Otherwise parse until the close block and move the parser to the next position
    const body = parser.parseUntilBlocks('endrender');
github kulfonjs / kulfon / core / LinkExt.js View on Github external
run(context, ...args) {
    const [path, name, rest = { class: '', __exact: true }] = args;
    const currentPath = `/${context.ctx.page.path}`;

    const isExact = rest.__exact;

    if (isExact &amp;&amp; path === currentPath) rest.class = `active ${rest.class}`;
    if (!isExact &amp;&amp; !isEmpty(path.split('/'), currentPath.split('/')))
      rest.class = `active ${rest.class}`;

    const attrs = Object.keys(rest)
      .filter(_ =&gt; !_.startsWith('__'))
      .map(key =&gt; `${key}="${rest[key]}"`)
      .join(' ');

    return new nunjucks.runtime.SafeString(
      `<a href="${path}">${name}</a>`
    );
  }
}
github frctl / fractal / packages / pages / src / engine / helpers / link-to.js View on Github external
helper: function (...args) {
      const page = this.env.pages.find(...args);
      if (!page || !page.permalink) {
        throw new Error(`Could not generate permalink for page`);
      }

      return new nunjucks.runtime.SafeString(`<a href="${page.permalink}">${page.label || page.title}</a>`);
    }
  };
github wellcometrust / wellcomecollection.org / server / extensions / component.js View on Github external
run(context, name, data) {
    const html = this.env.render(`components/${name}/${name}.njk`, data);
    return new nunjucks.runtime.SafeString(html);
  };
};
github fevrcoding / wok / build / gulp-tasks / lib / renderers / nunjucks.js View on Github external
markdownTag.fileTag = (context, file) => {
        return new nunjucks.runtime.SafeString(marked(env.render(file, context)));
    };
github formio / formio / src / worker / tasks / nunjucks.js View on Github external
environment.addFilter('submissionTable', function(obj, components) {
    return new nunjucks.runtime.SafeString(util.renderFormSubmission(obj, components));
  });
github formio / formio / src / util / nunjucks.js View on Github external
environment.addFilter('componentValue', function(obj, key, components) {
  var compValue = util.renderComponentValue(obj, key, components);
  return new nunjucks.runtime.SafeString(compValue.value);
});