I am a plugin newb, but got most of the way there. I am trying to build a simple webservice pluggin that fetches github release content and displays in on a page. In general the pluggin should query the GITHUB API, return JSON (Which includes Markdown text), Convert JSON to an array, process the array using a foreach, send the results to the page. The Markdown should be processed when displayed to the user.
I am using the https://learn.getgrav.org/cookbook/plugin-recipes#output-some-php-code-result-in-a-twig-template code as a starter. Everything is working as expected, but my only challenge is the output is not being formatted using markdown. Below is my code:
<?php
namespace Grav\Plugin;
class ExampleTwigExtension extends \Twig_Extension
{
public function getName()
{
return 'ExampleTwigExtension';
}
public function getFunctions()
{
return [
new \Twig_SimpleFunction('example', [$this, 'exampleFunction'])
];
}
public function exampleFunction()
{
$text = "";
//Set Variables
$token = "TOKEN";
$url = "GITHUB URL";
$decode_token = base64_decode($token);
//Init Curl
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_USERPWD, "$decode_token");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HEADER, 0);
//Set as variable to be re-used.
$data = curl_exec($ch);
$returned_content = $data;
//Convert JSON data to an ARRAY so that it can be parsed.
$releases = json_decode($returned_content, true);
if(curl_error($ch)) {
$text.= '<h3>There has been an error!</h3>';
$text.= '<p>
I am unable to get release information. Please contact <a href="mailto:address@domain.com?subject=NISP Release Error: ' .curl_error($ch).' ">CIS Governance</a> for more assistance
</p>';
$text.= 'Ref:'. curl_error($ch);
}else{
foreach ($releases as $release) {
$text.= '<div class="page-header">';
$text.= '<h2>';
$text.= $release['name'];
$text.= '</h2>';
$text.= '</div>';
$text.= '<div class="list-group">';
$text.= "\n\n". $release['body'];
$text.= '</div>';
}
}
// Return the Processed Text <<<< THIS IS Where I suspect I am doing something wrong...
return $text;
}
}