Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

variable inside an include tag cause an exception #14

Open
chros73 opened this issue Oct 24, 2012 · 2 comments
Open

variable inside an include tag cause an exception #14

chros73 opened this issue Oct 24, 2012 · 2 comments

Comments

@chros73
Copy link

chros73 commented Oct 24, 2012

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

@chros73
Copy link
Author

chros73 commented Jan 24, 2014

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.

Index: LiquidTemplate.class.php

--- 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 @@
 /**
  • *
    
  • *
    
  • \* @return array
    
  • */
    
  • public static function getContext()
  • {
  •    return self::$_context;
    
  • }
  • /**

  • Register the filter
    *

  • @param unknown_type $filter
    @@ -158,11 +183,14 @@

  • 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)
      @@ -192,7 +220,13 @@
      */
      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))
     {
    

    @@ -211,6 +245,7 @@
    $context->addFilters($filter);
    }

  •    self::$_context = $context;
     return $this->_root->render($context);
    

    }
    }

This one is here for backward compatibility.

Index: LiquidContext.class.php

--- 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 @@

 /**
  • \* Add a registers to the context
    
  • *
    
  • \* @param array $registers
    
  • */
    
  • public function addRegisters($registers = array())
  • {
  •    $this->registers = $registers;
    
  • }
  • /**
  • Invoke the filter that matches given name
    *
  • @param string $name The name of the filter

Index: Tag/LiquidTagInclude.class.php

--- 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;

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

@@ -61,12 +61,33 @@
*/
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]))
         {
    

    @@ -78,7 +99,7 @@
    }
    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);
    

Index: Tag/LiquidTagExtends.class.php

--- 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('/("[^"]+"|\'[^\']+\')?/');
    
  •    $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 'include' - 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);
    

    }

@chros73
Copy link
Author

chros73 commented Apr 22, 2014

I have created a fork for this (unit tests also included): https://github.com/chros73/php-liquid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant