all files / liquidjs/tags/ include.js

100% Statements 28/28
100% Branches 6/6
100% Functions 3/3
100% Lines 28/28
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   107×   15× 15× 14×   14× 14×       14× 14× 13×     14× 14× 14× 14×   14×   14×   14× 14×     13× 13× 13× 13×          
const Liquid = require('..')
const lexical = Liquid.lexical
const withRE = new RegExp(`with\\s+(${lexical.value.source})`)
const assert = require('../src/util/assert.js')
 
module.exports = function (liquid) {
  liquid.registerTag('include', {
    parse: function (token) {
      var match = lexical.value.exec(token.args)
      assert(match, `illegal token ${token.raw}`)
      this.value = match[0]
 
      match = withRE.exec(token.args)
      if (match) {
        this.with = match[1]
      }
    },
    render: function (scope, hash) {
      var filepath = this.value
      if (scope.opts.dynamicPartials) {
        filepath = Liquid.evalValue(this.value, scope)
      }
 
      var originBlocks = scope.opts.blocks
      var originBlockMode = scope.opts.blockMode
      scope.opts.blocks = {}
      scope.opts.blockMode = 'output'
 
      if (this.with) {
        hash[filepath] = Liquid.evalValue(this.with, scope)
      }
      return liquid.getTemplate(filepath, scope.opts.root)
        .then((templates) => {
          scope.push(hash)
          return liquid.renderer.renderTemplates(templates, scope)
        })
        .then((html) => {
          scope.pop()
          scope.opts.blocks = originBlocks
          scope.opts.blockMode = originBlockMode
          return html
        })
    }
  })
}