Answers.Body: <p>When you call Craft's <a href="https://craftcms.com/classreference/services/TemplatesService#getTwig-detail" rel="nofollow">TemplatesService->getTwig()</a> (<code>craft()-&gt;templates-&gt;getTwig()</code>), you get an instance of the Twig environment and from there you could be able to call <a href="http://twig.sensiolabs.org/doc/advanced.html#tests" rel="nofollow"><code>addTest</code></a>.</p>
  
  <p>100% untested, but something like this should work:</p>
  
  <pre><code>$twig = craft()-&gt;templates-&gt;getTwig();
  $twig-&gt;addTest(new My_Twig_SimpleTest());
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-07T21:22:10.477Z
Answers.Text: Note that if you want to get back the same Twig environment instance that Craft is actually using to render templates, there are some caveats, outlined [here](http://craftcms.stackexchange.com/a/16492/1098).
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-08T14:42:20.597Z
Answers.Text: Thanks Mats Mikkel Rummelhoff (again). Those caveats were a response to one of my previous questions :)
Answers.UserDisplayName: Mark Notton
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-07T18:03:24.373Z
Answers.LastActivityDate: 2016-10-07T18:03:24.373Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <p>Thanks Brad, that's what I was after. In the case of what I was trying to achieve; the following in my plugins init function did the job:</p>
  
  <pre><code>$twig = craft()-&gt;templates-&gt;getTwig();
  
  $twig-&gt;addTest(new \Twig_SimpleTest('array', function ($value) {
    return is_array($value);
  }));
  </code></pre>
  
  <p>I always struggle with syntax, so hopefully this example will help others.</p>
  
  <hr>
  
  <p><strong>Craft 3 Solution</strong>:</p>
  
  <p>Add this to your plugins main init() function: </p>
  
  <pre><code>$this-&gt;addTests(Craft::$app-&gt;view-&gt;getTwig());
  </code></pre>
  
  <p>Then add this function after the init(): </p>
  
  <pre><code>private function addTests($twig) {
    // Is String
    $twig-&gt;addTest(new \Twig_SimpleTest('string', function ($value) {
      return is_string($value);
    }));
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-08T14:40:18.600Z
Answers.LastActivityDate: 2017-12-31T17:50:16.513Z
Answers.LastEditDate: 2017-12-31T17:50:16.513Z
Answers.UserDisplayName: Mark Notton
Body: <p>I was on the Twig StackExchange forum and found <a href="https://stackoverflow.com/questions/13876023/check-if-variable-is-string-or-array-in-twig#answer-29642662"><strong>an answer that suited my needs</strong></a>.</p>
  
  <p>But I can't for the life of me figure out how to implement this into a Craft CMS Plugin.</p>
  
  <p>I'm familiar with <code>Twig_Function_Method</code> and <code>Twig_Filter_Method</code>, but I can't find anything on <code>Twig_SimpleTest</code>. </p>
  
  <p>Has the option to include "tests" been omitted from Craft CMS?</p>
  
Tags: <plugin-development><twigextension>
Title: Is it possible to extend twig with 'Twig_SimpleTest'?
ThreadText: # Is it possible to extend twig with 'Twig_SimpleTest'?
  
  (Asked by: username_0 on 2016-10-07)
  
  I was on the Twig StackExchange forum and found [**an answer that suited my needs**](https://stackoverflow.com/questions/13876023/check-if-variable-is-string-or-array-in-twig#answer-29642662).
  
  But I can't for the life of me figure out how to implement this into a Craft CMS Plugin.
  
  I'm familiar with `Twig_Function_Method` and `Twig_Filter_Method`, but I can't find anything on `Twig_SimpleTest`.
  
  Has the option to include "tests" been omitted from Craft CMS?
  
  **Tags:** plugin-development, twigextension
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2016-10-07. Score: 3 | Accepted answer
  
  When you call Craft's [TemplatesService->getTwig()](https://craftcms.com/classreference/services/TemplatesService#getTwig-detail) (`craft()->templates->getTwig()`), you get an instance of the Twig environment and from there you could be able to call [`addTest`](http://twig.sensiolabs.org/doc/advanced.html#tests).
  
  100% untested, but something like this should work:
  
  ```
  $twig = craft()->templates->getTwig();
  $twig->addTest(new My_Twig_SimpleTest());
  
  ```
  
  > **Comments:**
  > **username_2**: Note that if you want to get back the same Twig environment instance that Craft is actually using to render templates, there are some caveats, outlined [here](http://craftcms.stackexchange.com/a/16492/1098).  
  > **username_0**: Thanks username_2 (again). Those caveats were a response to one of my previous questions :)  
  
  ---
  
  ### Answer by username_0 on 2016-10-08. Score: 3
  
  Thanks username_1, that's what I was after. In the case of what I was trying to achieve; the following in my plugins init function did the job:
  
  ```
  $twig = craft()->templates->getTwig();
  
  $twig->addTest(new \Twig_SimpleTest('array', function ($value) {
    return is_array($value);
  }));
  
  ```
  
  I always struggle with syntax, so hopefully this example will help others.
  
  ---
  
  **Craft 3 Solution**:
  
  Add this to your plugins main init() function:
  
  ```
  $this->addTests(Craft::$app->view->getTwig());
  
  ```
  
  Then add this function after the init():
  
  ```
  private function addTests($twig) {
    // Is String
    $twig->addTest(new \Twig_SimpleTest('string', function ($value) {
      return is_string($value);
    }));
  }
  
  ```
  
  
  ---
  
  