harrydeluxe / php-liquid

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

How to register an Extended AbstractTag? #49

Closed randohinn closed 6 years ago

randohinn commented 7 years ago

Title says it all. I want to add a new tag, callable by {% entypo %} and I can't figure out how to register a AbstractTag. Here's the tag:

<?php

    namespace CMS\Liquid;

    use Liquid\AbstractTag;

    class EntypoTag extends AbstractTag {
            public function __construct($markup, array &$tokens, FileSystem $fileSystem = null) {
                parent::__construct($markup, $tokens, $fileSystem);
            }

            public function render(Context $context) {
                die($context);
            }
    }

And here is how I'm registering it so far:

 $template = new Template();
 $template->parse(file_get_contents(__DIR__.'/../src/views/admin/dashboard.html'));
 $template->registerTag('entypo', new EntypoTag('entypo',$template->tokens));

 echo $template->render();

And bam, I get a Unkown tag entypo [E:\Webdev\CMS\vendor\liquid\liquid\src\Liquid\AbstractBlock.php:146]. How can I propely register it?

sanmai commented 6 years ago

Probably you found the answer, but anyway.

You're doing it in the wrong order. And registerTag wants a class name, not an instance.

class EntypoTag extends AbstractTag {
    public function render(Context $context) {
        return "OK!";
    }
}

$template = new Template();
$template->registerTag('entypo', EntypoTag::class);
$template->parse('{% entypo %}');

echo $template->render();