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

include from array #45

Open hakimihamdan88 opened 7 years ago

hakimihamdan88 commented 7 years ago

possible to render include tag from array? I don't want to load from a file

sanmai commented 7 years ago

There's a virtual filesystem in liquid/liquid in packagist. This is the package you will install with composer.

Used like this:

class MyFileSystem extends Liquid\FileSystem\Virtual
{
    public static function fromArray($array)
    {
        return new static(function ($path) use ($array) {
            if (isset($array[$path])) {
                return $array[$path];
            }

            return '';
        });
    }
}

And then:

$template = new Template();
$template->setFileSystem(MyFileSystem::fromArray(array(
        'example' => "Hello, {{ name }}!",
)));

$template->parse("{% include 'example' %}");
echo $template->render(array("name" => "World"));