templatesjs
Advanced tools
Comparing version 1.1.111 to 1.2.0
16
index.js
@@ -32,2 +32,3 @@ 'use strict'; | ||
exports.dir; | ||
exports.logErr = false; | ||
function include(){ | ||
@@ -51,4 +52,7 @@ | ||
}else if(fs.existsSync(exports.dir+fileName) == false){ | ||
file=""; | ||
if(exports.logErr){ | ||
file = "File Not Found!"; | ||
}else{ | ||
file = ""; | ||
} | ||
} | ||
@@ -58,4 +62,8 @@ }else{ | ||
var file = fs.readFileSync(fileName); | ||
}else if(fs.existsSync(exports.dir+fileName) == false){ | ||
file=""; | ||
}else if(fs.existsSync(fileName) == false){ | ||
if(exports.logErr){ | ||
file = "File Not Found!"; | ||
}else{ | ||
file = ""; | ||
} | ||
} | ||
@@ -62,0 +70,0 @@ |
{ | ||
"name": "templatesjs", | ||
"version": "1.1.111", | ||
"description": "Render template without any engine, pure JavaScript,easy to use, works with any file format including HTML, include files inside files, can be used with any framework and raw node.js ", | ||
"version": "1.2.0", | ||
"description": "Render dynamic view , works with any file format including HTML, can be used with any framework and raw node.js ", | ||
"main": "index.js", | ||
@@ -11,11 +11,8 @@ "scripts": { | ||
"template", | ||
"templatesjs", | ||
"engine", | ||
"javascript", | ||
"engines", | ||
"render", | ||
"pure", | ||
"easy", | ||
"html", | ||
"include", | ||
"dynamic", | ||
"without" | ||
"view" | ||
], | ||
@@ -31,4 +28,14 @@ "engines": { | ||
}, | ||
"directories": {}, | ||
"dependencies": {} | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"dependencies": {}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/ImtiazChowdhury/templatesjs.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/ImtiazChowdhury/templatesjs/issues" | ||
}, | ||
"homepage": "https://github.com/ImtiazChowdhury/templatesjs#readme" | ||
} |
154
README.md
@@ -5,2 +5,3 @@ # templatesjs | ||
### Render dynamic data to your template. | ||
@@ -11,13 +12,17 @@ [](https://travis-ci.org/ImtiazChowdhury/templatesjs) | ||
[]() | ||
[]() | ||
[]() | ||
## Features | ||
Render dynamic data to your template without any engine. | ||
A pure javascript module without any dependency and further installation. | ||
* **Output data with <%%>** | ||
* **Output String, Array, Object.property** | ||
* **Format Output** | ||
* **UPPERCASE, Capitalized, Lowercase Output** | ||
* **Include** | ||
* **Custom delimiters (e.g., use <# #> instead of <% %>)** | ||
It works using simple javascript function, high speedy and easy to use. | ||
works with any file format including HTML. you can also include another files in your file using include() | ||
## table of contents: | ||
## Table of contents: | ||
@@ -28,16 +33,16 @@ | ||
- [Render string](#how-to-render-string) | ||
- [Render object](#how-to-render-value-of-object) | ||
- [Render array](#how-to-render-specific-index-value-of-an-array) | ||
- [Loop through array](#how-to-render-all-values-looping-through-the-whole-array) | ||
- [Loop through specific array indexes] (#how-to-render-all-values-looping-through-some-specific-index-of-array) | ||
- [Specify format of output] (#how-to-specify-format-of-output) | ||
- [Render string](#string) | ||
- [Render object](#object) | ||
- [Render array](#array) | ||
- [Loop through array](#loopthrough-array) | ||
- [Loop through specific array indexes](#loop-through-specific-index-of-array) | ||
- [Specify format of output](#format-of-output) | ||
- [UPPERCASE, Capitalized, lowercase output](#specify-case) | ||
- [Include files](#include-another-file-inside-a-file-or-data) | ||
- [Set default directory] (#set-default-directory-for-files) | ||
- [Change delimiter](#change-the-delimiter-sign) | ||
- [Shorthands for functions] (#shorthands-for-functions) | ||
- [Include files](#include-file) | ||
- [Set default directory](#set-default-directory) | ||
- [Change delimiter](#delimiter-sign) | ||
- [Shorthands for functions](#shorthands-for-functions) | ||
- [Demonstration](#demonstration-with-javascript-on-client-side) | ||
- [Test](#test) | ||
## Installation | ||
@@ -53,21 +58,33 @@ | ||
## Usage | ||
# Usage | ||
####Example html : | ||
```html | ||
## How to render string | ||
<body> | ||
Hello <%name%> | ||
</body> | ||
for examples below we will be rendering dynamic data to a HTML page | ||
``` | ||
####node.js | ||
```js | ||
temp.render("name", "John Doe"); | ||
``` | ||
in our html page we must use a tag to call data , a templatesjs tag looks like <%keyword%> | ||
note: you can change the delimiter that defines the tag (%) | ||
####Output : | ||
Hello John Doe | ||
##string | ||
Use <% %> to use rendered data in HTML page | ||
examle HTML(index.html) | ||
```html | ||
<!DOCTYPE HTML> | ||
<html> | ||
<head> | ||
<title>TemplatesJS Demonstration</title> | ||
</head> | ||
<body> | ||
HELLO <%user %> | ||
HELLO <%firstname%> <%lastname%> | ||
</body> | ||
@@ -83,12 +100,9 @@ </html> | ||
var templatesjs = require('templatesjs'); | ||
fs.readFile("./index.html", function(err,data){ | ||
if(err) through err | ||
/*you must set data once for each file, templatesjs works | ||
with the given data, not with the actual file;*/ | ||
templatesjs.set(data); //set the data to modify | ||
templatesjs.set(data); | ||
var output = templatesjs.render("user", "John Doe"); | ||
var output = templatesjs.render("firstname", "John"); | ||
output = templatesjs.render("lastname", "Doe"); | ||
res.write(output); | ||
@@ -102,4 +116,7 @@ res.end(); | ||
this will print `"HELLO John Doe"` on the browser instead of `hello <%user%>` | ||
templatesjs work with the data you set using set(). It modifies the data and returns the modified data. | ||
It will all `<%firstname%>` tags in the data with "John" and return the data. | ||
## How to render value of object | ||
## object | ||
we can also render array or object value as | ||
@@ -124,4 +141,6 @@ | ||
Templatesjs won't render the object `profile` to the page, | ||
it will only relace the `<%user.name%>` with the `name` property of `profile` object. | ||
## How to render specific index value of an array | ||
## array | ||
@@ -149,3 +168,3 @@ for array: | ||
## How to render all values looping through the whole array | ||
## loop through array | ||
@@ -171,3 +190,3 @@ or all values of an array : | ||
## How to render all values looping through some specific index of array | ||
## loop through specific index of array | ||
@@ -198,6 +217,8 @@ all array values starting from an index to another one | ||
this will print `"18helloworldJS"` | ||
A loop will be performed which will start form `user[2]` and finish on `user[5]'. | ||
No whitespace will be added before or after the values. | ||
## format of output | ||
## How to specify format of output | ||
suppose that we want a for each loop through our array values | ||
@@ -230,5 +251,7 @@ using templatesjs it can be done like | ||
A loop will be performed and values will be added inside the specified format | ||
replacing all "*" with actual value. I foyu want a Linebreak at the end of every link | ||
you must specify the format as `{<a href="user/*">*</a> <br />}` | ||
for a loop through specified indexes of an array in specified format | ||
#### for a loop through specified indexes of an array in specified format | ||
```html | ||
@@ -238,7 +261,2 @@ <body> | ||
<%user[2,4] {<a href="user/*">*</a>}%> | ||
<!-- | ||
**specify the format in curly braces | ||
** all "*" sign will be replaced by the actual | ||
**value with the format specified around it | ||
--> | ||
@@ -261,12 +279,6 @@ </body> | ||
or specify format for only one array index if you want | ||
#### or specify format for only one array index if you want | ||
```html | ||
<body> | ||
<%user[2] {<a href="user/*">*</a>}%> | ||
<!-- | ||
**specify the format in curly braces | ||
** all "*" sign will be replaced by the actual | ||
**value with the format specified around it | ||
--> | ||
@@ -286,5 +298,4 @@ </body> | ||
output : `<a href="user/foo">foo</a>` | ||
## Specify case | ||
@@ -294,3 +305,3 @@ | ||
you can specify whether the output will be in UPPERCASE, lowercase, Capitalized using a third optional `"case"` param | ||
you can specify whether the output will be in UPPERCASE, lowercase, Capitalized using a third optional `"style"` param | ||
in the `templatesjs.render()` function | ||
@@ -330,6 +341,8 @@ | ||
`lowercase: smith`; | ||
Any value for the style parameter other than "CASE", "Case" or "case" will produce an error message on the console | ||
and consider the value of style as undefined. | ||
## Include file | ||
## Include another file inside a file or data | ||
templatesjs also has an include feature which can be used | ||
@@ -360,7 +373,10 @@ to include file or template parts just use the `<%include%>` tag in your file | ||
The `include` function gets invoked when you set the data for template. | ||
It will look for every file specified inside <%inlude %> tag and replace the tag with Data read from those files. | ||
Because include is performed at the very beginning **you can render data inside those included files as well** | ||
If the file is not found it will replace the <%include %> tag with "". Without causing any error. | ||
You can set `teplatesjs.logErr` to `true` if you want a "File Not Found!" error in the output. | ||
## Set default directory for files | ||
## Set default directory | ||
@@ -384,6 +400,6 @@ **set default directory**: | ||
``` | ||
the default directory (if needed to be set) must be set before setting the data using `ste()`. | ||
## delimiter sign | ||
## Change the delimiter sign | ||
**Don't like to use the "%" sign to define tags in html page you can change them :D :D :D** | ||
@@ -460,7 +476,7 @@ use the templatesjs.delim() function | ||
var http = require('http'); | ||
templatesjs.dir = "./public/" | ||
http.crateServer(function(req,res){ | ||
fs.readFile("./public/index.html", function(err,data){ | ||
if(err) throw err; | ||
templatesjs.dir = "./public/" | ||
templatesjs.set(data) | ||
@@ -481,4 +497,12 @@ | ||
#### for each view you want to render set data once. | ||
## Test | ||
```sh | ||
$ npm test | ||
``` | ||
Please Report any bug or error https://github.com/ImtiazChowdhury/templatesjs/issues. | ||
Thank You for having a look through. | ||
Hope you enjoy coding with templatesJS. |
@@ -74,2 +74,3 @@ 'use strict'; | ||
it('should replace the <%include test.txt%> tag with "templatesjs"', function() { | ||
templatesjs.logErr = true; | ||
templatesjs.dir = "./test/" | ||
@@ -80,2 +81,9 @@ var result = templatesjs.set("<%include test.txt %>"); | ||
}); | ||
it('should replace the <%include test.txt%> tag with "File Not Found!"', function() { | ||
templatesjs.logErr = true; | ||
templatesjs.dir = "./wrongdir/" | ||
var result = templatesjs.set("<%include test.txt %>"); | ||
expect(result).to.equal('File Not Found!'); | ||
}); | ||
}); |
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
26628
7
385
1
0
488
1