@kosatyi/ejs 0.0.99 → 0.0.101

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