Giter Site home page Giter Site logo

jerry-goodboy / jsmart Goto Github PK

View Code? Open in Web Editor NEW

This project forked from miroshnikov/jsmart

0.0 1.0 0.0 1.47 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.

HTML 0.44% JavaScript 79.44% ApacheConf 0.04% PHP 1.75% CSS 1.35% Smarty 16.98%

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 a standalone JavaScript interpreter or CommonJS environments like [node.js] (http://nodejs.org).

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.

##A Quick Introduction

  • Include jSmart library Javascript file in your header
<html>
    <head>
      <script language="javascript" src="smart-2.11.min.js"></script>
    </head>
  • Create template, use PHP Smarty 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.
<script id="test_tpl" type="text/x-jsmart-tmpl">
 
   <h1>{$greeting}</h1>

   {foreach $books as $i => $book}
      <div style="background-color: {cycle values="cyan,yellow"};">
         [{$i+1}] {$book.title|upper} by {$book.author} 
            {if $book.price}                                
               Price: <span style="color:red">${$book.price}</span>
            {/if}                                           
      </div>
   {foreachelse}
      No books
   {/foreach}

   Total: {$book@total}

</script>
  • Create JavaScript data object with variables to assign to the template
<script>

    var data = {
       greeting: 'Hi, there are some JScript books you may find interesting:',
       books : [
          {
             title  : 'JavaScript: The Definitive Guide',          
             author : 'David Flanagan',                            
             price  : '31.18'
          },
          {
             title  : 'Murach JavaScript and DOM Scripting',
             author : 'Ray Harris',
          },
          {
             title  : 'Head First JavaScript',
             author : 'Michael Morrison',
             price  : '29.54'
          }
       ]      
    };

</script>
  • 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
<script>
   var tplText = document.getElementById('test_tpl').innerHTML;
   var tpl = new jSmart( tplText );
   var res = tpl.fetch( data );

   /* or fetch straigth from JavaScript string */
   var res = document.getElementById('test_tpl').innerHTML.fetch(data);
   
   document.write( res );
</script>
  • The result would be
<h1>Hi, there are some JScript books you may find interesting:</h1>

<div style="background-color: cyan;">
   [1] JAVASCRIPT: THE DEFINITIVE GUIDE by David Flanagan 
   <span style="color:red">$31.18</span>
</div>

<div style="background-color: yellow;">
   [2] MURACH JAVASCRIPT AND DOM SCRIPTING by Ray Harris 
</div>

<div style="background-color: cyan;">
   [3] HEAD FIRST JAVASCRIPT by Michael Morrison 
   <span style="color:red">$29.54</span>
</div>

Total: 3
  • 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!

jsmart's People

Contributors

miroshnikov avatar

Watchers

 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.