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.
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();
}
}
}
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
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
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