!function () {

var _isArray = Array.isArray || function(obj) {
  return Object.prototype.toString.call(obj) === '[object Array]'
}

// print value
function print(value, escape) {
  return typeof value === 'undefined'
  ? '' // placeholder
  : ( escape && /[&"<>]/.test(value += '') )
  // escape HTML chars http://www.w3.org/TR/html4/charset.html#h-5.3.2
  ? value.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
  : value
}

// get defined value from context stack
function get(scope, depth) {
  scope = scope
  .replace(/\s/g, '') // clear space
  .replace(/^(\.\.\/)+/, function (all, one) {
    depth -= all.length / one.length // parent path
    return ''
  })
  if (depth < 0) return "undefined"
  if (scope === '.') return "s" +depth // this
  var first = scope.match(/^[^.]+/)[0]  // nest path support, extract first scope for context finding
  var code = ''
  while (depth > 0) code += "typeof s" +depth +"." +first +" !== 'undefined' ? s" +depth-- +"." +scope +" : "
  return code +"s0." +scope
}

// core
function compile(source) {
  var inverted = 0, depth = 0 // context stack depth
  var compiled = new Function("s0", "print", "_isArray", "var tmp, out = '" +source
  .replace(/\\/g, "\\\\") // escape \
  .replace(/'/g, "\\'")   // escape '
  .replace(/\{\{([\^#/!&]?)([^{\n]+?)\}\}/g, function (a, flag, scope) { // block
    switch (flag) {
    case '^':   // if not
      inverted++
      return "'; tmp = " +get(scope, depth)
           + " ; if (!tmp || (_isArray(tmp) && tmp.length === 0)) { out += '"
    case '#':   // if/each/TODO lambdas/TODO helper
      return "'; tmp = " +get(scope, depth)
           + " ; var list" +depth +" = tmp ? _isArray(tmp) ? tmp : [tmp] : []"
           + " ; for (var i" +depth +"=0, len" +depth +"=list" +depth +".length"
           + " ;      i" +depth +"<len" +depth +"; i" +depth +"++)"
           + " { var s" +(depth+1) +" = list" +depth +"[i" +depth++ +"]; out += '"
    case '/':   // close
      inverted > 0 ? inverted-- : depth--
      return "'} out += '"
    case '!':   // comments
      return ""
    //case '>':   // TODO partials
    case '&':   // print noescape
      return "' +print(" +get(scope, depth) +", false) +'"
    default :   // print escape
      return "' +print(" +get(scope, depth) +", true) +'"
    }
  })
  .replace(/\n/g, "\\n") // escape CR
  .replace(/\r/g, "\\r") // escape LF
  +"'; return out")

  var render = function (data) {
    return compiled(data, print, _isArray)
  }
  return render.render = render // render api
}
compile.compile = compile // compile api

// export
if (typeof define === 'function' && define.amd) define(function () { return compile })
else if (typeof module === 'object' && module.exports) module.exports = compile
else this.lt = compile

}()

@@ -703,16 +703,19 @@
 ''%0A  %7D)%0A
+  %0A
   if (de
@@ -741,16 +741,34 @@
 defined%22
+ // out of context
 %0A  if (s
@@ -817,139 +817,586 @@
 var 
-first = scope.match(/%5E%5B%5E.%5D+/)%5B0%5D  // nest path support, extract first scope for context finding%0A  var
+scopeParts = scope.split('.')%0A  var code = ''%0A  while (depth %3E 0) %7B%0A    var scopeSegment = scopeParts%5B0%5D%0A%0A    // parent context searching%0A    code += %22typeof s%22 +depth +%22.%22 +scopeSegment +%22 !== 'undefined' ? %22%0A%0A    // nested path context searching%0A    for (var i=1, len=scopeParts.length; i%3Clen; i++) %7B%0A      code += %22typeof s%22 +depth +%22.%22 +scopeSegment +%22 === 'undefined' ? undefined : %22%0A      scopeSegment += %22.%22 +scopeParts%5Bi%5D%0A    %7D%0A   
  code 
++
 = 
-''%0A  while (depth %3E 0)
+%22s%22 +depth +%22.%22 +scopeSegment +%22 : %22%0A%0A    depth--%0A  %7D%0A    var scopeSegment = scopeParts%5B0%5D%0A%0A    // parent context searching%0A    //
  cod
@@ -1428,12 +1428,19 @@
 .%22 +
-firs
+scopeSegmen
 t +%22
@@ -1457,16 +1457,133 @@
 fined' ?
+ %22%0A%0A    // nested path context searching%0A    for (var i=1, len=scopeParts.length; i%3Clen; i++) %7B%0A      code += %22typeof
  s%22 +dep
@@ -1584,18 +1584,16 @@
 %22 +depth
---
  +%22.%22 +s
@@ -1600,42 +1600,163 @@
 cope
- +%22 : %22%0A  return code +%22s0.%22 +scop
+Segment +%22 === 'undefined' ? undefined : %22%0A      scopeSegment += %22.%22 +scopeParts%5Bi%5D%0A    %7D%0A    code += %22s%22 +depth +%22.%22 +scopeSegment // +%22 : %22%0A%0A  return cod
 e%0A%7D%0A

support nested undef