Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion README.textile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Example snippet:

h2. Howto use Liquid

Code to render:
Code to render with original php-liquid:

<pre>
$liquid->parse(file_get_contents('templates/products.tpl'))->render($products);
Expand All @@ -61,3 +61,24 @@ h2. Fork Notes

This fork is based on php-liquid by Mateo Murphy. The original library is still hosted at: ("http://code.google.com/p/php-liquid/":http://code.google.com/p/php-liquid/)

Variables couldn't be used inside an 'include' or an 'extends' tag.

Which means the below code didn't work:

<pre>
{% include foo %}
{% include {{ foo }} %}
</pre>

With this modification it will work (where a string not inside a " or ' is considered as a variable):

<pre>
{% include merchant/'head' %}
{% extends "root"/foo/bar/'index' %}
</pre>

Code to render with modified php-liquid (backward compatible, which means the original example does still work but variables can't be used in include and extends tags):

<pre>
$liquid->parse(file_get_contents('templates/products.tpl'), $products)->render();
</pre>
11 changes: 11 additions & 0 deletions lib/LiquidContext.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ public function addFilters($filter)
}


/**
* Add registers to the context
*
* @param array $registers
*/
public function addRegisters($registers = array())
{
$this->registers = $registers;
}


/**
* Invoke the filter that matches given name
*
Expand Down
30 changes: 28 additions & 2 deletions lib/LiquidTemplate.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ class LiquidTemplate

private static $_cache;

/**
* @var LiquidContext The context for keeping and resolving variables
*/
private static $_context;


/**
* Constructor
Expand Down Expand Up @@ -131,6 +136,17 @@ public static function getTags()
}


/**
*
*
* @return array
*/
public static function getContext()
{
return self::$_context;
}
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if adding this static function can be avoided. We already have more than enough static variables.



/**
* Register the filter
*
Expand Down Expand Up @@ -158,11 +174,14 @@ public static function tokenize($source)
* Parses the given source string
*
* @param string $source
* @param array $assigns An array of values for the template
*/
public function parse($source)
public function parse($source, array $assigns = array())
{
$cache = self::$_cache;

self::$_context = new LiquidContext($assigns, null);

if (isset($cache))
{
if (($this->_root = $cache->read(md5($source))) != false && $this->_root->checkIncludes() != true)
Expand Down Expand Up @@ -192,7 +211,13 @@ public function parse($source)
*/
public function render(array $assigns = array(), $filters = null, $registers = null)
{
$context = new LiquidContext($assigns, $registers);
$context = self::$_context;
if (!empty($assigns) && is_array($assigns)) {
$context->merge($assigns);
}
if (!is_null($registers) && is_array($registers)) {
$context->addRegisters($registers);
}

if (!is_null($filters))
{
Expand All @@ -211,6 +236,7 @@ public function render(array $assigns = array(), $filters = null, $registers = n
$context->addFilters($filter);
}

self::$_context = $context;
return $this->_root->render($context);
}
}
108 changes: 65 additions & 43 deletions lib/Tag/LiquidTagExtends.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,50 +38,72 @@ class LiquidTagExtends extends LiquidTag
*/
public function __construct($markup, &$tokens, &$fileSystem)
{
$regex = new LiquidRegexp('/("[^"]+"|\'[^\']+\')?/');
$regex = new LiquidRegexp('/([^\s]+)?/');

if ($regex->match($markup))
{
$this->_templateName = substr($regex->matches[1], 1, strlen($regex->matches[1]) - 2);
$regexTemplateName = new LiquidRegexp('/"[^"]+"|\'[^\']+\'|[^"\'\/]+/');
if ($regexTemplateName->match_all($regex->matches[1])) {
$regexQuote = new LiquidRegexp('/"[^"]+"|\'[^\']+\'/');
$context = LiquidTemplate::getContext();
$templName = "";
foreach ($regexTemplateName->matches[0] as $templPart) {
$templPartName = '';
if ($regexQuote->match($templPart)) {
$templPartName = trim($templPart,"\"''");
} else {
$templPartName = $context->get($templPart);
}
if (!empty($templPartName)) {
if (!empty($templName)) $templName .= "/";
$templName .= $templPartName;
}
}
$this->_templateName = $templName;
}

if (empty($this->_templateName)) {
throw new LiquidException("Error in tag 'extends' - Valid syntax: include [template]|'[template]' (with|for) [object|collection]");
}
}
else
{
throw new LiquidException("Error in tag 'extends' - Valid syntax: extends '[template name]'");
throw new LiquidException("Error in tag 'extends' - Valid syntax: extends [template]|'[template name]'");
}

parent::__construct($markup, $tokens, $fileSystem);
}


private function _findBlocks($tokens)
{
$blockstartRegexp = new LiquidRegexp('/^' . LIQUID_TAG_START . '\s*block (\w+)\s*(.*)?' . LIQUID_TAG_END . '$/');
$blockendRegexp = new LiquidRegexp('/^' . LIQUID_TAG_START . '\s*endblock\s*?' . LIQUID_TAG_END . '$/');
$b = array();
$name = null;
foreach($tokens as $token)
{
if($blockstartRegexp->match($token))
{
$name = $blockstartRegexp->matches[1];
$b[$name] = array();
}
else if($blockendRegexp->match($token))
{
$name = null;
}
else
{
if(isset($name))
{
array_push($b[$name], $token);
}
}
}
return $b;
private function _findBlocks($tokens)
{
$blockstartRegexp = new LiquidRegexp('/^' . LIQUID_TAG_START . '\s*block (\w+)\s*(.*)?' . LIQUID_TAG_END . '$/');
$blockendRegexp = new LiquidRegexp('/^' . LIQUID_TAG_START . '\s*endblock\s*?' . LIQUID_TAG_END . '$/');

$b = array();
$name = null;

foreach($tokens as $token)
{
if($blockstartRegexp->match($token))
{
$name = $blockstartRegexp->matches[1];
$b[$name] = array();
}
else if($blockendRegexp->match($token))
{
$name = null;
}
else
{
if(isset($name))
{
array_push($b[$name], $token);
}
}
}

return $b;
}


Expand Down Expand Up @@ -120,18 +142,18 @@ public function parse(&$tokens)
$childtokens = $this->_findBlocks($tokens);

$blockstartRegexp = new LiquidRegexp('/^' . LIQUID_TAG_START . '\s*block (\w+)\s*(.*)?' . LIQUID_TAG_END . '$/');
$blockendRegexp = new LiquidRegexp('/^' . LIQUID_TAG_START . '\s*endblock\s*?' . LIQUID_TAG_END . '$/');
$b = array();
$name = null;
$blockendRegexp = new LiquidRegexp('/^' . LIQUID_TAG_START . '\s*endblock\s*?' . LIQUID_TAG_END . '$/');

$b = array();
$name = null;

$rest = array();
$aufzeichnen = false;

for($i = 0; $i < count($maintokens); $i++)
{
if($blockstartRegexp->match($maintokens[$i]))
{
{
$name = $blockstartRegexp->matches[1];

if(isset($childtokens[$name]))
Expand All @@ -141,16 +163,16 @@ public function parse(&$tokens)
foreach($childtokens[$name] as $item)
array_push($rest, $item);
}
}

}
if(!$aufzeichnen)
array_push($rest, $maintokens[$i]);
array_push($rest, $maintokens[$i]);

if($blockendRegexp->match($maintokens[$i]) && $aufzeichnen === true)
{
if($blockendRegexp->match($maintokens[$i]) && $aufzeichnen === true)
{
$aufzeichnen = false;
array_push($rest, $maintokens[$i]);
}
}
}
}

Expand Down
31 changes: 26 additions & 5 deletions lib/Tag/LiquidTagInclude.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class LiquidTagInclude extends LiquidTag
*/
private $_document;

/**
* @var string The Source Hash
/**
* @var string The Source Hash
*/
protected $_hash;

Expand All @@ -61,12 +61,33 @@ class LiquidTagInclude extends LiquidTag
*/
public function __construct($markup, &$tokens, &$fileSystem)
{
$regex = new LiquidRegexp('/("[^"]+"|\'[^\']+\')(\s+(with|for)\s+(' . LIQUID_QUOTED_FRAGMENT . '+))?/');
$regex = new LiquidRegexp('/([^\s]+)(\s+(with|for)\s+(' . LIQUID_QUOTED_FRAGMENT . '+))?/');

if ($regex->match($markup))
{
$regexTemplateName = new LiquidRegexp('/"[^"]+"|\'[^\']+\'|[^"\'\/]+/');
if ($regexTemplateName->match_all($regex->matches[1])) {
$regexQuote = new LiquidRegexp('/"[^"]+"|\'[^\']+\'/');
$context = LiquidTemplate::getContext();
$templName = "";
foreach ($regexTemplateName->matches[0] as $templPart) {
$templPartName = '';
if ($regexQuote->match($templPart)) {
$templPartName = trim($templPart,"\"''");
} else {
$templPartName = $context->get($templPart);
}
if (!empty($templPartName)) {
if (!empty($templName)) $templName .= "/";
$templName .= $templPartName;
}
}
$this->_templateName = $templName;
}

$this->_templateName = substr($regex->matches[1], 1, strlen($regex->matches[1]) - 2);
if (empty($this->_templateName)) {
throw new LiquidException("Error in tag 'include' - Valid syntax: include [template]|'[template]' (with|for) [object|collection]");
}

if (isset($regex->matches[1]))
{
Expand All @@ -78,7 +99,7 @@ public function __construct($markup, &$tokens, &$fileSystem)
}
else
{
throw new LiquidException("Error in tag 'include' - Valid syntax: include '[template]' (with|for) [object|collection]");
throw new LiquidException("Error in tag 'include' - Valid syntax: include [template]|'[template]' (with|for) [object|collection]");
}

parent::__construct($markup, $tokens, $fileSystem);
Expand Down
Loading