<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>

CC BY-SA 3.0
2015-06-26T14:34:33.917Z
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.
Phil Gyford
CC BY-SA 3.0
2015-06-26T14:22:27.597Z
2015-06-26T14:22:27.597Z
Aaron Berkowitz
<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>

CC BY-SA 3.0
2015-06-26T13:16:46.653Z
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.
Phil Gyford
<templating><hook>
Displaying an array returned from a hook
# 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 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.  

---

