1 module handlebars.components.base;
2 
3 public import handlebars.tokens;
4 public import handlebars.lifecycle;
5 public import handlebars.properties;
6 
7 ///
8 class RenderComponentException : Exception {
9   pure nothrow @nogc @safe this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable nextInChain = null) {
10     super(msg, file, line, nextInChain);
11   }
12 }
13 
14 alias OnYield = string delegate(Token[]);
15 alias OnEvaluateBoolean = bool delegate(string);
16 alias OnEvaluateLong = long delegate(string);
17 
18 ///
19 interface IHbsComponent {
20   void content(Token[]);
21   void lifecycle(Lifecycle);
22 
23   Token[] content();
24   Lifecycle lifecycle();
25 
26   string yield();
27 }
28 
29 ///
30 abstract class HbsComponent(string tpl = "{{yield}}") : IHbsComponent {
31   ///
32   private {
33     Token[] _content;
34     Lifecycle _lifecycle;
35   }
36 
37   void content(Token[] value) {
38     this._content = value;
39   }
40 
41   void lifecycle(Lifecycle value) {
42     this._lifecycle = value;
43   }
44 
45   Token[] content() {
46     return this._content;
47   }
48 
49   Lifecycle lifecycle() {
50     return this._lifecycle;
51   }
52 
53   ///
54   string yield() {
55     if(this.content.length == 0) {
56       return "";
57     }
58 
59     return this.lifecycle.yield(this.content);
60   }
61 
62   string render(Component, Components...)() {
63     import handlebars.tpl;
64     return handlebars.tpl.render!(tpl, Component, Components)(cast(Component) this);
65   }
66 }