Open chros73 opened 12 years ago
So, I have made some modification in my code to achieve this, feel free to comment this. If you think it's good, someone can commit it.
Now you can use tags like this (where a string not inside a " or ' is considered as a variable):
{%include merchant/'head' %}
{%extends "root"/foo/merchant/'index' %}
Previously u could used this line to render a page:
$liquid->parse(file_get_contents('templates/products.tpl'))->render($products);
If you want to use variables inside include and extends tags with this mod, you should render the page with this (backward compatible):
$liquid->parse(file_get_contents('templates/products.tpl'), $products)->render();
So the diffs: (sorry I don't know which tag I should use in comments to display the diffs properly)
Note: the getFileSystem() method is there for a different purpose, but since it doesn't hurt anything I'm just left here.
--- LiquidTemplate.class.php (.../betertainmentsvn/trunk/shared/lib/lib) (revision 157899) +++ LiquidTemplate.class.php (.../betertainmentshared/branches/REG_liquidmods/lib/liquid-lib) (revision 157899) @@ -39,7 +39,12 @@
private static $_cache;
/**
* @var LiquidContext The context for keeping and resolving variables
*/
private static $_context;
/* * Constructor @@ -62,7 +67,16 @@ $this->_fileSystem = $fileSystem; }
/**
*
*
*/
public function getFileSystem()
{
return $this->_fileSystem;
}
/* * @@ -132,6 +146,17 @@
/**
self::$_context = new LiquidContext($assigns, null);
+ if (isset($cache)) { if (($this->_root = $cache->read(md5($source))) != false && $this->_root->checkIncludes() != true) @@ -192,7 +220,13 @@ */ public function render(array $assigns = array(), $filters = null, $registers = null) {
}
if (!is_null($filters))
{
@@ -211,6 +245,7 @@ $context->addFilters($filter); }
self::$_context = $context;
return $this->_root->render($context);
} }
This one is here for backward compatibility.
--- LiquidContext.class.php (.../betertainmentsvn/trunk/shared/lib/lib) (revision 157899) +++ LiquidContext.class.php (.../betertainmentshared/branches/REG_liquidmods/lib/liquid-lib) (revision 157899) @@ -69,6 +69,17 @@
/**
--- Tag/LiquidTagInclude.class.php (.../betertainmentsvn/trunk/shared/lib/lib) (revision 157899) +++ Tag/LiquidTagInclude.class.php (.../betertainmentshared/branches/REG_liquidmods/lib/liquid-lib) (revision 157899) @@ -45,8 +45,8 @@ */ private $_document;
@@ -61,12 +61,33 @@ */ public function __construct($markup, &$tokens, &$fileSystem) {
$regex = new LiquidRegexp('/([^\s]+)(\s+(with|for)\s+(' . LIQUID_QUOTED_FRAGMENT . '+))?/');
if ($regex->match($markup))
{
}
$this->_templateName = substr($regex->matches[1], 1, strlen($regex->matches[1]) - 2);
}
if (isset($regex->matches[1]))
{
@@ -78,7 +99,7 @@ } else {
throw new LiquidException("Error in tag 'include' - Valid syntax: include [template]|'[template]' (with|for) [object|collection]");
}
parent::__construct($markup, $tokens, $fileSystem);
--- Tag/LiquidTagExtends.class.php (.../betertainmentsvn/trunk/shared/lib/lib) (revision 157899) +++ Tag/LiquidTagExtends.class.php (.../betertainmentshared/branches/REG_liquidmods/lib/liquid-lib) (revision 157899) @@ -38,50 +38,73 @@ */ public function __construct($markup, &$tokens, &$fileSystem) {
$regex = new LiquidRegexp('/([^\s]+)?/');
if ($regex->match($markup))
{
throw new LiquidException("Error in tag 'extends' - Valid syntax: extends [template]|'[template name]'");
}
parent::__construct($markup, $tokens, $fileSystem);
}
I have created a fork for this (unit tests also included): https://github.com/chros73/php-liquid
Hi!
First of all thank you for your work! I tried to used a variable (name of the template file) inside the include tag, but it throws an exception.
{% assign foo = 'builder' %} {% include foo %} {% include {{ foo }} %}
Of course this is working: {% include 'builder' %}
I know that there is a block tag for this kind of operation, but I'd like to use the include tag instead (it would be a nice feature).
Thanks! Krisz