function test_error_with_css() { $text = " div { font-weight: bold; } "; $template = new LiquidTemplate(); $template->parse($text); $this->assertEqual($text, $template->render()); $this->assertIsA($template->root->nodelist[0], 'string'); }
function test_override_global_filter() { $template = new LiquidTemplate(); $template->register_filter(new GlobalFilter()); $template->parse("{{'test' | notice }}"); $this->assertEqual('Global test', $template->render()); $this->assertEqual('Local test', $template->render(array(), new LocalFilter())); }
function test_local_global() { $template = new LiquidTemplate(); $template->register_filter(new MoneyFilter()); $template->parse('{{1000 | money}}'); $this->assertIdentical(' 1000$ ', $template->render()); $this->assertIdentical(' 1000$ CAD ', $template->render(null, new CanadianMoneyFilter())); }
function test_array_scoping() { $template = new LiquidTemplate(); $template->parse('{{ test.test }}'); $this->assertEqual('worked', $template->render(array('test' => array('test' => 'worked')))); // this wasn't working properly in if tests, test seperately $template->parse('{{ foo.bar }}'); $this->dump($template->render(array('foo' => array()))); }
function test_with_block() { $template = new LiquidTemplate(); $template->parse(' {% comment %} {% endcomment %} '); $this->assertEqual(3, count($template->root->nodelist)); $this->assertIsA($template->root->nodelist[0], 'string'); $this->assertIsA($template->root->nodelist[1], 'CommentLiquidTag'); $this->assertIsA($template->root->nodelist[2], 'string'); }
function test_with_block() { $template = new LiquidTemplate(); $template->parse(' {% comment %} {% endcomment %} '); $nodelist = $template->getRoot()->getNodelist(); $this->assertEqual(3, count($nodelist)); $this->assertIsA($nodelist[0], 'string'); $this->assertIsA($nodelist[1], 'LiquidTagComment'); $this->assertIsA($nodelist[2], 'string'); }
function assert_template_result($expected, $template, $assigns = null, $message = "%s", $debug = false) { if (is_null($assigns)) { $assigns = array(); } $result = new LiquidTemplate(); $result->parse($template); if ($debug) { debug($result); } $this->assertEqual($expected, $result->render($assigns, $this->filters), $message); }
function test_tokenize_blocks() { $this->assertEqual(array('{%comment%}'), LiquidTemplate::tokenize('{%comment%}')); $this->assertEqual(array(' ', '{%comment%}', ' '), LiquidTemplate::tokenize(' {%comment%} ')); $this->assertEqual(array(' ', '{%comment%}', ' ', '{%endcomment%}', ' '), LiquidTemplate::tokenize(' {%comment%} {%endcomment%} ')); $this->assertEqual(array(' ', '{% comment %}', ' ', '{% endcomment %}', ' '), LiquidTemplate::tokenize(" {% comment %} {% endcomment %} ")); }
/** * @param array|Cache $cache * * @throws LiquidException */ public function setCache($cache) { if (is_array($cache)) { if (isset($cache['cache']) && class_exists('\\Liquid\\Cache\\' . ucwords($cache['cache']))) { $classname = '\\Liquid\\Cache\\' . ucwords($cache['cache']); self::$cache = new $classname($cache); } else { throw new LiquidException('Invalid cache options!'); } } else { if ($cache instanceof Cache) { self::$cache = $cache; } } }
/** * Parses the given tokens * * @param array $tokens */ public function parse(&$tokens) { $startRegexp = new LiquidRegexp('/^' . LIQUID_TAG_START . '/'); $tagRegexp = new LiquidRegexp('/^' . LIQUID_TAG_START . '\\s*(\\w+)\\s*(.*)?' . LIQUID_TAG_END . '$/'); $variableStartRegexp = new LiquidRegexp('/^' . LIQUID_VARIABLE_START . '/'); $this->_nodelist = array(); if (!is_array($tokens)) { return; } $tags = LiquidTemplate::getTags(); while (count($tokens)) { $token = array_shift($tokens); if ($startRegexp->match($token)) { if ($tagRegexp->match($token)) { // if we found the proper block delimitor just end parsing here and let the outer block proceed if ($tagRegexp->matches[1] == $this->blockDelimiter()) { return $this->endTag(); } if (array_key_exists($tagRegexp->matches[1], $tags)) { $tag_name = $tags[$tagRegexp->matches[1]]; } else { $tag_name = 'LiquidTag' . ucwords($tagRegexp->matches[1]); // search for a defined class of the right name, instead of searching in an array $tag_name = Liquid::classExists($tag_name) === true ? $tag_name : null; } if (class_exists($tag_name)) { $this->_nodelist[] = new $tag_name($tagRegexp->matches[2], $tokens, $this->_fileSystem); if ($tagRegexp->matches[1] == 'extends') { return true; } } else { $this->unknownTag($tagRegexp->matches[1], $tagRegexp->matches[2], $tokens); } } else { throw new LiquidException("Tag {$token} was not properly terminated"); // harry } } elseif ($variableStartRegexp->match($token)) { $this->_nodelist[] = $this->_createVariable($token); } elseif ($token != '') { $this->_nodelist[] = $token; } } $this->assertMissingDelimitation(); }
function test_include_tag_no_with() { $template = new LiquidTemplate(); $template->setFileSystem(new LiquidTestFileSystem()); $template->parse("Outer-{% include 'inner' %}-Outer-{% include 'inner' other:'23' %}"); $output = $template->render(array("inner" => "orig", "var" => array(1, 2, 3))); $this->assertEqual("Outer-Inner: orig-Outer-Inner: orig23", $output); }
public function assertTrueHelper($templateString, $expected, $data = array()) { $template = new LiquidTemplate(); $template->parse($templateString); $this->assertTrue($template->render($data) === $expected); }
<?php define('LIQUID_INCLUDE_SUFFIX', 'tpl'); define('LIQUID_INCLUDE_PREFIX', ''); require_once '../Liquid.class.php'; $liquid = new LiquidTemplate(); $liquid->parse('{{ hello }} {{ goback }}'); print $liquid->render(array('hello' => 'hello world', 'goback' => '<a href=".">index</a>'));
function test_nested_context_drop() { $template = new LiquidTemplate(); $template->parse(' {{ product.context.foo }} '); $output = $template->render(array('product' => new ProductDrop(), 'foo' => 'monkey')); $this->assertEqual(' monkey ', $output); }
<?php define('LIQUID_INCLUDE_SUFFIX', 'tpl'); define('LIQUID_INCLUDE_PREFIX', ''); require_once '../Liquid.class.php'; define('PROTECTED_PATH', dirname(__FILE__) . '/protected/'); $liquid = new LiquidTemplate(PROTECTED_PATH . 'templates/'); $cache = array('cache' => 'file', 'cache_dir' => PROTECTED_PATH . 'cache/'); //$cache = array('cache' => 'apc'); $liquid->setCache($cache); $liquid->parse(file_get_contents(PROTECTED_PATH . 'templates/index.tpl')); $assigns = array('document' => array('title' => 'This is php-liquid', 'content' => 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.', 'copyright' => 'Harald Hanek - All rights reserved.'), 'blog' => array(array('title' => 'Blog Title 1', 'content' => 'Nunc putamus parum claram', 'comments' => array(array('title' => 'First Comment', 'message' => 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr'))), array('title' => 'Blog Title 2', 'content' => 'Nunc putamus parum claram', 'comments' => array(array('title' => 'First Comment', 'message' => 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr'), array('title' => 'Second Comment', 'message' => 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr')))), 'array' => array('one', 'two', 'three', 'four')); print $liquid->render($assigns);
/** * Parses the given source string * * @param string $source */ public function parse($source) { $cache = self::$_cache; new dBug($cache, "source cache"); if (isset($cache)) { if (($this->_root = $cache->read(md5($source))) != false && $this->_root->checkIncludes() != true) { } else { $this->_root = new LiquidDocument(LiquidTemplate::tokenize($source), $this->_fileSystem); $cache->write(md5($source), $this->_root); } } else { new dBug(LiquidTemplate::tokenize($source), "Tokenized Source"); new dBug($this->_fileSystem, "File System"); $this->_root = new LiquidDocument(LiquidTemplate::tokenize($source), $this->_fileSystem); new dBug($this->_root, "Document Root"); } exit; return $this; }
<?php define('LIQUID_INCLUDE_ALLOW_EXT', true); require_once '../Liquid.class.php'; define('PROTECTED_PATH', dirname(__FILE__) . '/protected/'); $liquid = new LiquidTemplate(PROTECTED_PATH . 'templates/'); $cache = array('cache' => 'file', 'cache_dir' => PROTECTED_PATH . 'cache/'); //$cache = array('cache' => 'apc'); //$liquid->setCache($cache); $liquid->parse(file_get_contents(PROTECTED_PATH . 'templates/child.tpl')); $assigns = array('document' => array('title' => 'This is php-liquid', 'content' => 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.', 'copyright' => '© Copyright 2012 Harald Hanek - All rights reserved.')); echo $liquid->render($assigns);
<?php require_once '../lib/liquid.php'; $liquid = new LiquidTemplate(); $liquid->parse(file_get_contents('templates/index.liquid')); $assigns = array('date' => date('r')); print $liquid->render($assigns);
/** * Parses the tokens * * @param array $tokens */ function parse($tokens) { if (!isset($this->file_system)) { trigger_error("No file system", E_USER_ERROR); } // read the source of the template and create a new sub document $source = $this->file_system->read_template_file($this->template_name); $tokens = LiquidTemplate::tokenize($source); $this->document = new LiquidDocument($tokens, $this->file_system); }
/** * check for cached includes * * @return string */ public function checkIncludes() { $cache = LiquidTemplate::getCache(); if ($this->_document->checkIncludes() == true) { return true; } $source = $this->_fileSystem->readTemplateFile($this->_templateName); if ($cache->exists(md5($source)) && $this->_hash == md5($source)) { return false; } return true; }
/** * Parses the given source string * * @param string $source */ public function parse($source) { $cache = self::$_cache; if (isset($cache)) { if (($this->_root = $cache->read(md5($source))) != false && $this->_root->checkIncludes() != true) { } else { $this->_root = new LiquidDocument(LiquidTemplate::tokenize($source), $this->_fileSystem); $cache->write(md5($source), $this->_root); } } else { $this->_root = new LiquidDocument(LiquidTemplate::tokenize($source), $this->_fileSystem); } return $this; }
/** * Parses the given source string * * @param string $source */ function parse($source) { $this->root = new LiquidDocument(LiquidTemplate::tokenize($source), $this->file_system); }
/** * Parses the given tokens * * @param array $tokens */ public function parse(&$tokens) { $start_regexp = new LiquidRegexp('/^' . LIQUID_TAG_START . '/'); $tag_regexp = new LiquidRegexp('/^' . LIQUID_TAG_START . '\\s*(\\w+)\\s*(.*)?' . LIQUID_TAG_END . '$/'); $variable_start_regexp = new LiquidRegexp('/^' . LIQUID_VARIABLE_START . '/'); new dBug("Start = LiquidBlock::Parse"); new dBug(func_get_args(), "Arguments = LiquidBlock::Parse"); $this->_nodelist = array(); new dBug($this->_nodelist, 'when nodelist intialized'); if (!is_array($tokens)) { return; } $tags = LiquidTemplate::getTags(); new dBug($tags, "all tags"); while (count($tokens)) { $token = array_shift($tokens); new dBug("token: " . $token); new dBug($this->_nodelist, 'in tokens loop: _nodelist'); if ($start_regexp->match($token)) { if ($tag_regexp->match($token)) { new dBug($tag_regexp->matches, "token tag matches"); // if we found the proper block delimitor just end parsing here and let the outer block proceed if ($tag_regexp->matches[1] == $this->block_delimiter()) { //new dBug($this->end_tag(), "token tag matches");exit(); return $this->end_tag(); } if (array_key_exists($tag_regexp->matches[1], $tags)) { $tag_name = $tags[$tag_regexp->matches[1]]; } else { $tag_name = 'LiquidTag' . ucwords($tag_regexp->matches[1]); } // search for a defined class of the right name, instead of searching in an array new dBug($tag_name); //exit(); // fetch the tag from registered blocks if (class_exists($tag_name)) { new dBug('tag name found: ' . $tag_name); new dBug($this->_nodelist, 'before tag_name call'); $temp = new $tag_name($tag_regexp->matches[2], $tokens, $this->file_system); new dBug($temp, 'after tag_name call'); exit; // $this->_nodelist[] = new $tag_name($tag_regexp->matches[2], $tokens, $this->file_system); new dBug($this->_nodelist, 'after tag_name call'); } else { new dBug('unknown tag: ' . $tag_name); //exit(); $this->unknown_tag($tag_regexp->matches[1], $tag_regexp->matches[2], $tokens); } } else { throw new LiquidException("Tag {$token} was not properly terminated"); // harry } } elseif ($variable_start_regexp->match($token)) { new dBug($this->_nodelist, 'before create_variable call'); $this->_nodelist[] = $this->create_variable($token); new dBug($this->_nodelist, 'after create_variable call'); } elseif ($token != '') { new dBug($this->_nodelist, 'before blank token assignment'); $this->_nodelist[] = $token; new dBug($this->_nodelist, 'after blank token assignment'); } } new dBug('shouldnnot be here'); $this->assert_missing_delimitation(); }
/** * Tests filtered value assignment * * @return void */ public function testAssignWithFilters() { $template = new LiquidTemplate(); $template->parse('{% assign test = "hello" | upcase %}{{ test }}'); $this->assertTrue($template->render() === 'HELLO'); $template->parse('{% assign test = "hello" | upcase | downcase | capitalize %}{{ test }}'); $this->assertTrue($template->render() === 'Hello'); $template->parse('{% assign test = var1 | first | upcase %}{{ test }}'); $this->assertTrue($template->render(array('var1' => array('a', 'b', 'c'))) === 'A'); $template->parse('{% assign test = var1 | last | upcase %}{{ test }}'); $this->assertTrue($template->render(array('var1' => array('a', 'b', 'c'))) === 'C'); $template->parse('{% assign test = var1 | join %}{{ test }}'); $this->assertTrue($template->render(array('var1' => array('a', 'b', 'c'))) === 'a b c'); $template->parse('{% assign test = var1 | join : "." %}{{ test }}'); $this->assertTrue($template->render(array('var1' => array('a', 'b', 'c'))) === 'a.b.c'); }
<?php class ProductsFilter { function price($integer) { return sprintf("\$%.2d USD", $integer / 100); } function prettyprint($text) { return preg_replace('/\\*(.*)\\*/', '<b>\\1</b>', $text); } function count($array) { return count($array); } function paragraph($p) { return "<p>" . $p . "</p>"; } } require_once '../lib/liquid.php'; $liquid = new LiquidTemplate(); $liquid->register_filter(new ProductsFilter()); $liquid->parse(file_get_contents('templates/products.liquid')); $products_list = array(array('name' => 'Arbor Draft', 'price' => 39900, 'description' => 'the *arbor draft* is a excellent product'), array('name' => 'Arbor Element', 'price' => 40000, 'description' => 'the *arbor element* rocks for freestyling'), array('name' => 'Arbor Diamond', 'price' => 59900, 'description' => 'the *arbor diamond* is a made up product because im obsessed with arbor and have no creativity')); $products = array('products' => $products_list, 'section' => 'Snowboards', 'cool_products' => true); $assigns = array('date' => date('r')); print $liquid->render($products);