diff --git a/README.md b/README.md index 8942683..9e41084 100644 --- a/README.md +++ b/README.md @@ -1,172 +1,45 @@ -jSmart -====== +# avcajaraville/jsmart -jSmart is a port of the Smarty Template Engine to Javascript, a JavaScript template library that supports the template [syntax](https://github.com/umakantp/jsmart/wiki/syntax) and all the features (functioens, variable modifiers, etc.) of the well-known PHP template engine [Smarty](http://www.smarty.net/). +## Notice -jSmart is written entirely in JavaScript, does not have any DOM/DHTML/browser or third-party JavaScript library dependencies and can be run in a web browser as well as a standalone JavaScript interpreter or [CommonJS](http://www.commonjs.org/) environments like [node.js](http://nodejs.org/). +This is a fork from [umakantp/jsmart](https://github.com/umakantp/jsmart), which is also a fork from [miroshnikov/jsmart](https://github.com/miroshnikov/jsmart). -jSmart supports plugin architecture, you can [extend it with custom plugins](https://github.com/umakantp/jsmart/wiki/Create-Plugin): functions, blocks and variable modifiers, [templates inclusion](https://github.com/umakantp/jsmart/wiki/Include-Templates), [templates inheritance](https://github.com/umakantp/jsmart/wiki/Template-Inheritance) and overriding, [caching](https://github.com/umakantp/jsmart/wiki/Caching), [escape HTML](https://github.com/umakantp/jsmart/wiki/escape_html). +I originally forked from [umakantp/jsmart](https://github.com/umakantp/jsmart) and added the following modifications: -jSmart has some limited support of the [PHP Smarty syntax](https://github.com/umakantp/jsmart/wiki/syntax) and allows you to [use the same Smarty templates on both server and client side](https://github.com/umakantp/jsmart/wiki/Smarty-template-in-javascript), for both PHP and Javascript. +- Force **nocache** option for **extends** blocks. This was causing templates being only properly compiled once. If you need cache (and you probably would), you will have to address this outside the jSmart class. +- Add UMD, CommonJS & web/browser support. Based on [this pull request](https://github.com/umakantp/jsmart/pull/14). +- Remove **String.prototype.fetch** function (since this is a very bad practice), and instead, modifying the export of the module. Now it exports the constructor and the fetch function. +- The module now exports the following object: `{ jSmart: jSmart, fetch: fetch }`. This is, `jSmart` constructor and `fecth` function -### How to use jSmart in Node.js -1. Install jSmart from NPM Registry +**Caution**: when using fetch function, you want to proper set it context (ie: call or apply to the rescue). I didn’t want to re-write this function and keep as it was originally written. - $ npm install jsmart +## How to use it +Im gonna just highlight the main differences with [umakantp/jsmart](https://github.com/umakantp/jsmart). -2. Create template, use [PHP Smarty syntax](https://github.com/umakantp/jsmart/wiki/syntax). Say demo.tpl +For a more detailed explanation and documentation, please refer to [it main repository](https://github.com/umakantp/jsmart) or the [wiki](https://github.com/umakantp/jsmart/wiki) - Hello {$name} +```javascript +var import_jSmart = require( 'jsmart' ); +// import the constructor: +var jSmart = import_jSmart.jSmart; +// and the fetch function: +var fetch = import_jSmart.fetch; -3. Now lets read the template and compile it. _jSmart_ object compiles the template. +// Following the example on the original repo (refer to it for more details): +var fs = require( 'fs' ); +var tpl = fs.readFileSync( './demo.tpl', { encoding: 'utf-8' } ); +var compiledTpl = new jSmart( tpl ); +// We need to set the context of the fetch function, for example, through Function.prototype.call function +var output = fetch.call( compiledTpl, { name: 'World' } ); +console.log( output ); - var fs = require('fs'); - require('jsmart'); - var tpl = fs.readFileSync('./demo.tpl', {encoding: 'utf-8'}); - var compiledTpl = new jSmart(tpl); +``` -4. Assign data to the template passing Javascript object to the _fetch_ function. Variable _compiledTpl_ has the compiled template. You can call _fetch_ function as many times with different data. - - var fs = require(fs); - require('jsmart'); - var tpl = fs.readFileSync('./demo.tpl', {encoding: 'utf-8'}); - var compiledTpl = new jSmart(tpl); - var output = compiledTpl.fetch({name: 'World'}); - console.log(output); - -5. Execute the file. - - $ node demo.js - -6. Result would be. - - Hello World - - -### How to use jSmart in browser - -1. Include jSmart library Javascript file in your header. - - -
- - - -2. Create template, use [PHP Smarty syntax](https://github.com/umakantp/jsmart/wiki/syntax). Put the template's text in _<script>_ with the _type="text/x-jsmart-tmpl"_ so a browser will not try to parse it and mess it up. - - - -3. Create JavaScript data object with variables to assign to the template - - - -4. Create new object of _jSmart_ class, passing the template's text as it's constructor's argument than call _fetch(data)_, where data is an JavaScript object with variables to assign to the template - - - -5. The result would be - -