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

Calling a plugin within a twig

Started by Muut Archive 11 years ago · 4 replies · 1194 views
11 years ago

I've created a plugin using the tutorial and screwing with admin and have the following:

latest.yaml :

YAML
   enabled: true
   theme: default

latest.php:

PHP
   <?php
namespace Grav\Plugin;

use Grav\Common\Page\Collection;
use Grav\Common\Plugin;

class LatestPlugin extends Plugin 
{
    public static function getSubscribedEvents() {
    return [
        'onPluginsInitialized' => ['onPluginsInitialized', 0],
    ];
} 

    public function onPluginsInitialized() {
        /** @var Uri $uri */
        $this->enable([
               'onPageInitialized' => ['onPageInitialized', 0]
            ]);
    }

    public function onPageInitialized()
    {
            $this->latestPages(1);
    }

    public function latestPages($count = 10)
    {
        /** @var Pages $pages */
        $pages = $this->grav['pages'];
        $latest = array();

        foreach ($pages->routes() as $url => $path) {
            $page = $pages->dispatch($url);
            if ($page && $page->routable()) {
                $latest[$page->route()] = ['modified' => $page->modified(), 'page' => $page];
            }
        }

        // sort based on modified
        uasort($latest, function ($a, $b) {
            if ($a['modified'] == $b['modified']) {
                return 0;
            }

            return ($a['modified'] > $b['modified']) ? -1 : 1;
        });

        // build new array with just pages in it
        // TODO: Optimized this
        $list = array();
        foreach ($latest as $item) {
            $list[] = $item['page'];
        }

        $this->grav['debugger']->addMessage(array_slice($list, 0, $count)[0]);

        return array_slice($list, 0, $count);
    }
}

As you can see here: http://qwyk.ly, the plugin is working (I get an array in the debugger)

My only problem is that in sidebar.html.twig (in user/themes/learn2/templates/partials) the following:

TWIG
        <ul>
            <li>THE PLUGIN SHOULD SHOW DATA HERE </li>
            {% for posts in latestPages(1) %}
                <li>{{ posts.route }}</li>
            {% endfor %}
            <li>AND WOULD FINISH HERE</li>
        </ul>

Fails, returning nothing at posts .... All help gratefully received

11 years ago

You are kinda doing it wrong. You should generate a latest_pages array, and add that to the Twig environment. Then in your sidebar you could loop over the latest_pages array and display stuff.

Pretty much every Grav plugin does this kinda of thing. Take a look at archives plugin for example.

11 years ago

So I pulled all that code from the admin plugin (latestPages) ... Doesn't latestPages return an array? It certainly seems to in the debugger. To be fair, I am brand new to PHP, so perhaps I am not understanding you right.

The problem here is not that the plugin doesn't work (debugger shows that it does) more that I can't get it to show on the page from the twig call...

Thanks again for help.

11 years ago

Thanks - with a little tweaking I now have it working. I wasn't calling the plugin with grav.twig.latestPages.... That fixed things - Thanks!

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1349 9 years ago
Archive · by Muut Archive, 9 years ago
2 934 9 years ago
Archive · by Muut Archive, 9 years ago
2 4060 9 years ago
Archive · by Muut Archive, 9 years ago
1 2946 9 years ago
Archive · by Muut Archive, 9 years ago
3 1118 9 years ago