Skip to content

An easy-to-use, simple, versatile NodeJS Templating Module

Notifications You must be signed in to change notification settings

Teminix/node-js-easy-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Node JS Easy Templating Module

This is a templating module which is used for node JS. Highly versatile and easy to use. You can add your own variable detection parameters. This templating module does not require you to only use html, it can in fact, be used in any type of file.

Warning: project still in progress, downloading and using this module is not advised. Download/use on your own risk.



Architecture: Asynchronous and Synchronous.


Setup

Download easy-temp.min.js if you wish to integrate it in you application without the intention of understanding the code, place it in your respective application directory or wherever you can access it. However, if you do want to understand the code, download easy-temp.js, it provides commenting.
Code to initiate:

const et = require("./path/to/easy-temp.min"); // remove the '.min' if your using uncompressed version

Key to documentation syntax(important):

For a function in this documentation:

functionName(String(name),Number(amount),function(callback),?argument=Object(defaultValue))

`?` Is an optional argument, after the '`=`' is where the `Object(defaultValue)` is the default value. `Object` tells the argument type, which is an Object.

String(name) Means nothing but that the argument named 'name' must be a String. Same applies to any other supported datatype.
function(callback) means that the argument takes a function with a name of 'callback'

An example of this function would be:

functionName('blah',45,function(){console.log('hello')},{message:'this is an optional argument'})





Usable functions from this library:

This library at this stage has only two functions, Asynchronous and Synchronous respectively:

  • temp()
  • tempSync()


Asynchronous temp() function:

Assume et as the module using the require function below:

const et = require('./path/to/easy-temp.min');

Syntax:

et.temp(String(path),function(callback),?vars=null,?start='<(',?end=')>')



path:

This is the path to respective file you want to template.
Example: "index.html" or some other file(does not have to be an html file)


callback:

This is a callback function since this is an asynchronous function. This callback has one callback variable. That variable is the content of the template file after it has been processed.
Example: function(resp){console.log(resp)} would log the resp to the console.


vars:(Object)

This is where you would specify your variables, note that this is an optional variable, so you do not have to specify variables for the template to substitute if you do not want it to. This would take a key value pair object.
Example: {title:'Future of society',content:'The world as we know it... blah'}
Here the variable title has the value 'Future of society' and content has the value 'The world as we know it... blah'


start:(String) and end:(String)

Optional, you can specify how you want the template function to replace variables in the file you are rendering. The start argument would specify the start parameter and the end would specify the end
Example: As an example, you wish to substitute: $title$ with Heavens in your template. You set the start argument in the function as "$" and end as "$"

<span class='title'>$title$</span>

Here, the $title$ part would be replaced with 'Heavens' after the function executes:

<span class='title'>Heavens</span>

Examples:




Here are some full fledged examples of code and ways to implement this function in your application:

Basic templating using default start and end

Start and end parameters are: <( and )> respectively.

Javascript(index.js):

const et = require('./path/to/easy-temp.min');
et.temp('index.html',function(resp){console.log(resp)},{title:'My dream job',content:'My dream job is front end web development ... ...'})
// The response/output would be logged to the console

HTML(index.html):

<body>
  <h1><(title)></h1>
   <p><(content)></p>
</body>

Output(console):

node index.js

<body>
  <h1>My dream Job</h1>
   <p>My dream job is front end we development ... ...</p>
</body>



Templating with different start and end parameters

Example 1:

Start and end parameters here are: * and * respectively.

Javascript(index.js):

const et = require('./path/to/easy-temp.min');
et.temp('index.html',function(resp){console.log(resp)}
,{location:'India',name:'Sarah'}
, "*","*");
// The response/output would be logged to the console

HTML(index.html):

<body>
  <h1>Welcome *name*!</h1>
   <p>Explore the vast heritage of *location*</p>
</body>

Output(console):

node index.js

<body>
  <h1>Welcome Sarah!</h1>
   <p>Explore the vast heritage of India</p>
</body>

Example 2:

Start and end parameters here are: ${ and } respectively.

Javascript(index.js):

const et = require('./path/to/easy-temp.min');
et.temp('index.html',function(resp){console.log(resp)}
,{car:'BMW',owner:'Bill'}
, "${","}");
// The response/output would be logged to the console

HTML(index.html):

<body>
  <img>
  <div class="car">${car}</div><br>
  <input type='text' name='owner' value='${owner}'>
</body>

Output(console):

node index.js
<body>
  <img>
  <div class="car">BMW</div><br>
  <input type='text' name='owner' value='Bill'>
</body>




Synchronous tempSync() function:

Assume et as the module using the require function below:

const et = require('./path/to/easy-temp.min');

Syntax:

Note: Since this is synchronous, this function does not have a callback function

let template = et.tempSync(String(path),?vars=null,?start='<(',?end=')>')



path:

This is the path to respective file you want to template.
Example: "index.html" or some other file(does not have to be an html file)


vars:(Object)

This is where you would specify your variables, note that this is an optional argument, so you do not have to specify variables for the template to substitute if you do not want it to. This would take a key value pair object.
Example: {title:'World wide web',content:'The world wide web is a vast connection of webservers...'}
Here the variable title has the value 'World wide web' and content has the value 'The world wide web is a vast connection of web servers...'


start:(String) and end:(String)

Optional, you can specify how you want the template function to replace variables in the file you are rendering. The start argument would specify the start parameter and the end would specify the end
Example: As an example, you wish to substitute: $title$ with Heavens in your template. You set the start argument in the function as "$" and end as "$"

<span class='title'>$title$</span>

Here, the $title$ part would be replaced with 'Heavens' after the function executes:

<span class='title'>Heavens</span>

Examples:




Here are some full fledged examples of code and ways to implement this synchronous function in your application:

Basic templating using default start and end

Start and end parameters are: <( and )> respectively.

Javascript(index.js):

const et = require('./path/to/easy-temp.min');
let template = et.tempSync('index.html',{title:'My dream job',content:'My dream job is front end web development ... ...'});
console.log(template);
// The response/output would be logged to the console

HTML(index.html):

<body>
  <h1><(title)></h1>
   <p><(content)></p>
</body>

Output(console):


node index.js

<body>
  <h1>My dream Job</h1>
   <p>My dream job is front end we development ... ...</p>
</body>



Templating with different start and end parameters

Example 1:

Start and end parameters here are: * and * respectively.

Javascript(index.js):

const et = require('./path/to/easy-temp.min');
let template = et.tempSync('index.html'
,{location:'India',name:'Sarah'}
, "*","*");
console.log(template);
// The response/output would be logged to the console

HTML(index.html):

<body>
  <h1>Welcome *name*!</h1>
   <p>Explore the vast heritage of *location*</p>
</body>

Output(console):

node index.js

<body>
  <h1>Welcome Sarah!</h1>
   <p>Explore the vast heritage of India</p>
</body>

Example 2:

Start and end parameters here are: ${ and } respectively.

Javascript(index.js):

const et = require('./path/to/easy-temp.min');
let template = et.tempSync('index.html'
,{car:'BMW',owner:'Bill'}
, "*","*");
console.log(template);
// The response/output would be logged to the console

HTML(index.html):

<body>
  <img>
  <div class="car">${car}</div><br>
  <input type='text' name='owner' value='${owner}'>
</body>

Output(console):

node index.js
<body>
  <img>
  <div class="car">BMW</div><br>
  <input type='text' name='owner' value='Bill'>
</body>




Note: For both functions, the variables would be replaced in the template Globally, i.e, if there are two places with the same variable, they both would get replaced. Also note that if there is an undefined variable, it won't be replaced Example: {content:'something'} in the document: <(title)> won't be replaced, it would remain as <(title)> only