https://github.com/icholy/swapm
Code generation for the rest of us
https://github.com/icholy/swapm
Last synced: 12 months ago
JSON representation
Code generation for the rest of us
- Host: GitHub
- URL: https://github.com/icholy/swapm
- Owner: icholy
- Created: 2012-10-17T22:19:57.000Z (almost 14 years ago)
- Default Branch: stable
- Last Pushed: 2013-03-06T03:27:04.000Z (over 13 years ago)
- Last Synced: 2025-07-09T02:51:23.681Z (about 1 year ago)
- Language: JavaScript
- Size: 352 KB
- Stars: 4
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Swap'Em [](https://travis-ci.org/icholy/swapm)
> Code generation for the rest of us
## Features
* Language agnostic
* [Mustache](https://github.com/janl/mustache.js) Templates + JSON
* Inline templates & data
* Simple project integration
## Install
npm install -g swapm
## Initialize Project
cd project/
swapm --init
this will create a `project/swapm` directory where your data & templates live.
## Injection
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.
### Good
/* [=[template: "foo", data:{
baz: "hello world"
}]=] */
// [=[end]=]
### Bad
/*
[=[template: "foo", data:{
baz: "hello world"
}]=]
*/ <-- this will get cut
/* <-- this too
[=[end]=]
*/
## Data
* All data is in JSON format.
* There are three ways to define data.
### Inline
[=[template: "my_template", data:{
foo: "bar",
bar: [1, 2, 3, 4]
}]=]
[=[end]=]
### Source block
[#my_data=[{
foo: "bar",
bar: [1, 2, 3, 4]
}]=]
### Json file
**File:** `project/swapm/data/my_data.json`
{
foo: "bar",
bar: [1, 2, 3, 4]
}
## Templates
* Mustache is the templating engine used.
* There are two ways of defining a template
### Source block
[@my_template=[
Foo: {{foo}}
Bars: {{#bar}}{{.}}{{/bar}}
]=]
### .tmpl file
**File:** `project/swapm/templates/my_template.tmpl`
Foo: {{foo}}
Bars: {{#bar}}{{.}}{{/bar}}
### Computed Properties
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]=]
# Warning
this is a work in progress and it might eat your code.