2.0.0 • Published 8 years ago

templata v2.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
8 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

8 years ago

1.3.32

9 years ago

1.3.30

9 years ago

1.3.29

9 years ago

1.3.28

9 years ago

1.3.27

9 years ago

1.3.26

9 years ago

1.3.25

9 years ago

1.3.24

9 years ago

1.3.22

9 years ago

1.3.21

9 years ago

1.3.20

9 years ago

1.3.19

9 years ago

1.3.18

10 years ago

1.3.17

10 years ago

1.3.16

10 years ago

1.3.15

10 years ago

1.3.14

10 years ago

1.3.13

10 years ago

1.3.12

10 years ago

1.3.11

10 years ago

1.3.10

10 years ago

1.3.9

10 years ago

1.3.8

10 years ago

1.3.7

10 years ago

1.3.6

10 years ago

1.3.5

10 years ago

1.3.3

10 years ago

1.3.2

10 years ago

1.3.0

10 years ago

1.2.0

10 years ago

1.1.3

10 years ago

1.1.2

10 years ago

1.1.1

10 years ago

1.1.0

10 years ago

1.0.4

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago