diff --git a/README.md b/README.md index 8942683..2c1eef2 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 port 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 - -

Hi, there are some JScript books you may find interesting:

- -
- [1] JAVASCRIPT: THE DEFINITIVE GUIDE by David Flanagan - $31.18 -
- -
- [2] MURACH JAVASCRIPT AND DOM SCRIPTING by Ray Harris -
- -
- [3] HEAD FIRST JAVASCRIPT by Michael Morrison - $29.54 -
- - Total: 3 - -6. The template's text is compiled in the _jSmart_ constructor, so it's fast to call _fetch()_ with different assigned variables many times. - - var tpl = new jSmart( '{$greeting}, {$name}!' ); - - tpl.fetch( {greeting:'Hello', name:'John'} ); //returns: Hello, John! - - tpl.fetch( {greeting:'Hi', name:'Jane'} ); //returns: Hi, Jane! - - -### DOCUMENTATION - -[https://github.com/umakantp/jsmart/wiki](https://github.com/umakantp/jsmart/wiki) - -### TESTS - -* Install [Node.js](http://nodejs.org/) and [PHP](http://www.php.net) in a folder. - e.g. Install them in directories _/home/user/jsmart/node_ and _/home/user/jsmart/php_ respectively. - -* Clone jSmart repo in the same folder. - e.g. Clone at _/home/user/jsmart_. So jsmart repo is in _/home/user/jsmart/jsmart_ folder. - -* Go to jSmart and run _make test_. - e.g. Go to _/home/user/jsmart/jsmart_ and run _make test_. - -* You can modify _makefile_ and _test/js/test-common.js_ for changing path of node and php respectively as per your needs but never commit those changes in master repository. - -### NOTICE - -This project was originally hosted at [Google code](http://code.google.com/p/jsmart/) and was started by [miroshnikov](https://github.com/miroshnikov). -Since author was not active on project. I have forked and planned on pushing further improvements and features. +## Thanks to +- [miroshnikov](https://github.com/miroshnikov) for his [original jSmart](https://github.com/miroshnikov/jsmart). +- [umakantp](https://github.com/umakantp) for his [jSmart fork](https://github.com/umakantp/jsmart). +- [duzun](https://github.com/duzun) for adding UMD support on his [pull request](https://github.com/umakantp/jsmart/pull/14). \ No newline at end of file diff --git a/jsmart.js b/jsmart.js index 2131827..93be854 100644 --- a/jsmart.js +++ b/jsmart.js @@ -9,7 +9,7 @@ */ -(function() { +(function(global) { /** merges two or more objects into one @@ -1861,7 +1861,7 @@ } - jSmart = function(tpl) + var jSmart = function(tpl) { this.tree = []; this.tree.blocks = {}; @@ -1948,36 +1948,36 @@ var currSect = ''; for (var f=s.match(re); f; f=s.match(re)) { - s = s.slice(f.index+f[0].length); - if (f[1]) - { - currSect = f[1]; - } - else if ((!currSect || currSect == section) && currSect.substr(0,1) != '.') - { - if (f[3] == '"""') - { - var triple = s.match(/"""/); - if (triple) - { - data.smarty.config[f[2]] = s.slice(0,triple.index); - s = s.slice(triple.index + triple[0].length); - } - } - else - { - data.smarty.config[f[2]] = trimQuotes(f[3]); - } - } - var newln = s.match(/\n+/); - if (newln) - { - s = s.slice(newln.index + newln[0].length); - } - else - { - break; - } + s = s.slice(f.index+f[0].length); + if (f[1]) + { + currSect = f[1]; + } + else if ((!currSect || currSect == section) && currSect.substr(0,1) != '.') + { + if (f[3] == '"""') + { + var triple = s.match(/"""/); + if (triple) + { + data.smarty.config[f[2]] = s.slice(0,triple.index); + s = s.slice(triple.index + triple[0].length); + } + } + else + { + data.smarty.config[f[2]] = trimQuotes(f[3]); + } + } + var newln = s.match(/\n+/); + if (newln) + { + s = s.slice(newln.index + newln[0].length); + } + else + { + break; + } } } @@ -2248,10 +2248,10 @@ data[append].push(content); } } - else - { - data[append] = [content]; - } + else + { + data[append] = [content]; + } } } return ''; @@ -2424,7 +2424,7 @@ dbgWnd.document.write(" \ \ \ - jSmart Debug Console \ + jSmart Debug Console \