Comments (2)
Can you please show me some code demo to explain ?
from puja.
Code at this moment:
class PujaException extends Exception{}
class PujaCompiler{
var $template_dir = 'templates/';
var $cache_dir;
var $cache_level;
var $parse_executer = 'include';
var $custom_filter;
var $custom_tags;
var $debug = false;
var $headers = array();
var $data_only_array = false;
var $include_multi_level = true;
var $extends_multi_level = true;
var $_custom_filter;
var $_custom_tags;
var $_filter;
var $_tags;
var $_core_matches;
var $_operators = array(' and ',' or ', ' not ',' in ',' is ', '%','!==','!=','>=','<=','===','==','<>','>','<','&&','||','!','+','-','*','/','=',';','__seperate__','__array_split__');
var $_cache;
var $_include_content = array();
var $_mtime = 0;
var $_data = array();
function __construct(){
if(ini_get('magic_quotes_gpc')) ini_set('magic_quotes_gpc',false);
if(ini_get('magic_quotes_runtime')) ini_set('magic_quotes_runtime',false);
}
/**
* Get template callback
* @param Array $matches
*/
function get_template_content_callback($matches){
return isset($this->_data[$matches[1]])?$this->_data[$matches[1]]:null;
}
/**
* Get template content
* @param string $tpl_file
* @throws Exception
* @return string template content
* @todo: add instant variable {{ $skin }}
*/
function get_template_content($tpl_file){
$tpl_file = $this->remove_quote($tpl_file);
if(!file_exists($this->template_dir.$tpl_file)){
throw new PujaException('Template <strong>'.$this->template_dir.$tpl_file.'</strong> doesn\'t exists!');
}
if($this->cache_level == 1){
$mtime = filemtime($this->template_dir.$tpl_file);
if($mtime > $this->_mtime) $this->_mtime = $mtime;
}
$content = file_get_contents($this->template_dir.$tpl_file);
$content = str_replace(array('\{#','\{$','\{{','\{%'),array('[:lpuja_comment:]','[:lpuja_specialvar:]','[:lpuja_variable:]','[:lpuja_percent:]'),$content);
//remove template comment
$content = preg_replace('/\{\#\s?(.*?)\s?\#\}/','',$content);
// parse instant variable
$content = preg_replace_callback('/\{\$\s*([a-z0-9\_]*?)\s*\$\}/i',array($this,"get_template_content_callback"),$content);
if($this->debug){
$template_debug = new TemplateDebug;
$template_debug->operators = $this->_operators;
$template_debug->content = $content;
$template_debug->tpl_file = $this->template_dir.$tpl_file;
$template_debug->valid_syntax();
}
return $content;
}
...
}
I think, next example more compatible with PSR code style...
- CamelCase vs under_score.
- var vs public / protected / private
- Indents
<?php
class PujaException extends Exception
{
}
class PujaCompiler
{
public $templateDir = 'templates/';
public $cacheDir;
public $cacheLevel;
public $parseExecuter = 'include';
public $customFilter;
public $customTags;
public $debug = false;
public $headers = array();
public $dataOnlyArray = false;
public $includeMultiLevel = true;
public $extendsMultiLevel = true;
private $_customFilter;
private $_customTags;
private $_filter;
private $_tags;
private $_coreMatches;
private $_operators = array(' and ', ' or ', ' not ', ' in ', ' is ', '%', '!==', '!=', '>=', '<=', '===', '==', '<>', '>', '<', '&&', '||', '!', '+', '-', '*', '/', '=', ';', '__seperate__', '__array_split__');
private $_cache;
private $_includeContent = array();
private $_mtime = 0;
private $_data = array();
public function __construct()
{
if (ini_get('magic_quotes_gpc')) {
ini_set('magic_quotes_gpc', false);
}
if (ini_get('magic_quotes_runtime')) {
ini_set('magic_quotes_runtime', false);
}
}
/**
* Get template callback
* @param Array $matches
*/
public function getTemplateContentCallback($matches)
{
return isset($this->_data[$matches[1]]) ? $this->_data[$matches[1]] : null;
}
/**
* Get template content
* @param string $tplFile
* @throws Exception
* @return string template content
* @todo: add instant variable {{ $skin }}
*/
public function getTemplateContent($tplFile)
{
$tplFile = $this->removeQuote($tplFile);
if (!file_exists($this->templateDir . $tplFile)) {
throw new PujaException('Template <strong>' . $this->templateDir . $tplFile . '</strong> doesn\'t exists!');
}
if ($this->cacheLevel == 1) {
$mtime = filemtime($this->templateDir . $tplFile);
if ($mtime > $this->_mtime) {
$this->_mtime = $mtime;
}
}
$content = file_get_contents($this->templateDir . $tplFile);
$content = str_replace(array('\{#', '\{$', '\{{', '\{%'), array('[:lpuja_comment:]', '[:lpuja_specialvar:]', '[:lpuja_variable:]', '[:lpuja_percent:]'), $content);
//remove template comment
$content = preg_replace('/\{\#\s?(.*?)\s?\#\}/', '', $content);
// parse instant variable
$content = preg_replace_callback('/\{\$\s*([a-z0-9\_]*?)\s*\$\}/i', array($this, "getTemplateContentCallback"), $content);
if ($this->debug) {
$template_debug = new TemplateDebug;
$template_debug->operators = $this->_operators;
$template_debug->content = $content;
$template_debug->tplFile = $this->templateDir . $tplFile;
$template_debug->validSyntax();
}
return $content;
}
}
What do you think about this?
from puja.
Related Issues (10)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from puja.