@itrocks/template 0.0.25 → 0.0.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -377,13 +377,27 @@ Result:
377
377
 
378
378
  ### Including Another Template
379
379
 
380
- Any expression starting with `./` or `../` is considered a template include:
380
+ Any expression starting with `/`, `./` or `../` is considered a template include:
381
381
  ```html
382
382
  <div>
383
383
  {./another-template.html}
384
384
  </div>
385
385
  ```
386
386
 
387
+ The default contextual data is the one in the current scope.
388
+
389
+ You can pass parent or sub-data to your included template as an alternative context:
390
+ ```html
391
+ <div>
392
+ <!--subData-->
393
+ {./another-template.html-}
394
+ {./another-template.html(-)}
395
+ <!--end-->
396
+ {./another-template.html(subData)}
397
+ </div>
398
+ ```
399
+ In this example, `-` refers to the parent block context. Parentheses are optional in this specific case.
400
+
387
401
  #### Delimiting Rendered Content in an Included Template
388
402
 
389
403
  To keep templates W3C-compliant and browser-viewable, each HTML template may contain full HTML structure.
package/cjs/template.js CHANGED
@@ -177,7 +177,11 @@ class Template {
177
177
  }
178
178
  this.headLinks.push(...template.headLinks);
179
179
  this.headTitle = template.headTitle;
180
- return parsed.substring(parsed.indexOf('<!--BEGIN-->') + 12, parsed.indexOf('<!--END-->'));
180
+ const beginPosition = parsed.indexOf('<!--BEGIN-->');
181
+ const endPosition = parsed.indexOf('<!--END-->');
182
+ return (beginPosition > -1)
183
+ ? parsed.slice(beginPosition + 12, (endPosition > -1) ? endPosition : parsed.length)
184
+ : parsed;
181
185
  }
182
186
  isContextClean() {
183
187
  const clean = this.getCleanContext();
@@ -301,7 +305,7 @@ class Template {
301
305
  if (conditional && !parsed) {
302
306
  if ((typeof this.target)[0] === 's') {
303
307
  this.target = this.target.substring(0, this.target.lastIndexOf(' '));
304
- while ((this.index < this.length) && !' \n\r\t\f'.includes(this.source[this.index])) {
308
+ while ((this.index < this.length) && !' >\n\r\t\f'.includes(this.source[this.index])) {
305
309
  this.index++;
306
310
  this.start++;
307
311
  }
@@ -344,7 +348,23 @@ class Template {
344
348
  if (expression === '') {
345
349
  return undefined;
346
350
  }
347
- if ((expression[0] === '.') && (expression.startsWith('./') || expression.startsWith('../'))) {
351
+ if (((expression[0] === '.') && ((expression[1] === '/') || ((expression[1] === '.') && (expression[2] === '/'))))
352
+ || (expression[0] === '/')) {
353
+ let expressionEnd = expression.length - 1;
354
+ if (expression[expressionEnd] === '-') {
355
+ let blockBack = 1;
356
+ expressionEnd--;
357
+ while (expression[expressionEnd] === '-') {
358
+ blockBack++;
359
+ expressionEnd--;
360
+ }
361
+ const blockStack = this.blockStack;
362
+ return this.include(expression.slice(0, expressionEnd), blockStack[blockStack.length - blockBack].data);
363
+ }
364
+ if (expression[expressionEnd] === ')') {
365
+ const openPosition = expression.lastIndexOf('(');
366
+ return this.include(expression.slice(0, openPosition), await this.parsePath(expression.slice(openPosition + 1, expression.length - 1), data));
367
+ }
348
368
  return this.include(expression, data);
349
369
  }
350
370
  this.blockBack = 0;
@@ -355,7 +375,7 @@ class Template {
355
375
  }
356
376
  async parseVariable(variable, data) {
357
377
  if (variable === '') {
358
- return (typeof data === 'function')
378
+ return (((typeof data)[0] === 'f') && ((data + '')[0] !== 'c'))
359
379
  ? data.call()
360
380
  : data;
361
381
  }
@@ -383,7 +403,7 @@ class Template {
383
403
  data = new rename_1.default(data);
384
404
  }
385
405
  let value = data[variable];
386
- return ((typeof value === 'function') && !value.prototype)
406
+ return (((typeof value)[0] === 'f') && ((value + '')[0] !== 'c'))
387
407
  ? value.call(data)
388
408
  : value;
389
409
  }
@@ -457,7 +477,6 @@ class Template {
457
477
  continue;
458
478
  }
459
479
  // begin condition / loop block
460
- this.blockStack.push({ blockStart, collection, data, iteration, iterations });
461
480
  if (tagIndex > this.start) {
462
481
  this.target += this.trimEndLine(this.source.substring(this.start, tagIndex));
463
482
  this.start = tagIndex;
@@ -468,6 +487,7 @@ class Template {
468
487
  this.target = '';
469
488
  this.inLiteral = false;
470
489
  const condition = await this.parseExpression(data, '}', '-->');
490
+ this.blockStack.push({ blockStart, collection, data, iteration, iterations });
471
491
  let blockData = condition ? (this.target ? data : undefined) : this.target;
472
492
  blockStart = this.index;
473
493
  iteration = 0;
package/esm/template.js CHANGED
@@ -172,7 +172,11 @@ export default class Template {
172
172
  }
173
173
  this.headLinks.push(...template.headLinks);
174
174
  this.headTitle = template.headTitle;
175
- return parsed.substring(parsed.indexOf('<!--BEGIN-->') + 12, parsed.indexOf('<!--END-->'));
175
+ const beginPosition = parsed.indexOf('<!--BEGIN-->');
176
+ const endPosition = parsed.indexOf('<!--END-->');
177
+ return (beginPosition > -1)
178
+ ? parsed.slice(beginPosition + 12, (endPosition > -1) ? endPosition : parsed.length)
179
+ : parsed;
176
180
  }
177
181
  isContextClean() {
178
182
  const clean = this.getCleanContext();
@@ -296,7 +300,7 @@ export default class Template {
296
300
  if (conditional && !parsed) {
297
301
  if ((typeof this.target)[0] === 's') {
298
302
  this.target = this.target.substring(0, this.target.lastIndexOf(' '));
299
- while ((this.index < this.length) && !' \n\r\t\f'.includes(this.source[this.index])) {
303
+ while ((this.index < this.length) && !' >\n\r\t\f'.includes(this.source[this.index])) {
300
304
  this.index++;
301
305
  this.start++;
302
306
  }
@@ -339,7 +343,23 @@ export default class Template {
339
343
  if (expression === '') {
340
344
  return undefined;
341
345
  }
342
- if ((expression[0] === '.') && (expression.startsWith('./') || expression.startsWith('../'))) {
346
+ if (((expression[0] === '.') && ((expression[1] === '/') || ((expression[1] === '.') && (expression[2] === '/'))))
347
+ || (expression[0] === '/')) {
348
+ let expressionEnd = expression.length - 1;
349
+ if (expression[expressionEnd] === '-') {
350
+ let blockBack = 1;
351
+ expressionEnd--;
352
+ while (expression[expressionEnd] === '-') {
353
+ blockBack++;
354
+ expressionEnd--;
355
+ }
356
+ const blockStack = this.blockStack;
357
+ return this.include(expression.slice(0, expressionEnd), blockStack[blockStack.length - blockBack].data);
358
+ }
359
+ if (expression[expressionEnd] === ')') {
360
+ const openPosition = expression.lastIndexOf('(');
361
+ return this.include(expression.slice(0, openPosition), await this.parsePath(expression.slice(openPosition + 1, expression.length - 1), data));
362
+ }
343
363
  return this.include(expression, data);
344
364
  }
345
365
  this.blockBack = 0;
@@ -350,7 +370,7 @@ export default class Template {
350
370
  }
351
371
  async parseVariable(variable, data) {
352
372
  if (variable === '') {
353
- return (typeof data === 'function')
373
+ return (((typeof data)[0] === 'f') && ((data + '')[0] !== 'c'))
354
374
  ? data.call()
355
375
  : data;
356
376
  }
@@ -378,7 +398,7 @@ export default class Template {
378
398
  data = new Str(data);
379
399
  }
380
400
  let value = data[variable];
381
- return ((typeof value === 'function') && !value.prototype)
401
+ return (((typeof value)[0] === 'f') && ((value + '')[0] !== 'c'))
382
402
  ? value.call(data)
383
403
  : value;
384
404
  }
@@ -452,7 +472,6 @@ export default class Template {
452
472
  continue;
453
473
  }
454
474
  // begin condition / loop block
455
- this.blockStack.push({ blockStart, collection, data, iteration, iterations });
456
475
  if (tagIndex > this.start) {
457
476
  this.target += this.trimEndLine(this.source.substring(this.start, tagIndex));
458
477
  this.start = tagIndex;
@@ -463,6 +482,7 @@ export default class Template {
463
482
  this.target = '';
464
483
  this.inLiteral = false;
465
484
  const condition = await this.parseExpression(data, '}', '-->');
485
+ this.blockStack.push({ blockStart, collection, data, iteration, iterations });
466
486
  let blockData = condition ? (this.target ? data : undefined) : this.target;
467
487
  blockStart = this.index;
468
488
  iteration = 0;
package/package.json CHANGED
@@ -67,5 +67,5 @@
67
67
  "test": "jest"
68
68
  },
69
69
  "types": "./esm/template.d.ts",
70
- "version": "0.0.25"
70
+ "version": "0.0.26"
71
71
  }