all files / lib/render/ template.js

72.55% Statements 37/51
37.5% Branches 9/24
72.73% Functions 8/11
61.11% Lines 22/36
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123                        19×       19×                                             32×                 68×                                                                     32×                                              
/**
 * Methods for rendering a template.
 */
import nunjucks from 'nunjucks';
import moment from 'moment';
import Url from '../url';
 
let env;
 
/**
 * Render a template with given context variables.
 * @param {string} template  Template name, excluding the extension.
 * @param {object} variables Object of variables to interpolate in the
 *   template.
 * @return {string} Rendered template.
 */
export function render(template, variables) {
  Iif (!env) {
    return '';
  }
 
  return env.render(`${template}.html`, variables);
}
 
/**
 * Render a string template with given context variables.
 * @param {string} str  Template string.
 * @param {object} variables Object of variables to interpolate in the
 *   template.
 * @return {string} Rendered template.
 */
export function renderString(str, variables) {
  Iif (!env) {
    return '';
  }
 
  return env.renderString(str, variables);
}
 
/**
 * Allow adding custom filters to the template engine.
 * @see {@link
 *   http://mozilla.github.io/nunjucks/api#custom-filters
 * @param {string} name Filter name.
 * @param {Function} func Filter function.
 * @param {boolean} async Whether the filter should be async.
 */
export function addFilter(name, func, async = false) {
  env.addFilter(name, func, async);
}
 
/**
 * Adds built in filters to template renderer.
 */
export function addBuiltinFilters() {
  [
    [
      'date',
      function(date, momentTemplate) {
        return moment(date).format(momentTemplate);
      }
    ],
    [
      'slug',
      function(str) {
        return Url.slug(str);
      }
    ],
    [
      'absolute_url',
      function(relativePath, basePath) {
        var base = basePath || '';
        var baseLength = base.length - 1;
        base = base[baseLength] === '/' ? base.substr(0, baseLength) : base;
 
        var input = relativePath || '';
        var inputLength = input.length - 1;
        if (input[inputLength] !== '/') {
          input = input + '/';
        }
        if (input[0] !== '/') {
          input = '/' + input;
        }
 
        return base + input;
      }
    ],
    [
      'limit',
      function(arr, length) {
        return arr.slice(0, length);
      }
    ]
  ].forEach(filter => {
    addFilter.apply(null, filter);
  });
}
 
/**
 * Exposed method to configure template engine.
 * @param {Object} options Options object with following properties:
 *   @param {string|Array.<string>} paths Either an array of paths or a singular
 *     path that we can load templates from.
 *   @param {boolean} cacheTemplates Whether our template engine should cache
 *     its templates. Only set to false when in watch mode.
 */
export function configure({
  paths,
  cacheTemplates = false
}) {
  let fileSystemLoader = new nunjucks.FileSystemLoader(paths, {
    noCache: cacheTemplates
  });
  env = new nunjucks.Environment(fileSystemLoader, {
    autoescape: false
  });
 
  addBuiltinFilters();
}
 
export const ErrorMessage = {
  NO_TEMPLATE: 'template not found'
};