2.0.0 • Published 6 years ago

templata v2.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

Templata (Template Compiler)

build status coverage code climate issue count dependencies development dependencies npm npm version GitHub version license

FAQ

Q: How can I create a custom helper?
A: You should take a look at "src/helper" there are several example implementations.

Q: How can I create a custom filter?
A: You should take a look at "src/filter" there are several example implementations.

Q: Why a template compiler with out any logic itself?
A: I needed a simple modifiable parser which allows me to create my own logic.

Q: Why should I "import" functions into the template?
A: You don't need to. But I recommend to just pass a reference to your function instead of passing your function source

Compiler API

class Compiler {
    static settings: CompilerSettings
    constructor(imports?: Object, helper?: Object, filter?: Object, provider?: Object)
    registerImport(name: string, imports: any): Compiler
    hasImport(name: string): boolean
    removeImport(name: string): Compiler
    registerHelper(operator: string, callback: Helper): Compiler
    hasHelper(operator: string): boolean
    removeHelper(operator: string): Compiler
    registerFilter(name: string, callback: Filter): Compiler
    hasFilter(name: string): boolean
    removeFilter(name: string): Compiler
    registerProvider(name: string, callback: Provider): Compiler
    hasProvider(name: string): boolean
    removeProvider(name: string): Compiler
    callProvider(name: string, ...args: any[]): any
    on(name: string, callback: Listener): Compiler
    off(name: string, callback: Listener): Compiler
    dispatch(name: string, ...data: any[]): void
    initialize(fn: InitializeFunction): Compiler
    compile(template: string): (data: Object) => string
}

interface Buffer extends Object {
    POST_APPEND: string
    APPEND: string
    START: string
    END: string
}

interface CompilerSettings extends Object {
    VARIABLE_NAME: string
    VARIABLE_PRINT: string
    DELIMITER: {
        FILTER_SEPERATOR: string
        OPENING_BLOCK: string
        CLOSING_BLOCK: string
        CLOSING: string
        SPACE: string
    }
}

interface Filter extends Function {
    (name: string, input: string, buffer: Templata.Object.Buffer, compiler: Compiler): string
}

interface Helper extends Function {
    (operator: string, parameter: string, selfClosing: boolean, closingTag: boolean, buffer: Templata.Object.Buffer, compiler: Compiler): string
}

interface Provider extends Function {
    (name: string, ...args: any[]): void
}

interface Listener extends Function {
    (name: string, compiler: Compiler, ...data: any[]): void
}

interface CompileFunction extends Function {
    (data: Object): string
}

interface InitializeFunction extends Function {
    (compiler: Compiler): void
}

Default helper (optional)

Interpolation

<!-- render name -->
<span class="name">{{= local.name =}}</span>
<!-- render HTML-Encoded adCode -->
<input type="text" value="{{! local.adCode !}}">
<!-- use filter -->
<span class="name">{{= local.name | lowercase =}}</span>
<span class="name">{{= local.name | uppercase =}}</span>
<span class="price">{{= local.price | currency =}}</span>

Conditional

<!-- if(condition) -->
{{? true ?}}
<!-- else if(condition) -->
{{?? false ??}}
<!-- else -->
{{? ?}}
<!-- close if -->
{{/?}}

Iteration

<!-- start loop -->
{{~ local.arrayOrObject :value,key: ~}}
<!-- use value or key in here -->
{{= key =}}: {{= value =}}
<!-- end loop -->
{{/~}}

JavaScript

<!-- define custom variable -->
{{- var variable = 'example content'; -}}
<!-- do whate ever you want -->
{{- function localFn(x, y){return x > y;} -}}
<!-- use your custom code -->
<!-- prints "EXAMPLE CONTENT" -->
{{= variable | uppercase =}}
<!-- prints "5 is bigger then 2" -->
{{? localFn(5,2) ?}}{{= '5 is bigger then 2' =}}{{/?}}

Comments

{{* comment which is not visible after compilation *}}
2.0.0

6 years ago

1.3.32

7 years ago

1.3.30

7 years ago

1.3.29

7 years ago

1.3.28

7 years ago

1.3.27

7 years ago

1.3.26

7 years ago

1.3.25

7 years ago

1.3.24

7 years ago

1.3.22

8 years ago

1.3.21

8 years ago

1.3.20

8 years ago

1.3.19

8 years ago

1.3.18

8 years ago

1.3.17

8 years ago

1.3.16

8 years ago

1.3.15

8 years ago

1.3.14

8 years ago

1.3.13

8 years ago

1.3.12

8 years ago

1.3.11

8 years ago

1.3.10

8 years ago

1.3.9

8 years ago

1.3.8

8 years ago

1.3.7

8 years ago

1.3.6

8 years ago

1.3.5

8 years ago

1.3.3

8 years ago

1.3.2

8 years ago

1.3.0

8 years ago

1.2.0

8 years ago

1.1.3

8 years ago

1.1.2

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago