public function haml($content = '') { $parser = new HamlParser(); //$this->template['content'] = $parser->setFile(APPPATH.'/views/'.$content); echo $parser->setFile(APPPATH . '/views/' . $content); //return $this; }
public function testTemplateReentrancy() { $this->parser->assign("a", 1); $parser2 = new HamlParser(); $this->assertEquals(0, count($parser2->getVariables())); $this->assertEquals(1, count($this->parser->getVariables())); $parser2->setSource('= $var'); $this->assertEquals("Hello", $parser2->render(array('var' => "Hello"))); }
function render($template) { $args = func_get_args(); $haml = new HamlParser(array('style' => 'nested', 'ugly' => false, 'debug' => false, 'escapeHtml' => true)); $php = $haml->parse($template); file_put_contents('/tmp/tmpl.php', $php); foreach (array_slice($args, 1) as $arr) { extract($arr); } ob_implicit_flush(false); ob_start(); require '/tmp/tmpl.php'; return ob_get_clean(); }
/** * Test attribute reparing. phpHaml can repair: * - value without quotation marks (eg. { :bgcolor => green }) * - value without quotation marks followed by colon (same syntax like key eg. { :align => :center } */ public function testAttributeRepair() { $this->markTestIncomplete(); return; $this->parser->setSource('%b{ :bgcolor => green, :align => :center } Hello, World!'); $this->assertEquals('<b align="center" bgcolor="green">Hello, World!</b>', $this->parser->fetch()); }
function render_template($name) { $tmp_dir = Wordless::theme_temp_path(); $template_path = Wordless::join_paths(Wordless::theme_views_path(), "{$name}.haml"); if (!is_file($template_path)) { render_error("Template missing", "<strong>Ouch!!</strong> It seems that <code>{$template_path}</code> doesn't exist!"); } if (!file_exists($tmp_dir)) { mkdir($tmp_dir, 0760); } if (!is_writable($tmp_dir)) { chmod($tmp_dir, 0760); } if (is_writable($tmp_dir)) { $haml = new HamlParser(array('style' => 'expanded', 'ugly' => false)); include $haml->parse($template_path, $tmp_dir); } else { render_error("Temp dir not writable", "<strong>Ouch!!</strong> It seems that the <code>{$tmp_dir}</code> directory is not writable by the server! Go fix it!"); } }
/** * Setup Haml and support configure * * @param unknown_type $oParent * @param unknown_type $aDebug * @param unknown_type $bInside */ function __construct($view, $oParent = null, $aDebug = null, $bInside = false) { $this->cakeView = $view; $config = Configure::read('HAML'); $this->isDebug($config['debug']); $this->indentBy = $config['contentIndent']; $this->showCacheTime = $config['showCacheTime']; $this->compress = $config['compressHTML']; $this->noCache = $config['noCache']; parent::__construct(VIEWS, TMP . 'haml', $oParent, $aDebug, $bInside); }
function haml_layout($file, &$controller) { // –егистраци¤ переменных $framework =& $controller->framework; $data =& $controller->data; $layout =& $controller; $fragment =& $controller->fragment; // »нициализаци¤ парсера // ”казываем расположение шаблонов // ”казываем путь, где будут хранитьс¤ скомпилированные шаблоны $parser = new HamlParser(LAYOUT_PATH, HAML_COMPILED_TEMPLATES_PATH); $parser->setSource(file_get_contents(LAYOUT_PATH . "{$file}.haml")); $php_code = $parser->get_php_from_haml(); // ¬џѕќЋЌ≈Ќ»≈ ЎјЅЋќЌј $code = ' ?>' . $php_code . '<?php '; $output = ''; ob_start(); eval($code); $output = ob_get_contents(); ob_end_clean(); echo $output; }
function __construct() { if (file_exists(Trax::$vendor_path . "/phphaml/includes/haml/HamlParser.class.php")) { $haml_compile_path = Trax::$tmp_path . "/haml"; if (!is_dir($haml_compile_path)) { exec("mkdir -p {$haml_compile_path}"); } include_once Trax::$vendor_path . "/phphaml/includes/haml/HamlParser.class.php"; HamlParser::$bRegisterSass = false; $this->parser = new HamlParser(false, $haml_compile_path); } else { Trax::$current_controller_object->raise("Missing phphaml in " . Trax::$vendor_path . "<br />Please run 'git clone git@github.com:phpontrax/phphaml.git' from inside your apps vendor folder.", "HAML parser", "500"); } }
/** * Renders a template and its contained plartials. Accepts * a list of locals variables which will be available inside * the code of the template * * @param string $name The template filenames (those not starting * with an underscore by convention) * * @param array $locals An associative array. Keys will be variables' * names and values will be variable values inside * the template * * @see php.bet/extract * */ function render_template($name, $locals = array()) { $valid_filenames = array("{$name}.html.haml", "{$name}.haml", "{$name}.html.php", "{$name}.php"); foreach ($valid_filenames as $filename) { $path = Wordless::join_paths(Wordless::theme_views_path(), $filename); if (is_file($path)) { $template_path = $path; $arr = explode('.', $path); $format = array_pop($arr); break; } } if (!isset($template_path)) { render_error("Template missing", "<strong>Ouch!!</strong> It seems that <code>{$name}.html.haml</code> or <code>{$name}.html.php</code> doesn't exist!"); } extract($locals); switch ($format) { case 'haml': $tmp_dir = Wordless::theme_temp_path(); if (!file_exists($tmp_dir)) { mkdir($tmp_dir, 0760); } if (!is_writable($tmp_dir)) { chmod($tmp_dir, 0760); } if (is_writable($tmp_dir)) { $haml = new HamlParser(array('style' => 'expanded', 'ugly' => false)); include $haml->parse($template_path, $tmp_dir); } else { render_error("Temp dir not writable", "<strong>Ouch!!</strong> It seems that the <code>{$tmp_dir}</code> directory is not writable by the server! Go fix it!"); } break; case 'php': include $template_path; break; } }
public function render($page='',$render_params=array(),$template='',$return=false){ //some variables for the page to use $node = $this->parent; if($page=='') $page=$node->page['uri']; if($template==='') $template=$node->page['template']; $tplCacheDir=$node->ini['front']['cache_dir'].'tpl'; //TODO:for some reason this doesn't work...I think it's a big in the parser if($node->ini['front']['debug_haml']) $hamlOpts=array('style'=>'nested', 'ugly'=>true); else $hamlOpts=array(); $curPage=$node->root_http_path.$page; if(isset($node->query['page'])) $toRoot=str_repeat('../',count(explode('/',$node->query['page']))-1); else $toRoot=''; $headerHTML=implode($node->page['headerHTML']); //set $cancelPage to true in your tpl.php if you want to cancel loading the page $cancelPage=false; //load backend extra php if available for template then page if(file_exists($node->ini['front']['template_dir'].$template.'.php')) include $node->ini['front']['template_dir'].$template.'.php'; if(!$cancelPage){ if(file_exists($node->ini['front']['template_dir'].$page.'.php')) include $node->ini['front']['template_dir'].$page.'.php'; if(!$cancelPage){ //load page content $page_filename=$node->ini['front']['template_dir'].$page; ob_start(); $page_haml_cache=$tplCacheDir.'/'.preg_replace(array('/^.\//','/\//','/.haml/'),array('','-',''),$page_filename).'.php'; if(file_exists($page_haml_cache) && file_exists($page_filename.'.haml') && (@filemtime($page_haml_cache) > filemtime($page_filename.'.haml'))){ include $page_haml_cache; }elseif(file_exists($page_filename.'.haml')){ require_once $this->parent->fs_path.'lib/haml/HamlParser.php'; if(!isset($haml)) $haml = new HamlParser($hamlOpts); include $haml->parse($page_filename.'.haml', $tplCacheDir); }elseif(file_exists($page_filename.'.phtml')){ include $page_filename.'.phtml'; } $content=ob_get_clean(); }else $content=''; }else $content=''; //load template or dump content $template_filename=$node->ini['front']['template_dir'].$template; $template_haml_cache=$tplCacheDir.'/'.preg_replace(array('/^.\//','/\//','/.haml/'),array('','-',''),$template_filename).'.php'; if($return){ return $content; }elseif($template && file_exists($template_haml_cache) && (@filemtime($template_haml_cache) > filemtime($template_filename.'.haml'))){ include $template_haml_cache; }elseif($template && file_exists($template_filename.'.haml')){ require_once $this->parent->fs_path.'lib/haml/HamlParser.php'; if(!isset($haml)) $haml = new HamlParser($hamlOpts); include $haml->parse($template_filename.'.haml', $tplCacheDir); }elseif($template && file_exists($template_filename.'.phtml')){ include $template_filename.'.phtml'; }else echo $content; }
/** * Implements singleton pattern * * @see HamlParser::__construct() * @param string Path to files * @param boolean/string Compile templates (can be path) * @return HamlParser */ protected static function getInstance($sPath = false, $bCompile = true) { if (is_null(self::$oInstance)) { self::$oInstance = new self($sPath, $bCompile, null, null, true); } return self::$oInstance; }
/** * Create and append line to parent * * @param string Line * @param object Parent parser * @return object */ public function createLine($sLine, $parent) { $oHaml = new HamlParser($this->sPath, $this->bCompile, $parent); $oHaml->setSource($sLine); $oHaml->iIndent = $parent->iIndent + 1; $parent->aChildren[] = $oHaml; return $oHaml; }
<?php /** * Example with many features. * * @author Amadeusz Jasak <*****@*****.**> * @package phpHaml * @subpackage Examples */ require_once './includes/haml/HamlParser.class.php'; $parser = new HamlParser('./tpl', './tmp/haml'); class ConfigModel { public $ID; public $name; public $value; public function __construct($ID, $name, $value) { $this->ID = $ID; $this->name = $name; $this->value = $value; } public function getID() { return $this->ID; } } $models = array(); for ($i = 1; $i <= 100; $i++) { $models[] = new ConfigModel($i, md5($i), md5(uniqid(rand()))); }
/** * Test tag with dash in name */ public function testTagWithDash() { $this->parser->setSource('%foo-bar Hello, World!'); $this->assertEquals('<foo-bar>Hello, World!</foo-bar>', $this->parser->fetch()); }
function render_partial($template, $data) { $haml = new HamlParser('./templates', SITE_ROOT . '/tmp/haml'); $haml->append($data); return $haml->setFile($template); }
<?php /** * Including by variable * * @author Amadeusz Jasak <*****@*****.**> * @package phpHaml * @subpackage Examples */ require_once './includes/haml/HamlParser.class.php'; $parser = new HamlParser('./tpl', './tmp/haml'); $parser->assign('menu', 'my_menu'); echo $parser->setFile('example9.haml');
/** * Removes variables * * @return object */ public function clearVariables() { self::$aVariables = array(); return $this; }
<?php /** * Multi level including example * * @author Amadeusz Jasak <*****@*****.**> * @package phpHaml * @subpackage Examples */ require_once './includes/haml/HamlParser.class.php'; $parser = new HamlParser('./tpl', './tmp/haml'); echo $parser->setFile('example7.haml');
<?php /** * Text blocks example * * @author Amadeusz Jasak <*****@*****.**> * @package phpHaml * @subpackage Examples */ require_once './includes/haml/HamlParser.class.php'; $parser = new HamlParser('./tpl', './tmp/haml'); $parser->registerBlock('md5', 'md5'); echo $parser->setFile('example4.haml');
public function testFrameBorder() { $this->parser->setSource('%iframe{ :frameBorder => "0" } Hello!!'); $this->assertEquals('<iframe frameBorder="0">Hello!!</iframe>', $this->parser->fetch()); }
/** * Render Haml. Append globals variables * * Simple way to use Haml * <code> * echo HamlParser::haml('%strong Hello, World!'); // <strong>Hello, World!</strong> * $foo = 'bar'; // This is global variable * echo Haml::haml('%strong= "Foo is $foo"'); // <strong>Foo is bar</strong> * </code> * * @param string Haml source * @return string xHTML */ public static function haml($sSource) { static $__haml_parser; if (!$__haml_parser) { $__haml_parser = new HamlParser(); } $__haml_parser->setSource($sSource); $__haml_parser->append($GLOBALS); return $__haml_parser->fetch(); }
function parse_haml($input) { $haml = new HamlParser(array('style' => 'nested', 'ugly' => false)); $html = $haml->parse($input); echo $html; }
/** * Test very_basic.haml */ public function testVeryBasic() { $this->parser->setSource($this->getModifiedFile('very_basic', array(1 => '!!! Transitional'))); $this->assertEquals($this->getResults('very_basic', array(3 => ' <head></head>')), $this->parser->fetch() . "\n"); }