https://github.com/plotdb/template-text
text from template for all purposes
https://github.com/plotdb/template-text
Last synced: about 1 year ago
JSON representation
text from template for all purposes
- Host: GitHub
- URL: https://github.com/plotdb/template-text
- Owner: plotdb
- Created: 2020-11-06T04:16:35.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-05-05T00:52:24.000Z (almost 3 years ago)
- Last Synced: 2025-02-14T23:33:02.947Z (about 1 year ago)
- Language: JavaScript
- Size: 35.2 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# 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`.
## 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("", cfg, );
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.
'''}
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(, {uuid: uuid});
We use LiveScript to parse the expression when interpolating.
## 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