picocms / Pico

Pico is a stupidly simple, blazing fast, flat file CMS.
http://picocms.org/
MIT License
3.84k stars 614 forks source link

Twig filters and functions for version 2 #442

Closed bricebou closed 6 years ago

bricebou commented 6 years ago

Hi, again...

I'm really sorry, you know. I try, I read, I search but I can't figure out some things...

I'd like to declare some personal Twig filters or functions and I tried to reproduce https://github.com/picocms/Pico/issues/424.

In a copy of the DummyPlugin.php, just to make sure the issue wasn't due to something else, I have exactly this:

public function onPageRendering(Twig_Environment &$twig, array &$twigVariables, &$templateName)
    {
      $testFunction = new Twig_SimpleFunction('testFunction', function () {
        return "Test works!";
      });
      $twig->addFunction($testFunction);
    }

But I have a PHP fatal error:

[Thu Jun 28 15:08:51.199462 2018] [php7:error] [pid 1863] [client ::1:48864] PHP Fatal error:  Uncaught TypeError: Argument 1 passed to bisDummyPlugin::onPageRendering() must be an instance of Twig_Environment, string given, called in /home/bbrice/webdev/www-dev/vendor/picocms/pico/lib/AbstractPicoPlugin.php on line 117 and defined in /home/bbrice/webdev/www-dev/plugins/bisDummyPlugin.php:458\nStack trace:\n#0 /home/bbrice/webdev/www-dev/vendor/picocms/pico/lib/AbstractPicoPlugin.php(117): bisDummyPlugin->onPageRendering('index.twig', Array)\n#1 /home/bbrice/webdev/www-dev/vendor/picocms/pico/lib/Pico.php(2439): AbstractPicoPlugin->handleEvent('onPageRendering', Array)\n#2 /home/bbrice/webdev/www-dev/vendor/picocms/pico/lib/Pico.php(476): Pico->triggerEvent('onPageRendering', Array)\n#3 /home/bbrice/webdev/www-dev/index.php(36): Pico->run()\n#4 {main}\n  thrown in /home/bbrice/webdev/www-dev/plugins/bisDummyPlugin.php on line 458

I've tried to look the Pico source code but I don't understand much. I've seen a PicoTwigExtension class which structure I tried to copy but filters and functions were not recognized...

Thanks in advance :)

PhrozenByte commented 6 years ago

You're using the DummyPlugin as of Pico 1.0, not of Pico 2.0.

bricebou commented 6 years ago

Yeah, I figure that out... I should use the onTemplateRendered with the &output variable?

-- Brice Boucard

Le 28 juin 2018 à 16:19, Daniel Rudolf notifications@github.com a écrit :

You're using the DummyPlugin as of Pico 1.0, not of Pico 2.0.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

PhrozenByte commented 6 years ago

For what? Registering a Twig function? No. You can access $twig using the onTwigRegistered event.

bricebou commented 6 years ago

I'll give it a try ! But I don't understand... I'm a bit frustrated, cause I launched the search Twig_Environment on this repo and this function didn't appear... (https://github.com/picocms/Pico/search?q=Twig_Environment&unscoped_q=Twig_Environment)

Sorry, and many thanks !

PhrozenByte commented 6 years ago

GitHub's web search only searches in the repo's default branch (the master branch), and since Pico 2.0 is still in beta, you will therefore search in the source code of Pico 1.0. I'm not sure why you even want to use GitHub's web search, it's so inflexible... Simply search your local files or, even better, check your local plugins/DummyPlugin.php directly.

bricebou commented 6 years ago

I wanted to make sure I wasn't missing anything... :-(

Again, so sorry for all the noise I make...

Time to play with Twig filters and functions... Thanks again !