Getting started - PeterNaydenov/code-assembly-line GitHub Wiki
Installation
Add code-assembly-line to your project:
npm install code-assembly-line
Create new instance of the engine:
import CodeAssemblyLine from 'code-assembly-line'
// start instance of template engine
const tplEngine = new CodeAssemblyLine ();
Code-Assembly-Line is ready to use.
Templates
Templates for Code-Assembly-Line are simple javascript object where key is a template-name and value is the template. Example:
// create a template library
const templateLibrary = {
'hello' : 'Hello {{user}}'
, 'bye' : 'Bye {{user}}'
}
tplEngine.insertTemplate ( templateLibrary ) // provide more than one template at once.
Templates are ready to use.
Processes
Process is a list of predefined data-manipulation and rendering steps. It's represented by array of step-objects. Every process-step has parameter do and couple of other params that will help you better to describe the process. Let's look at very simple example with two steps:
const hello = [
{ do: 'set' as: 'user' }
, { do: 'draw', tpl:'hello' }
]
tplEngine.insertProcess ( hello, 'hello' ) // register process to template engine
String data will be converted to object with parameter 'user' and then the object will be rendered with template 'hello'.
Let's see some examples:
const name = 'Peter';
const simple = tplEngine.run ( 'hello', name )
// => 'Hello Peter'
const friends = [ 'Ivo', 'Georgy', 'Stefan' ];
const greetFriends = tplEngine.run ( 'hello', friends )
// => [ 'Hello Ivo', 'Hello Georgy', 'Hello Stefan' ]
Process will be started for each member of the array. Result will be array of 3 greeting results. Define separate process for merging results:
const doMerge = [ { do:'block', space: ', ' ] // will combine results in a single string
tplEngine.insertProcess ( doMerge, 'merge' )
const single = tplEngine.run ( ['hello','merge'], friends )
/*
Will execute process 'hello' and then will execute 'merge'. Result of 'hello' process will be
treated as input-data on running 'merge' process.
*/
Try this code by yourself. Sandbox with library loaded is available for you on this fiddle.
Blocks
Blocks is standard place where process result can be saved. Just add process-step 'block' with name and your render is saved for later use.
const CodeAssemblyLine = require ("code-assembly-line");
const tplEngine = new CodeAssemblyLine ();
tplEngine
.insertTemplate ( { hello : 'Hello {{user}}!' } )
.insertProcess ( [
{ do : 'set', as: 'user' }
, { do: 'draw', tpl: 'hello' }
, { do: 'block', name: 'greetings', space: '\n'}
], 'greetings' )
.run ( 'greetings', [ 'Peter', 'Ivan', 'Stefan', 'Ivo' ])
// ...latter in the code
const result = tplEngine.getBlock ( 'greetings' )
/* =>
Hello Peter!
Hello Ivan!
Hello Stefan!
Hello Ivo!
*/
Saved blocks are available to other templates. Use 'greetings' block from any other template by using placeholder 'block/greetings'. Try this code-sample
Data
Data provides alternative content for placeholders if information is missing on running the process. Data for Code-Assembly-Line is a javascript object where key is a data-name and value is the data itself. Similar to templates. Example:
data = {
'name' : 'no-name'
, 'address' : 'Address is not available'
}
tplEngine.insertData ( data )
Try this code-sample
Note: Blocks are subset of data and can be used on exactly same way.