PHP Snippets

I am a Rocket Theme junkie. I use and love all of their products. I was very excited to give Grav a try on my latest project. I needed a “no database” CMS but I also need the ability to run and display my own PHP code. There are several other CMSs that do this really well through PHP-Snippets. They provide a place in the admin area to create and save your code then you just put a tag in your document that calls the snippets. It then displays the output nicely in the body of the document. Two good examples of this are http://monstra.org and http://feindura.org/.

Please let me know if there is, or will be, a way to do this with Grav.

Thanks

I think the best way to run your custom PHP is to create a small plugin that adds a Twig variable where you can put the output of your PHP processing.

Something along the lines of

<?php
namespace Grav\Plugin;

use Grav\Common\Grav;
use Grav\Common\Plugin;

class MyPlugin extends Plugin
{
    public static function getSubscribedEvents()
    {
        return [
            'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
        ];
    }
 
    public function calculateSomething()
    {
        //your custom PHP

        return 'something';
    }

    public function onTwigSiteVariables() {
        if (!$this->isAdmin()) {
            $this->grav['twig']->myVariable = $this->calculateSomething();
        }
    }

}

and in your Twig files (or Page) add

{{ myVariable }}

to output your PHP processing output.

Cool, I need PHP functions too. Thanks for sharing!

Another options is to create a simple plugin that adds a custom Twig extension. There is an example of this in the SmartyPants plugin. https://github.com/getgrav/grav-plugin-smartypants

For example, make sure you are subscribed to the onTwigExtensions() event, then:

    /**
     * Add Twig Extensions
     */
    public function onTwigExtensions()
    {
        require_once(__DIR__.'/twig/YourCustomTwigExtension.php');
        $this->grav['twig']->twig->addExtension(new YourCustomTwigExtension());
    }

Then create a twig extension in a twig/ folder of your plugin called YourCustomExtension.php or whatever name you wish, with something like:

<?php
namespace Grav\Plugin; 

use \Grav\Common\Grav;

class YourCustomTwigExtension extends \Twig_Extension
{

    /**
     * Returns extension name.
     *
     * @return string
     */
    public function getName()
    {
        return 'YourCustomTwigExtension';
    }

    public function getFilters()
    {
        return [
            new \Twig_SimpleFilter('php_something', [$this, 'phpSomethingFilter']),

        ];
    }

    public function phpSomethingFilter($content)
    {
        $output = some_php_function($content);

        return $output;
    }
}

Then you can just use this new filter in your Twig code:

{{ 'something'|php_something }}

I really don’t know anything about Twig. Can you please elaborate.
How do I:
“make sure you are subscribed to the onTwigExtensions() event,”

Is this will be your help?

You can look at that smartypants plugin i linked to above, or look through the plugin tutorial on the docs site.

The “grav-plugin-minicode” that takefumi posted is exactly the type of solution I was looking for. Unfortunately it does not seem to be processing php. I created a simple test.html file in the user/minicodes folder with this content:

<html>
        <?php
        $output = "Hello World" ;
        echo "<pre>$output</pre>";
        ?>
</html>

And then added a {{ 'test.html' | minicode }} twig-tag to a page I get the following output:

$output"; ?>

Hello all,

i tried this code
—code

<?php namespace Grav\Plugin; use Grav\Common\Grav; use Grav\Common\Plugin; class MyPlugin extends Plugin { public static function getSubscribedEvents() { return [ 'onTwigSiteVariables' => ['onTwigSiteVariables', 0] ]; } public function calculateSomething() { //your custom PHP return 'something'; } public function onTwigSiteVariables() { if (!$this->isAdmin()) { $this->grav['twig']->myVariable = $this->calculateSomething(); } } } -``` >and in your Twig files (or Page) add >{{ myVariable }} this will directly show the output of my phpcode on all pages, which was not expected. it shall only show up, when i use the new twig variable anyone has hints for me? cheers chris

What is output exactly?

basically the phpcode show the onlinestatus of a gameserver.

error_reporting(0);
$fp = fsockopen("gameserver.com", 3000, $errno, $errstr, 1);
if (!$fp) {
             echo '<div> gameserver.com:2583 - offline</div>';
            } else {
             echo '<div> gameserver.com:2583 - online</div>';
fclose($fp);
}
---```

It will directly show "gameserver.com:2583 -online" on top of every page, even if i dont use the {{ myVariable }}

And if i use the variable on the page and twig processing is enabled, there is nothing showing up then

cheers
chris