function __construct($tpl) { $this->tpl = $tpl; $config = array("tpl_dir" => "view/", "cache_dir" => "template_cache/"); //add url tag parent::registerTag("({@.*?@})", "{@(.*?)(?:\\|(.*?))@}", function ($params) { // function called by the tag $tag = $params[1][0]; $arg = $params[2][0]; return Yeast::generateURI($tag, $arg); }); parent::configure($config); }
<?php // namespace use Rain\Tpl; // include include "library/Rain/Tpl.php"; // configure $config = array("base_url" => null, "tpl_dir" => "templates/", "cache_dir" => "cache/", "debug" => true, "auto_escape" => true); Tpl::configure($config); // set variables $var = array("variable" => "Hello World!", "version" => "3.0 Alpha", "menu" => array(array("name" => "Home", "link" => "index.php", "selected" => true), array("name" => "FAQ", "link" => "index.php/FAQ/", "selected" => null), array("name" => "Documentation", "link" => "index.php/doc/", "selected" => null)), "week" => array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"), "user" => (object) array("name" => "Rain", "citizen" => "Earth", "race" => "Human"), "numbers" => array(3, 2, 1), "bad_text" => 'Hey this is a malicious XSS <script>alert(1);</script>', "table" => array(array("Apple", "1996"), array("PC", "1997")), "title" => "Rain TPL 3 - Easy and Fast template engine", "copyright" => "Copyright 2006 - 2012 Rain TPL<br>Project By Rain Team"); // add a function Tpl::register_tag("({@.*?@})", "{@(.*?)@}", function ($params) { // function called by the tag $value = $params[0]; return "Translate: <b>{$value}</b>"; }); // draw $tpl = new Tpl(); $tpl->assign($var); echo $tpl->draw("bootstrap/hero");
error_reporting(E_ERROR); include_once "config.php"; require "lib/Mobile_Detect.php"; require "lib/Rain/autoload.php"; use Rain\Tpl; // Detect if Mobile or Computer is used $detect = new Mobile_Detect(); $mobile = $detect->isMobile() || isset($_GET['mobile']) && $_GET['mobile'] == 1; // Use template ? if ($config["useTemplate"]) { // Templace RainTPL if (!is_dir('cache')) { mkdir('cache', 0705); chmod('cache', 0705); } Tpl::configure(array("tpl_dir" => array($config["template"]["tpl_dir"], $config["template"]["tpl_dir"]), "cache_dir" => "cache/", "debug" => false)); // Add PathReplace plugin (necessary to load the CSS with path replace) Tpl::registerPlugin(new Tpl\Plugin\PathReplace()); // create the Tpl object $tpl = new Tpl(); header("Vary: User-Agent"); if ($mobile) { $tpl->draw($config["template"]["mobile"]); } else { $tpl->draw($config["template"]["desktop"]); } } else { header("Vary: User-Agent"); if ($mobile) { //readfile("tpl/teleinfo.tabs.mobile.html"); readfile($config["notemplate"]["mobile"]);
protected function initTemplateEngine($params) { $defaults = array('tpl_dir' => "{$this->params['templates_dir']}/", 'cache_dir' => sys_get_temp_dir() . '/'); $params = array_merge($defaults, $params); // init template engine $tpl = new Tpl(); $tpl->configure($params); return $tpl; }
/** * Mostramos todas las plantillas cargadas * @param boolean $return Indica si retornar o no el HTML generado * @return nothing */ public static function show() { if (count(self::$templates) >= 1) { $dir_theme = 'views' . DS . 'html' . DS; $dir_base = explode('/', $_SERVER['REQUEST_URI']); array_shift($dir_base); array_pop($dir_base); $dir_base = '/' . implode('/', $dir_base) . '/'; self::add_key('site', get_config('site')); self::add_key('core_files', self::$files); self::add_key('core_paths', array('theme' => $dir_theme, 'base' => $dir_base)); self::add_key('site', get_config('site')); // Instanciamos RainTPL $rain = new RainTpl(); // Configuramos Rain para trabajar RainTpl::configure('base_url', $_SERVER['SERVER_NAME'] . $dir_base); RainTpl::configure('tpl_dir', 'views' . DS . 'html' . DS); RainTpl::configure('cache_dir', CACHE_DIR . 'html' . DS); $rain->assign('lang', self::load_language(self::$configuration['lang'])); $rain->assign(self::$variables); if (self::$configuration['start_with'] !== null) { $rain->draw(self::$configuration['start_with'], false); } // Recorremos el arreglo de plantillas y las vamos mostrando foreach (self::$templates as $template) { $rain->draw($template, false); } if (self::$configuration['end_with'] !== null) { $rain->draw(self::$configuration['end_with'], false); } } else { //TODO: Considerar idiomas if (count(self::$variables) >= 1) { echo json_encode(self::$variables); } } }