This a part of the KDFramework
that handles the compilation of templating operations.
KDPistachio
This package exports both the definition of the Pistachio
templating language and also provides the necessary tools to normalize, parse and compile the Pistachio
templates.
Pistachio
Pistachio
is the templating language of KDFramework
. It uses mustache syntax but rather than focusing on the logical expressions, it only tries to represent a dom structure of a component. Because of that there are no loops, no conditionals, etc.
There are mainly 2 parts of a Pistachio
token: {<markup>{<expression>}}
-
markup
: Emmet-style definition of a wrapper which will wraps the result of thePistachio
expression inDOM
. (e.gspan#dom-id.class-one.class-two
) -
expression
: Any validcoffee-script
expression is acceptable. There are 2 special expressions to make it easier to subviews and represent the values from the data object of the view.- Subview -
{{> <subview> }}
- Use>
character right after the opening curly braces to represent a view instance. - Data -
{{ #(<property>) }}
- Use#()
and pass the name of the property from object's data. Equivalent of@data[<property>]
- Subview -
# {<markup>{<expression>}} pistachio = "{span#foo.bar.baz{#(qux)}}" # which represents a `KDViewNode` instance view = tagName : 'span' domId : 'foo' cssClass : 'bar baz' partial : thisdataqux # new KDTextNode { value: this.data.qux } # which will eventually be used to represent the following dom element # => <span class="bar baz" id="foo">this.data.qux</span>
Tools
Compilation of a Pistachio
template includes 3 steps.
- Normalizing the initial template.
- Parsing the normalized template.
- Compiling the parsed template into function calls.
Normalizer
Step 1: This step includes operations to make template string easier to parse, Such as compiling coffee-script
code into JavaScript
, simplifying Pistachio
expressions.
** Responsibilities of Normalizer
- Compiling
coffee-script
expressions intoJavaScript
expressions.
require 'kdf-pistachio' template = "{{> @view}} {{ functionCall foo }}"normalized = Normalizernormalize template consolelog normalized# => "{{> this.view}}{{functionCall(foo)}}"
- Transforming
Pistachio
expressions with markup into regular expressions with aDOM
element wrapping it.
require 'kdf-pistachio' template = "{article.has-markdown{> view}}"normalized = Normalizernormalize template consolelog normalized# => '<article class="has-markdown">{{> view}}</article>'
- Transforming
Pistachio
data property
expressions.
require 'kdf-pistachio' template = "{{ #(foo)}}"normalized = Normalizernormalize template consolelog normalized# => "{{ this.data.foo}}"
Parser
Step 2: Parser
takes a normalized template string and turns that into regular JavaScript arrays that contains the object representation of each node in Pistachio
template. It makes it easier to traverse over the tree and operate on it. For example, one other tool, Compiler
, uses this parsed output and generates necessary KDViewNode
/KDTextNode
function calls.
require 'kdf-pistachio' normalizedTemplate = """ <div class="foo">Hello World</div> {{> this.view}} """ parsed = Parserparse normalizedTemplateexpectparsedtoEqual type: ParsernodeTypeVIEW_NODE options: tagName: 'div' cssClass: 'foo' children: type: ParsernodeTypeTEXT_NODEoptions: value: "'Hello World'" type: ParsernodeTypePISTACHIO_NODE options: value: '{{> this.view}}'
Compiler
Step 3: Compiler
takes parsed templated, and turns them into KDViewNode
/KDTextNode
object creation calls in JavaScript
. So this is can be added into the build step after coffee-script
to JavaScript
compilation finished.
So if we use the parsed output from last example:
require 'kdf-pistachio' compiled = Compilercompile parsed # the result from last example. ###compiled result:[new KDViewNode({ tagName: 'div' cssClass: 'foo' subviews: [ new KDTextNode({value: 'Hello World'}) ]}), this.view]###
Connecting them all together.
Pistachio
class offers an entry point to all of these tools through its class method Pistachio.compile
. This method takes a regular untouched Pistachio
template string, and it returns Compiler
output, while using Normalizer
and Parser
to feed the compiler with the correct parsed input.
require 'kdf-pistachio' pistachio = """ {div.foo{ doFunStuffWithString 'Hello World' }} {{> @view}} """ compiled = Pistachiocompile pistachio###compiled result:[new KDViewNode({ tagName: 'div' cssClass: 'foo' subviews: [ new KDTextNode({value: doFunStuffWithString('Hello World')}) ]}), this.view]###
Installation
npm install kdf-dom-operations