protected function tpl() { $config = array('tpl_dir' => APP_PATH . 'widgets/' . $this->name . '/', 'cache_dir' => CACHE_PATH, 'tpl_ext' => 'tpl', 'auto_escape' => false); $view = new Tpl(); $view->objectConfigure($config); $view->assign('c', $this); return $view; }
<?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");
/** * 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); } } }
<?php // include require "library/Rain/autoload.php"; // namespace use Rain\Tpl; // config $config = array("tpl_dir" => "templates/simple/", "cache_dir" => "cache/", "debug" => true); Tpl::configure($config); // Add PathReplace plugin (necessary to load the CSS with path replace) Tpl::registerPlugin(new Tpl\Plugin\PathReplace()); // create the Tpl object $tpl = new Tpl(); // assign a variable $tpl->assign("name", "Obi Wan Kenoby"); // assign an array $tpl->assign("week", array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")); // draw the template $tpl->draw("simple_template");
<?php // include require "library/Rain/autoload.php"; // namespace use Rain\Tpl; // config $config = array("base_url" => null, "tpl_dir" => "templates/nested_loop/", "cache_dir" => "cache/", "debug" => true); Tpl::configure($config); // Add PathReplace plugin Tpl::registerPlugin(new Tpl\Plugin\PathReplace()); $user = array(array('name' => 'Jupiter', 'color' => 'yellow', 'orders' => array(array('order_id' => '123', 'order_name' => 'o1d'), array('order_id' => '1sn24', 'order_name' => 'o2d'))), array('name' => 'Mars', 'color' => 'red', 'orders' => array(array('order_id' => '3rf22', 'order_name' => '¶©µ¥Aj'))), array('name' => 'Empty', 'color' => 'blue', 'orders' => array()), array('name' => 'Earth', 'color' => 'blue', 'orders' => array(array('order_id' => '2315', 'order_name' => '¶©µ¥15'), array('order_id' => 'rf2123', 'order_name' => '¶©µ¥215'), array('order_id' => '0231', 'order_name' => '¶©µ¥315'), array('order_id' => 'sn09-0fsd', 'order_name' => '¶©µ¥45415')))); // draw $tpl = new Tpl(); $tpl->assign("user", $user); echo $tpl->draw("test"); class Test { public static function method($variable) { echo "Hi I am a static method, and this is the parameter passed to me: {$variable}!"; } } // end