Answers.Body: <p><a href="http://buildwithcraft.com/classreference/services/TemplatesService#hook-detail" rel="nofollow">Template hooks</a> are useful if you need to change the current twig context array somewhere inside of a template. If you make any changes to <code>$context</code> inside of your hook's function, those changes will be available after your hook tag. </p>
  
  <p>If you just need to return some text or an array, a <a href="http://buildwithcraft.com/docs/plugins/variables" rel="nofollow">variable</a> will do the trick just fine! </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-26T14:34:33.917Z
Answers.Text: Gotcha, thanks Aaron. Yes, variables seem to do the trick and be a much better solution. It wasn't quite clear from things I've read what the purpose of hooks were, and I went up the wrong path.
Answers.UserDisplayName: Phil Gyford
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-26T14:22:27.597Z
Answers.LastActivityDate: 2015-06-26T14:22:27.597Z
Answers.UserDisplayName: Aaron Berkowitz
Body: <p>I'm writing a plugin which has a hook like: </p>
  
  <pre><code>craft()-&gt;templates-&gt;hook('myHook', function(&amp;$context)
  {
      return array('hello', 'there');
  });
  </code></pre>
  
  <p>While I can call it in a template with:</p>
  
  <pre><code>{% hook myHook %}
  </code></pre>
  
  <p>I get an "Array to string conversion", understandably. But I can't work out how to treat the returned data like an array. How can I use <code>hook 'myHook'</code> in a <code>for</code> loop, or <code>set</code> it to a variable? It must be simple, but I've exhausted my random attempts.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-06-26T13:16:46.653Z
Comments.Text: I'm now thinking maybe I should be using a Variable for this instead. I'm not sure what the pros/cons are of either method.
Comments.UserDisplayName: Phil Gyford
Tags: <templating><hook>
Title: Displaying an array returned from a hook
ThreadText: # Displaying an array returned from a hook
  
  (Asked by: username_0 on 2015-06-26)
  
  I'm writing a plugin which has a hook like:
  
  ```
  craft()->templates->hook('myHook', function(&$context)
  {
      return array('hello', 'there');
  });
  
  ```
  
  While I can call it in a template with:
  
  ```
  {% hook myHook %}
  
  ```
  
  I get an "Array to string conversion", understandably. But I can't work out how to treat the returned data like an array. How can I use `hook 'myHook'` in a `for` loop, or `set` it to a variable? It must be simple, but I've exhausted my random attempts.
  
  **Tags:** templating, hook
  
  > **Comments:**
  > **username_0**: I'm now thinking maybe I should be using a Variable for this instead. I'm not sure what the pros/cons are of either method.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-06-26. Score: 2 | Accepted answer
  
  [Template hooks](http://buildwithcraft.com/classreference/services/TemplatesService#hook-detail) are useful if you need to change the current twig context array somewhere inside of a template. If you make any changes to `$context` inside of your hook's function, those changes will be available after your hook tag.
  
  If you just need to return some text or an array, a [variable](http://buildwithcraft.com/docs/plugins/variables) will do the trick just fine!
  
  > **Comments:**
  > **username_0**: Gotcha, thanks username_1. Yes, variables seem to do the trick and be a much better solution. It wasn't quite clear from things I've read what the purpose of hooks were, and I went up the wrong path.  
  
  ---
  
  