Code generation for the rest of us
- Language agnostic
- Mustache Templates + JSON
- Inline templates & data
- Simple project integration
npm install -g swapm
cd project/
swapm --init
this will create a project/swapm directory where your data & templates live.
An injection point consists of an open and close tag. The open tag must state the template and data to use.
[=[template: "template_name", data: "data_name"]=]
[=[end]=]
- The template is rendered and injected between the two tags.
- Injection tags can span multiple lines.
- The syntax used to comment out a tag cannot be on its own line.
/* [=[template: "foo", data:{
baz: "hello world"
}]=] */
// [=[end]=]
/*
[=[template: "foo", data:{
baz: "hello world"
}]=]
*/ <-- this will get cut
/* <-- this too
[=[end]=]
*/
- All data is in JSON format.
- There are three ways to define data.
[=[template: "my_template", data:{
foo: "bar",
bar: [1, 2, 3, 4]
}]=]
[=[end]=]
[#my_data=[{
foo: "bar",
bar: [1, 2, 3, 4]
}]=]
File: project/swapm/data/my_data.json
{
foo: "bar",
bar: [1, 2, 3, 4]
}
- Mustache is the templating engine used.
- There are two ways of defining a template
[@my_template=[
Foo: {{foo}}
Bars: {{#bar}}{{.}}{{/bar}}
]=]
File: project/swapm/templates/my_template.tmpl
Foo: {{foo}}
Bars: {{#bar}}{{.}}{{/bar}}
This is a mustache feature but it's essential to know when using swapm.
/*
[#foo_data=[{
items: [
{ a: 1, b: 2 },
{ a: 3, b: 4 },
{ a: 5, b: 6 }
],
addAtoB: function() {
return this.a + this.b;
}
}]=]
[@foo_tmpl=[
{{#items}}
Sum: {{addAtoB}}
{{/items}}
]=]
*/
// [=[template: "foo_tmpl", data: "foo_data"]=]
// [=[end]=]
this is a work in progress and it might eat your code.
