Answers.Body: <p>In your plugins main file add the following:</p>
  
  <pre><code>public function addTwigExtension()
  {
      Craft::import('plugins.myplugin.twigextensions.myTwigExtension');
  
      return new MyTwigExtension();
  }
  </code></pre>
  
  <p>In your plugin folder, add a new folder called 'twigextensions'.</p>
  
  <p>In the new 'twigextensions' folder, add your twig filter 'MyTwigExtension.php'.</p>
  
  <pre><code>&lt;?php
  
  namespace Craft;
  
  use Twig_Extension;
  use Twig_Filter_Method;
  
  class MyTwigExtension extends \Twig_Extension
  {
  
      public function getName()
      {
          return 'My Twig Extension';
      }
  
      public function getFilters()
      {
          $returnArray = array();
          $methods = array(
              'myfilter',
          );
  
          foreach ($methods as $methodName) {
              $returnArray[$methodName] = new \Twig_Filter_Method($this, $methodName);
          }
  
          return $returnArray;
      }
  
      public function myfilter($content)
      {
          // perform filter operations
  
          return "My Filter Result";
      }
  
  }
  </code></pre>
  
  <p>You can thank Luke Holder's twig extension <a href="https://github.com/lukeholder/craft-inflect/releases" rel="nofollow">craft-inflect</a> for this!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-15T10:15:13.817Z
Answers.Text: Dear Douglas thank you for the answer. Whit the code i did now my plugin setup, but it does still not work. When i try to use the filter, it ends with a error "Undefined offset: 0". Maybe there is still a problem with including the needed library? Here is the link to the PHP and Plugin-Code [link](https://github.com/tombauer/k4-navigation). Maybe you see the problem?
Answers.UserDisplayName: Tom Bauer
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-15T11:37:45.823Z
Answers.Text: @giveandfindhelp See [this question](http://craftcms.stackexchange.com/questions/4859/plugin-dev-load-third-party-library) for some expert opinions on how to include third party libraries/classes in Craft plugins.
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-15T11:58:43.380Z
Answers.Text: @MatsMikkelRummelhoff Thanks. I updated the plugin, but it has still the same error. I added a better description in github to explain what i try to do - see https://github.com/tombauer/k4-navigation.
Answers.UserDisplayName: Tom Bauer
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-15T12:10:18.633Z
Answers.Text: That specific error message – ```Undefined offset: 0``` – just means that you attempted to access the first index of an array, and the array is either empty, or not an array at all. You have several calls like this in your code: ```$selectElem = $html->find( $selectedFilter)[0];```; note that if ```$html->find()``` returns a falsey value (or an empty array), you'd get the error message above when trying to access specific indexes. You should test whatever ```$html->find()``` returns (e.g. using ```empty()``` and ```is_array()``` before attempting to access that return value's data.
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-15T12:12:54.893Z
Answers.Text: I have to say though, that this is going into general PHP territory, and as such is technically outside of the scope for this site. I'd recommend looking to the [main Stack Overflow site](http://stackoverflow.com/) for any general PHP or Simple HTML DOM related questions or issues, as those aren't really related to Craft in any way :)
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-15T12:17:25.680Z
Answers.Text: Also – and please don't take this the wrong way, I'm just trying to be helpful – I'm honestly a little confused about your approach here. Wouldn't it be better to just feed your Twig filters a _dataset_ representing your navigational structure (e.g. a nested array of links and labels), and then output the necessary markup, rather than to first build the menu markup manually and then manipulate it with Simple HTML DOM? Seems a little roundabout as long as you have control over the initial markup/input. Just my 2 cents :)
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-15T12:41:40.063Z
Answers.Text: @MatsMikkelRummelhoff Thanks for your hint - i got working the plugin with a static code :-) - so the library works and now i have to find the problem with my dynamic content. And YES, your right, it's not a realy sexy solution and your solution with a dataset would be much better. For the moment i try to get working my "quick&dirty" solution (with standard, built in craft navigation function) and after i will try to optimize it. Thanks again.
Answers.UserDisplayName: Tom Bauer
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-15T14:18:25.197Z
Answers.Text: Sounds like a decent strategy, @giveandfindhelp. Good luck!
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-15T02:19:51.740Z
Answers.LastActivityDate: 2015-04-15T02:19:51.740Z
Answers.UserDisplayName: Douglas McDonald
Body: <p>I found several solutions to build a dynamic, complex navigation in craft. One is described here:
   <a href="https://craftcms.stackexchange.com/questions/4782/adding-active-class-to-globally-cached-navigation">Adding active class to globally cached navigation</a>. 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:
   <a href="http://www.bahu.ch/craft/" rel="nofollow noreferrer">http://www.bahu.ch/craft/</a>. 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.</p>
  
  <pre><code>   &lt;?php 
     namespace Craft;
     use Twig_Extension;
  
     use Twig_Filter_Method;
  </code></pre>
  
  <p>Now i need to include the library (i know, that the next line is incorrect)</p>
  
  <pre><code>   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);
  </code></pre>
  
  <p>on the next line the current url should replace the string "test.html"</p>
  
  <pre><code>    $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;
  
     }
  
     }
  </code></pre>
  
  <p>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: <a href="https://github.com/tombauer/k4-navigation" rel="nofollow noreferrer">https://github.com/tombauer/k4-navigation</a></p>
  
  <p>Many thanks for any help!</p>
  
  <p>Sunny regards
  Tom</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-04-14T18:16:31.810Z
Comments.Text: It's a bit difficult to figure out exactly what your question is. Can you be more specific?
Comments.UserDisplayName: Mats Mikkel Rummelhoff
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-04-14T19:16:05.773Z
Comments.Text: I updated the question and hope it's now more specific. I simple like to include my standalone php code as a twig filter.
Comments.UserDisplayName: Tom Bauer
Tags: <plugin-development><templating><navigation>
Title: How do I create a custom Twig extension which includes third party code?
ThreadText: # How do I create a custom Twig extension which includes third party code?
  
  (Asked by: username_0 on 2015-04-14)
  
  I found several solutions to build a dynamic, complex navigation in craft. One is described here:
  [Adding active class to globally cached navigation](https://craftcms.stackexchange.com/questions/4782/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/username_0/k4-navigation>
  
  Many thanks for any help!
  
  Sunny regards
  Tom
  
  **Tags:** plugin-development, templating, navigation
  
  > **Comments:**
  > **username_1**: It's a bit difficult to figure out exactly what your question is. Can you be more specific?  
  > **username_0**: I updated the question and hope it's now more specific. I simple like to include my standalone php code as a twig filter.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2015-04-15. Score: 2 | Accepted answer
  
  In your plugins main file add the following:
  
  ```
  public function addTwigExtension()
  {
      Craft::import('plugins.myplugin.twigextensions.myTwigExtension');
  
      return new MyTwigExtension();
  }
  
  ```
  
  In your plugin folder, add a new folder called 'twigextensions'.
  
  In the new 'twigextensions' folder, add your twig filter 'MyTwigExtension.php'.
  
  ```
  <?php
  
  namespace Craft;
  
  use Twig_Extension;
  use Twig_Filter_Method;
  
  class MyTwigExtension extends \Twig_Extension
  {
  
      public function getName()
      {
          return 'My Twig Extension';
      }
  
      public function getFilters()
      {
          $returnArray = array();
          $methods = array(
              'myfilter',
          );
  
          foreach ($methods as $methodName) {
              $returnArray[$methodName] = new \Twig_Filter_Method($this, $methodName);
          }
  
          return $returnArray;
      }
  
      public function myfilter($content)
      {
          // perform filter operations
  
          return "My Filter Result";
      }
  
  }
  
  ```
  
  You can thank Luke Holder's twig extension [craft-inflect](https://github.com/lukeholder/craft-inflect/releases) for this!
  
  > **Comments:**
  > **username_0**: Dear username_2 thank you for the answer. Whit the code i did now my plugin setup, but it does still not work. When i try to use the filter, it ends with a error "Undefined offset: 0". Maybe there is still a problem with including the needed library? Here is the link to the PHP and Plugin-Code [link](https://github.com/username_0/k4-navigation). Maybe you see the problem?  
  > **username_1**: @giveandfindhelp See [this question](http://craftcms.stackexchange.com/questions/4859/plugin-dev-load-third-party-library) for some expert opinions on how to include third party libraries/classes in Craft plugins.  
  > **username_0**: @username_1 Thanks. I updated the plugin, but it has still the same error. I added a better description in github to explain what i try to do - see https://github.com/username_0/k4-navigation.  
  > **username_1**: That specific error message – ```Undefined offset: 0``` – just means that you attempted to access the first index of an array, and the array is either empty, or not an array at all. You have several calls like this in your code: ```$selectElem = $html->find( $selectedFilter)[0];```; note that if ```$html->find()``` returns a falsey value (or an empty array), you'd get the error message above when trying to access specific indexes. You should test whatever ```$html->find()``` returns (e.g. using ```empty()``` and ```is_array()``` before attempting to access that return value's data.  
  > **username_1**: I have to say though, that this is going into general PHP territory, and as such is technically outside of the scope for this site. I'd recommend looking to the [main Stack Overflow site](http://stackoverflow.com/) for any general PHP or Simple HTML DOM related questions or issues, as those aren't really related to Craft in any way :)  
  > **username_1**: Also – and please don't take this the wrong way, I'm just trying to be helpful – I'm honestly a little confused about your approach here. Wouldn't it be better to just feed your Twig filters a _dataset_ representing your navigational structure (e.g. a nested array of links and labels), and then output the necessary markup, rather than to first build the menu markup manually and then manipulate it with Simple HTML DOM? Seems a little roundabout as long as you have control over the initial markup/input. Just my 2 cents :)  
  > **username_0**: @username_1 Thanks for your hint - i got working the plugin with a static code :-) - so the library works and now i have to find the problem with my dynamic content. And YES, your right, it's not a realy sexy solution and your solution with a dataset would be much better. For the moment i try to get working my "quick&dirty" solution (with standard, built in craft navigation function) and after i will try to optimize it. Thanks again.  
  > **username_1**: Sounds like a decent strategy, @giveandfindhelp. Good luck!  
  
  ---
  
  