Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

PHP Snippets

Started by Muut Archive 10 years ago · 10 replies · 1875 views
10 years ago

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

10 years ago

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

TWIG
{{ myVariable }}

to output your PHP processing output.

10 years ago

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

10 years ago

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:

PHP
    /**
     * 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
<?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:

TWIG
{{ 'something'|php_something }}
10 years ago

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

10 years ago

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:

PHP
<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"; ?>

10 years ago

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]
];
}

PHP
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

10 years ago

basically the phpcode show the onlinestatus of a gameserver.

HTML

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

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1328 9 years ago
Archive · by Muut Archive, 9 years ago
2 921 9 years ago
Archive · by Muut Archive, 9 years ago
2 4050 9 years ago
Archive · by Muut Archive, 9 years ago
1 2928 9 years ago
Archive · by Muut Archive, 9 years ago
3 1107 9 years ago