Grav CMS创建Twig扩展名不起作用

问题描述 投票:-1回答:1

我创建了一个Twig扩展,以将PHP包含在Twig模板中,但它不起作用。

这是我的代码:

<?php
namespace Grav\Plugin;
class PhpIncludeTwigExtension extends \Twig_Extension
{
    public function getName()
    {
        return 'PhpIncludeTwigExtension';
    }
    public function getFunctions()
    {
        return [
            new \Twig_SimpleFunction('includeFile', [$this, 'exampleFunction'])
        ];
    }
    public function exampleFunction(string $file)
    {
        ob_start();
        require_once(dirname(__FILE__).'./'.$file);
        $content = ob_get_clean();

        return $content;   
    }
}

我正在使用此扩展程序,例如:

{{ includeFile('./../../getData.php') }}
php twig twig-extension grav
1个回答
0
投票

这是我希望将其包含在模板中的函数php:

`

   if(! $file ){

    $file = file_get_contents('https://digital-learning-academy.com/feed/');

    $cache->write('FlueRss',$file);
}



$temp = simplexml_load_string( trim( $file ), "SimpleXMLElement", LIBXML_NOCDATA );
$data = json_decode( json_encode( $temp ) );




function html(){

    global $data;

    $items = array_slice($data->channel->item,0,3); 



    $html ='';
    foreach ( $items as  $item) {


    $category = $item->category[0];



    if($category != "DLA"){

        $title = $item->title;
        $link = $item->link;

        $pubDate = $item->pubDate;
        $date = strtotime($pubDate);
        $pub = date("d-m-y",$date);

        foreach ($item->enclosure as  $value) {

            $img = $value->url;

            }



            $html .=  '<div class="col-md-4">';

            $html .= '<div class="card card-shadow" data-aos="flip-left" data-aos-duration="1200">';

            $html .= '<a href="' .$link.'" target="_blank" ><img class="card-img-top" src="' .$img.'" alt="wrappixel kit"></a>';

            $html .= '<div class="p-30">';
            $html .= '<div class="d-flex no-block font-14">';
            $html .= '<a href="' . $link . '">' . $category . "</a>";
            $html .= '<span class="ml-auto">' . $pub . "</span>";
            $html .= "</div>";
            $html .='<h5 class="font-medium m-t-20"><a href="' .$link .'"  target="_blank" class="link">' .$title ."</a></h5>";
            $html .= "</div>";
            $html .= "</div>";
            $html .= "</div>"; 
    }
}

echo $html;

}

html();

?>`
© www.soinside.com 2019 - 2024. All rights reserved.