How do I create a custom Twig extension which includes third party code? 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.

   &lt;?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' =&gt; 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-&gt;find('a[href="test.html"]')[0];

    if (is_object($selectElem)){
       // loop li and set class active
        for ($i = 1; $i &lt;= 20; $i++) {
            $selectElem-&gt;setAttribute("class","active");
            $selectElem =  $selectElem-&gt;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
Douglas McDonald's answer is correct with one issue though. The import statement is actually case sensitive


  Craft::import('plugins.myplugin.twigextensions.MyTwigExtension');


I also have 2 additional notes:


The getName() method is deprecated so you shouldn't bother with it
The getFilters() method can be written much shorter, for example:


public function getFilters()
{
    return [
        'myfilter' =&gt; new \Twig_Filter_Method($this, 'myfilter'),
    ];
}