Xtpl (Ajax Template Transformer)
PHP/Ajax Template Render ClassXtpl, a basic powerful PHP/Ajax class for HTML rendering, built on PHP >= 5.6, using shortcode concept to replace template's placeholders with data and generate HTML response for Ajax, Similar to Symfony renderView, but comes with new features. Xtpl is a Bundle of FloatPHP Framework, works mostly in FloatPHP projects (based on Twig template engine), and also compatible with non-frameworked projects. Available as Symfony Bundle & Laravel Package.
View on GithubWhich projects ?
The perfect example for Xtpl usecase, is Ajax Load More System :
by pressing load more button, after an Ajax call, Controller fetch data from database and generate HTML response that will be shown in client side using jQuery append function. So in place of (Hardcoding) building HTML result in Controller and return content, you can simply define shortcodes in target (HTML Template), then use Xtpl class to return rendered result content.
Why use Xtpl ?
By using it, editing templates will be simple, because HTML result is separated from logical, and controller is quickly maintainable. And in big term, Xtpl is dedicated for keeping clear MVC Pattern.
How it works ?
The basic concept is on shortcodes and keys of result SQL arrays, the class transform shortcodes names to value of data with the same names of array keys, for example; in template the shortcode <span>{name}</span>
is placed, and in SQL result 2D Array ['name'=>'John'] etc, then rendered result gonna be <span>John</span>
.
How to install it?
{
"require": {
"Jakiboy/Xtpl": "0.1.0"
}
}
$ composer install
How to use?
Basic usage :
include('Xtpl.php'); // Our class
$multiple; // SQL 2D Array : (SELECT * FROM example)
$x = new Xtpl('path/to/template.xtpl'); // instance with template path
echo $x->transformAll($multiple)->response; // response for Ajax call
What methods ?
transformAll($array)
Array $array
Transform all shortcodes with entities (attributes), shortcodes replacements are defined automatically, shortcode Must be compatible with returned data, eg. if in data array an attribute is named phone, shortcode in template Must be {phone}, or you can customize shortcode with setDelimiter method. transformAll method works onlly on 2D array, as an SQL response.
echo $x->transformAll($multiple)->response;
transform($array)
Array $array
Works only on 1D array as a single shifted array.
echo $x->transform($single)->response;
setDelimiter($delimiterX,$delimiterY)
String $delimiterX, $delimiterY
Customize shortcodes, or even use multiple shortcodes : {shortcode}, [shortcode], <shortcode> ...
$x->setDelimiter('[',']');
shortcodeIn($shortcode)
Return boolean
Return boolean of shortcode existence on template
$x->shortcodeIn('{name}'); // return false|true
clear()
Clean result from undefined shortcode
echo $x->transformAll($multiple)->clear()->response;
Other Use case ?
Usage for static method :
echo Xtpl::build('template.tpl')->transformAll($multiple)->response;
Transform all with extra shortcode :
$x->transformAll($multiple);
$x->transform(['date'=>'22/10/2017']); // {date}
echo $x->response;
Transform method on 2D array using loop :
foreach($multiple as $single)
{
$x->transform($single);
}
echo $x->response;
Separated transform method on non 2D array :
echo $x->transform(['id' => '1']) // $id
->transform(['name' => 'john']) // $name
->transform(['mail' => 'john@mail.com']) // $email
->transform(['phone'=> '012345678']) // $phone
->response;
Both transform and transformAll on 2D & 1D array :
echo $x->transformAll($multiple)->transform($single)->response;
FAQ
Hwo to render view for Ajax request using Symfony (Twig) ?
I'ts quite simple,
echo $this->render(
'TestBundle:Test:testTemplate.html.twig', // Ajax template
$single // 1D array
);
Hwo to render view for Ajax request using Laravel ?
I'ts also simple,
echo view(
"testTemplate.ajax", // Ajax template
compact('single','test') // 1D array passed
)->render();
TODO
- Make Xtpl works on Multi-dimensional Array
Coded by | JIHAD SINNAOUR (Jakiboy) with