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