https://github.com/formix/templater
Template any text file using Javascript
https://github.com/formix/templater
Last synced: about 2 months ago
JSON representation
Template any text file using Javascript
- Host: GitHub
- URL: https://github.com/formix/templater
- Owner: formix
- License: mit
- Created: 2016-05-17T14:25:34.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2018-05-09T15:05:24.000Z (about 8 years ago)
- Last Synced: 2025-12-27T13:14:12.020Z (6 months ago)
- Language: C#
- Size: 19.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
templater
=========
Template absolutely any text file using this simple ASP like Javascript
templating engine.
## Usage
### Without model
```C#
var template = new Template("Templates/github-example.txt");
template.Render(Console.Out); // writes to any TextWriter
```
### With a model
You can render using a model. The `model` object is then available to the
script.
```C#
var template = new Template("Templates/github-example.txt");
template.Render(Console.Out, new {
FirstName = "John",
LastName = "Doe",
Age = 30
});
```
When the render method is called for the first time, the template is compiled
to a script that is reused if further calls to `Render` is made with different
models.
The `output` object is the `System.IO.TextWriter` that is passed to the
`Render` method.
## Template example
*github-example.txt*
```ASP
<%
var x = 5 * 3;
%>
The result is: '<% =x %>'
<% for (var i = 0; i < 3; i++) { %>
Hello for the <%
if (i === 0) {
output.Write("first");
} else if (i === 1) {
output.Write("second");
} else if (i === 2) {
output.Write("third");
}
%> time! <%
} %>
Thanks
```
This will produce the following text file:
```
The result is: '15'
Hello for the first time!
Hello for the second time!
Hello for the third time!
Thanks
```