@kosatyi/ejs 0.0.100 → 0.0.102

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/dist/esm/index.js CHANGED
@@ -418,7 +418,7 @@ class Compiler {
418
418
  source = `${BUFFER}.start();${source}return ${BUFFER}.end();`;
419
419
  source += `\n//# sourceURL=${path}`;
420
420
  let result = null;
421
- let params = [SCOPE, BUFFER, SAFE, COMPONENT, ELEMENT].concat(GLOBALS);
421
+ let params = [SCOPE, COMPONENT, ELEMENT, BUFFER, SAFE].concat(GLOBALS);
422
422
  try {
423
423
  result = Function.apply(null, params.concat(source));
424
424
  result.source = `(function(${params.join(',')}){\n${source}\n});`;
@@ -586,6 +586,8 @@ function createBuffer() {
586
586
  return buffer
587
587
  }
588
588
 
589
+ const PARENT = Symbol('ContextScope.parentTemplate');
590
+
589
591
  const createContextScope = (config, methods) => {
590
592
  const {
591
593
  BLOCKS,
@@ -596,14 +598,20 @@ const createContextScope = (config, methods) => {
596
598
  SAFE,
597
599
  SCOPE,
598
600
  COMPONENT,
599
- ELEMENT,
601
+ ELEMENT
600
602
  } = config.vars;
603
+ /**
604
+ *
605
+ * @type {symbol}
606
+ */
607
+
601
608
  /**
602
609
  * @name ContextScope
603
610
  * @param data
604
611
  * @constructor
605
612
  */
606
613
  function ContextScope(data) {
614
+ this[PARENT] = null;
607
615
  this[BLOCKS] = {};
608
616
  this[MACRO] = {};
609
617
  Object.assign(
@@ -613,28 +621,45 @@ const createContextScope = (config, methods) => {
613
621
  }
614
622
  Object.assign(ContextScope.prototype, methods);
615
623
  Object.defineProperty(ContextScope.prototype, BUFFER, {
616
- value: createBuffer(),
624
+ value: createBuffer()
617
625
  });
618
626
  Object.defineProperty(ContextScope.prototype, BLOCKS, {
619
627
  value: {},
620
- writable: true,
628
+ writable: true
621
629
  });
622
630
  Object.defineProperty(ContextScope.prototype, MACRO, {
623
631
  value: {},
624
- writable: true,
632
+ writable: true
625
633
  });
626
634
  Object.defineProperty(ContextScope.prototype, LAYOUT, {
627
635
  value: false,
628
- writable: true,
636
+ writable: true
629
637
  });
630
638
  Object.defineProperty(ContextScope.prototype, EXTEND, {
631
639
  value: false,
632
- writable: true,
640
+ writable: true
641
+ });
642
+ Object.defineProperty(ContextScope.prototype, PARENT, {
643
+ value: null,
644
+ writable: true
633
645
  });
634
646
  Object.defineProperties(ContextScope.prototype, {
647
+ /** @type {function} */
648
+ setParentTemplate:{
649
+ value(value){
650
+ this[PARENT] = value;
651
+ return this
652
+ }
653
+ },
654
+ /** @type {function} */
655
+ getParentTemplate: {
656
+ value(){
657
+ return this[PARENT]
658
+ }
659
+ },
635
660
  /** @type {function} */
636
661
  useSafeValue: {
637
- get: () => safeValue,
662
+ get: () => safeValue
638
663
  },
639
664
  /** @type {function} */
640
665
  useComponent: {
@@ -646,7 +671,7 @@ const createContextScope = (config, methods) => {
646
671
  throw new Error(`${COMPONENT} must be a function`)
647
672
  }
648
673
  }
649
- },
674
+ }
650
675
  },
651
676
  /** @type {function} */
652
677
  useElement: {
@@ -658,49 +683,51 @@ const createContextScope = (config, methods) => {
658
683
  throw new Error(`${ELEMENT} must be a function`)
659
684
  }
660
685
  }
661
- },
686
+ }
662
687
  },
663
688
  /** @type {()=>this[MACRO]} */
664
689
  getMacro: {
665
690
  value() {
666
691
  return this[MACRO]
667
- },
692
+ }
668
693
  },
669
694
  /** @type {function} */
670
695
  getBuffer: {
671
696
  value() {
672
697
  return this[BUFFER]
673
- },
698
+ }
674
699
  },
675
700
  /** @type {function} */
676
701
  getBlocks: {
677
702
  value() {
678
703
  return this[BLOCKS]
679
- },
704
+ }
680
705
  },
681
706
  /** @type {function} */
682
707
  setExtend: {
683
708
  value(value) {
684
709
  this[EXTEND] = value;
685
- },
710
+ return this
711
+ }
686
712
  },
687
713
  /** @type {function} */
688
714
  getExtend: {
689
715
  value() {
690
716
  return this[EXTEND]
691
- },
717
+ }
692
718
  },
693
719
  /** @type {function} */
694
720
  setLayout: {
695
721
  value(layout) {
696
722
  this[LAYOUT] = layout;
697
- },
723
+ return this
724
+ }
698
725
  },
699
726
  /** @type {function} */
700
727
  getLayout: {
701
728
  value() {
702
729
  return this[LAYOUT]
703
- },
730
+ }
704
731
  },
705
732
  /** @type {function} */
706
733
  clone: {
@@ -710,14 +737,14 @@ const createContextScope = (config, methods) => {
710
737
  filter.push(BLOCKS);
711
738
  }
712
739
  return omit(this, filter)
713
- },
740
+ }
714
741
  },
715
742
  /** @type {function} */
716
743
  extend: {
717
744
  value(layout) {
718
745
  this.setExtend(true);
719
746
  this.setLayout(layout);
720
- },
747
+ }
721
748
  },
722
749
  /** @type {function} */
723
750
  echo: {
@@ -725,21 +752,21 @@ const createContextScope = (config, methods) => {
725
752
  const buffer = this.getBuffer();
726
753
  const params = [].slice.call(arguments);
727
754
  params.forEach(buffer);
728
- },
755
+ }
729
756
  },
730
757
  /** @type {function} */
731
758
  fn: {
732
759
  value(callback) {
733
760
  const buffer = this.getBuffer();
734
761
  const context = this;
735
- return function () {
762
+ return function() {
736
763
  if (isFunction(callback)) {
737
764
  buffer.backup();
738
765
  buffer(callback.apply(context, arguments));
739
766
  return buffer.restore()
740
767
  }
741
768
  }
742
- },
769
+ }
743
770
  },
744
771
  /** @type {function} */
745
772
  macro: {
@@ -747,10 +774,10 @@ const createContextScope = (config, methods) => {
747
774
  const list = this.getMacro();
748
775
  const macro = this.fn(callback);
749
776
  const context = this;
750
- list[name] = function () {
777
+ list[name] = function() {
751
778
  return context.echo(macro.apply(undefined, arguments))
752
779
  };
753
- },
780
+ }
754
781
  },
755
782
  /** @type {function} */
756
783
  call: {
@@ -761,7 +788,7 @@ const createContextScope = (config, methods) => {
761
788
  if (isFunction(macro)) {
762
789
  return macro.apply(macro, params)
763
790
  }
764
- },
791
+ }
765
792
  },
766
793
  /** @type {function} */
767
794
  block: {
@@ -785,13 +812,13 @@ const createContextScope = (config, methods) => {
785
812
  }
786
813
  };
787
814
  this.echo(current()(next()));
788
- },
815
+ }
789
816
  },
790
817
  /** @type {function} */
791
818
  hasBlock: {
792
819
  value(name) {
793
820
  return this.getBlocks().hasOwnProperty(name)
794
- },
821
+ }
795
822
  },
796
823
  /** @type {function} */
797
824
  include: {
@@ -800,7 +827,7 @@ const createContextScope = (config, methods) => {
800
827
  const params = extend(context, data || {});
801
828
  const promise = this.render(path, params);
802
829
  this.echo(promise);
803
- },
830
+ }
804
831
  },
805
832
  /** @type {function} */
806
833
  use: {
@@ -808,18 +835,18 @@ const createContextScope = (config, methods) => {
808
835
  this.echo(
809
836
  Promise.resolve(this.require(path)).then((exports) => {
810
837
  const list = this.getMacro();
811
- each(exports, function (macro, name) {
838
+ each(exports, function(macro, name) {
812
839
  list[[namespace, name].join('.')] = macro;
813
840
  });
814
841
  })
815
842
  );
816
- },
843
+ }
817
844
  },
818
845
  /** @type {function} */
819
846
  async: {
820
847
  value(promise, callback) {
821
848
  this.echo(Promise.resolve(promise).then(callback));
822
- },
849
+ }
823
850
  },
824
851
  /** @type {function} */
825
852
  get: {
@@ -828,29 +855,29 @@ const createContextScope = (config, methods) => {
828
855
  const result = path.shift();
829
856
  const prop = path.pop();
830
857
  return hasProp(result, prop) ? result[prop] : defaults
831
- },
858
+ }
832
859
  },
833
860
  /** @type {function} */
834
861
  set: {
835
862
  value(name, value) {
836
863
  const path = getPath(this, name, false);
837
- const result = path.shift();
864
+ const result= path.shift();
838
865
  const prop = path.pop();
839
- if (this.getExtend() && hasProp(result, prop)) {
866
+ if (this.getParentTemplate() && hasProp(result, prop)) {
840
867
  return result[prop]
841
868
  }
842
869
  return (result[prop] = value)
843
- },
870
+ }
844
871
  },
845
872
  /** @type {function} */
846
873
  each: {
847
- value: function (object, callback) {
874
+ value: function(object, callback) {
848
875
  if (isString(object)) {
849
876
  object = this.get(object, []);
850
877
  }
851
878
  each(object, callback);
852
879
  },
853
- writable: true,
880
+ writable: true
854
881
  },
855
882
  /** @type {function} */
856
883
  el: {
@@ -862,28 +889,33 @@ const createContextScope = (config, methods) => {
862
889
  )
863
890
  );
864
891
  },
865
- writable: true,
892
+ writable: true
866
893
  },
867
894
  /** @type {function} */
868
895
  ui: {
869
- value(layout) {},
870
- writable: true,
871
- },
896
+ value(layout) {
897
+ },
898
+ writable: true
899
+ }
872
900
  });
873
901
  return ContextScope
874
902
  };
875
903
 
876
904
  class Context {
877
905
  #scope
906
+
878
907
  constructor(config, methods) {
879
908
  this.configure(config, methods);
880
909
  }
910
+
881
911
  create(data) {
882
912
  return new this.#scope(data)
883
913
  }
914
+
884
915
  configure(config, methods) {
885
916
  this.#scope = createContextScope(config, methods);
886
917
  }
918
+
887
919
  helpers(methods) {
888
920
  extend(this.#scope.prototype, methods || {});
889
921
  }
@@ -896,6 +928,7 @@ class EJS {
896
928
  #compiler
897
929
  #cache
898
930
  #template
931
+
899
932
  constructor(options) {
900
933
  configSchema(this.#config, options || {});
901
934
  this.#context = new Context(this.#config, this.#extend);
@@ -903,19 +936,11 @@ class EJS {
903
936
  this.#cache = new Cache(this.#config);
904
937
  this.#template = new Template(this.#config, this.#cache, this.#compiler);
905
938
  //
906
- bindContext(this, [
907
- 'configure',
908
- 'create',
909
- 'render',
910
- 'require',
911
- 'context',
912
- 'preload',
913
- 'compile',
914
- 'helpers',
915
- ]);
939
+ bindContext(this, ['configure', 'create', 'render', 'require', 'context', 'preload', 'compile', 'helpers']);
916
940
  //
917
941
  this.helpers({ require: this.require, render: this.render });
918
942
  }
943
+
919
944
  configure(options) {
920
945
  configSchema(this.#config, options || {});
921
946
  this.#context.configure(this.#config, this.#extend);
@@ -924,55 +949,66 @@ class EJS {
924
949
  this.#template.configure(this.#config);
925
950
  return this.#config
926
951
  }
952
+
953
+ filePath(name) {
954
+ return ext(name, this.#config.extension)
955
+ }
956
+
957
+ require(name) {
958
+ const scope = this.context({});
959
+ return this.#output(this.filePath(name), scope).then(() => scope.getMacro())
960
+ }
961
+
927
962
  render(name, data) {
928
- const filepath = ext(name, this.#config.extension);
929
963
  const scope = this.context(data);
930
- return this.#output(filepath, scope).then((content) => {
964
+ return this.#output(this.filePath(name), scope).then(this.outputContent(name, scope))
965
+ }
966
+
967
+ outputContent(name, scope) {
968
+ return (content) => {
931
969
  if (scope.getExtend()) {
932
970
  scope.setExtend(false);
933
- const layout = scope.getLayout();
934
- const data = scope.clone();
935
- return this.render(layout, data)
971
+ return this.renderLayout(scope.getLayout(), scope, name)
936
972
  }
937
973
  return content
938
- })
974
+ }
975
+ }
976
+
977
+ renderLayout(name, data, parent) {
978
+ const scope = this.context(data);
979
+ if (parent) scope.setParentTemplate(parent);
980
+ return this.#output(this.filePath(name), scope).then(this.outputContent(name, scope))
939
981
  }
982
+
940
983
  helpers(methods) {
941
984
  this.#context.helpers(extend(this.#extend, methods));
942
985
  }
986
+
943
987
  context(data) {
944
988
  return this.#context.create(data)
945
989
  }
990
+
946
991
  compile(content, path) {
947
992
  return this.#compiler.compile(content, path)
948
993
  }
994
+
949
995
  preload(list) {
950
996
  return this.#cache.load(list || {})
951
997
  }
998
+
952
999
  create(options) {
953
1000
  return new this.constructor(options)
954
1001
  }
955
- require(name) {
956
- const filepath = ext(name, this.#config.extension);
957
- const scope = this.context({});
958
- return this.#output(filepath, scope).then(() => scope.getMacro())
959
- }
1002
+
960
1003
  #output(path, scope) {
961
1004
  const { globalHelpers } = this.#config;
962
- const params = [
963
- scope,
964
- scope.getBuffer(),
965
- scope.useSafeValue,
966
- scope.useComponent,
967
- scope.useElement,
968
- ].concat(
969
- globalHelpers
970
- .filter((name) => isFunction(scope[name]))
971
- .map((name) => scope[name].bind(scope))
972
- );
1005
+ const params = [scope, scope.useComponent, scope.useElement, scope.getBuffer(), scope.useSafeValue];
1006
+ const globals = globalHelpers
1007
+ .filter((name) => isFunction(scope[name]))
1008
+ .map((name) => scope[name].bind(scope));
973
1009
  return this.#template
974
1010
  .get(path)
975
- .then((callback) => callback.apply(scope, params))
1011
+ .then((callback) => callback.apply(scope, params.concat(globals)))
976
1012
  }
977
1013
  }
978
1014