Coder Social home page Coder Social logo

b2blosangeles / jsmart Goto Github PK

View Code? Open in Web Editor NEW

This project forked from miroshnikov/jsmart

0.0 2.0 0.0 1.52 MB

JavaScript template engine, port of the PHP template engine Smarty to Javascript, lets you use the same templates for PHP and JavaScript, on both server and client-side.

License: MIT License

HTML 1.34% PHP 5.38% JavaScript 37.05% CSS 4.15% Smarty 52.08%

jsmart's Introduction

Logo Smarty for JavaScript Templates

jSmart is a port of the Smarty Template Engine to Javascript, a JavaScript template library that supports the template syntax and all the features (functions, variable modifiers, etc.) of the well-known PHP template engine [Smarty] (http://www.smarty.net).

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 Node.js.

jSmart supports plugin architecture, you can extend it with custom plugins: functions, blocks and variable modifiers, templates inclusion, templates inheritance and overriding, caching, escape HTML.

jSmart has some limited support of the PHP syntax and allows you to use the same Smarty templates on both server and client side, for both PHP and Javascript.

See the overview of the basic syntax of jSmart templates here

Demo page play with it at JsFiddle

Discussion board feel free to ask questions, share your ideas, etc.

##Usage

  • Browser
<script type="text/javascript" src="smart.min.js"></script>
<script id="tpl" type="text/x-smarty-tmpl">
   Hello {$who}!
</script>

<script>
   var tplText = document.getElementById('tpl').innerHTML;
   var compiled = new jSmart( tplText );
   var res = compiled.fetch( { who:'world' } );  //res = Hello world!
</script>
  • Node.js
npm install smarty.js
var Smarty = require('smarty.js');
var fs = require('fs');

Smarty.prototype.getTemplate = function(name) {
   return fs.readFileSync('./templates/'+name, {encoding: 'utf-8'});
}

var tplText = fs.readFileSync('./templates/main.tpl', {encoding: 'utf-8'});
var compiled = new Smarty(tplText);
var res = compiled.fetch({...});
  • Require.js
require(['smart'], function(Smarty){
   var compiled = new Smarty("Hello {$who}!");
   var res = compiled.fetch({who:'world'});
});
  • Bower
bower install smarty

Note: The template's text is compiled in the constructor, so it's fast to call fetch() with different assigned variables many times.

var compiled = new jSmart( '{$greeting}, {$name}!' );
compiled.fetch( {greeting:'Hello', name:'John'} ); //Hello, John!
compiled.fetch( {greeting:'Hi', name:'Jane'} );    //Hi, Jane!

jsmart's People

Contributors

miroshnikov avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.