harrydeluxe / php-liquid

A PHP port of Ruby's Liquid Templates
http://www.delacap.com/artikel/Liquid-Templates/
MIT License
239 stars 119 forks source link

variable inside an include tag cause an exception #14

Open chros73 opened 12 years ago

chros73 commented 12 years ago

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 commented 10 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.

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

 /**

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

 /**

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) {

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) {

chros73 commented 10 years ago

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