I found several solutions to build a dynamic, complex navigation in craft. One is described here: Adding active class to globally cached navigation. Now i would like to create a more powerfull twig filter for creating different navigation and breadcrumb types with selecting of active links. I already wrote some test code in plain php and used for this the library simplehtmldom (helper for changing dom elements in php like in jquery). A demonstration about what i want to do can be found here: http://www.bahu.ch/craft/. Now i started with creating the twig filter, but i can't include the library in my plugin. Here is a quick overview of my plugin code.
<?php
namespace Craft;
use Twig_Extension;
use Twig_Filter_Method;
Now i need to include the library (i know, that the next line is incorrect)
include('simple_html_dom.php');
class k4NavigationTwigExtension extends \Twig_Extension
{
public function getName()
{
return 'k4Navigation';
}
public function getFilters()
{
return array(
'k4Navigation' => new Twig_Filter_Method($this, 'k4Navigation'),
);
}
public function k4Navigation($navigation)
{
$html = str_get_html($navigation);
on the next line the current url should replace the string "test.html"
$selectElem = $html->find('a[href="test.html"]')[0];
if (is_object($selectElem)){
// loop li and set class active
for ($i = 1; $i <= 20; $i++) {
$selectElem->setAttribute("class","active");
$selectElem = $selectElem->parent();
if (!is_object($selectElem)){
break;
}
}
}
return $html;
}
}
Maybe someone can give me a hint, how to includ the library and the first example? You can download the full test code and started plugin from here: https://github.com/tombauer/k4-navigation
Many thanks for any help!
Sunny regards Tom