Giter VIP home page Giter VIP logo

jquery-tmpl-php's Introduction

jquery-tmpl-php

An attempt at implementing jquery-tmpl in PHP.

Syntax

jquery-tmpl is implemented by converting template code into valid JavaScript. As such, a number of the features do not translate well to PHP, such as the variety of places you can insert a function into jquery-tmpl template tags. The jqTmpl class supports the following subset of jquery-tmpl syntax:

  • ${foo}, {{= foo}}
  • {{html bar}}
  • {{if foo}}
  • {{else}}, {{else param}}
  • {{! comment}}
  • {{tmpl "selector"}}, ie. {{tmpl "#baz"}}
  • {{each foo}}, {{each(k,v) foo}}

Known unsupported:

  • {{tmpl(foo) "#bar"}} -- data parameter to nested template
  • {{= func()}} -- functions in template tags
  • {{= $item.func()}} -- functions in options parameter to jqTmpl::tmpl()

Dependencies

  • html5lib -- "A Python and PHP implementations of a HTML parser based on the WHATWG HTML5 specification for maximum compatibility with major desktop web browsers."

Optional:

  • PHPUnit -- running tests: make test or phpunit t/jqTmpl.php

Examples

<?php

include 'jqTmpl.class.php';
$t = new jqTmpl;

$html = <<<EOF
<script id="movieTemplate" type="text/x-jquery-tmpl"> 
	{{tmpl "#titleTemplate"}}
	<tr class="detail"><td>Director: \${Director}</td></tr>
</script>

<script id="titleTemplate" type="text/x-jquery-tmpl"> 
	<tr class="title"><td>\${Name}</td></tr>
</script>
EOF;

$t->load_document($html);

$data = array(
	(object)array("Name" => "The Red Violin", "Director" => "Francois Girard"),
	(object)array("Name" => "Eyes Wide Shut", "Director" => "Stanley Kubrick"),
	(object)array("Name" => "The Inheritance", "Director" => "Mauro Bolognini")
);

echo $t->tmpl( '#movieTemplate', $data );

Output (whitespace adjusted):

<tr class="title"><td>The Red Violin</td></tr>
<tr class="detail"><td>Director: Francois Girard</td></tr>

<tr class="title"><td>Eyes Wide Shut</td></tr>
<tr class="detail"><td>Director: Stanley Kubrick</td></tr>

<tr class="title"><td>The Inheritance</td></tr>
<tr class="detail"><td>Director: Mauro Bolognini</td></tr>

See Also

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.