Skip to content

plotdb/template-text

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

template-text

generate text from template for all purposes with LiveScript syntax. Use eval so one should always use this with owned/reviewed template.file.

Note: this is equivalent to run code in template. Always use it with trusted data source only.

Usage

install with npm:

npm install --save template-text

command name and parameters:

tt -c config.file -o output.file template.file

You can also require a module as config to use functions in the template from the module:

tt -r some-module -o output.file template.file

API

Programmatically interpolate your template with template-text api:

tt = require("template-text")
cfg = function() { return { ... }; } || { ... };
ret = tt("<template-file-content>", cfg, <root-path>);

where:

  • cfg: a hash of all available variable
  • root-path: reference root when look up files for include api.

Template File Format

For every string you want to replace, use !{ ... } with variable / expression inside. For example,

Hello !{user.name}!

You have visited our website for !{user.count} time.

It also supports conditional block:

!{if user.count > 10 => '''

it seems that you have been here for many times.

''' else '''

Newcomer here? You probably will want to take a look at our tutorial.

'''}

If you need interpolation inside expressions, please note that """ is escaped so you need to use !"" ... ""!:

!{if user.count > 10 => !""

it seems that you have been here for !{count} times.

""! else !""

Hi !{name}, newcomer here? You probably will want to take a look at our tutorial.

""!}

When calling with API, you can even use builtin functions for expressions, or custom function if you pass it into the config object:

A random number: !{Math.random()}
A random UUID: !{uuid()}

Program counterpart for above text:

uuid = require("uuid");
tt = require("template-text");

ret = tt(<the above text>, {uuid: uuid});

Again, we use LiveScript to parse the expression when interpolating, so it's quite dangerous to load template from untrusted source. Be sure to only use template-text with templates from trusted source.

Builtin Functions

Except common JS APIs, you can also use following API in template:

  • include(filename) - recursively include file in place.

TODO

  • Token Customizing - It's possible that the token !{ ... } is used by the target language / syntax of the text we want to interpolate. So it will be nice if we can make it customizable.
  • Defaul Language - make it customizable about the language users want to use, at least for following languages:
    • ES6 ( ES2015 or later )
    • TypeScript

License

MIT

About

text from template for all purposes

Resources

License

Stars

Watchers

Forks

Packages

No packages published