<blockquote>
  <p>Is there a way to hook into the current request before the template gets rendered? </p>
</blockquote>

<p>Sure, Craft does this in several places in the Control Panel itself.</p>

<p>A plugin can <a href="http://buildwithcraft.com/docs/plugins/hooks-reference#registerCpRoutes" rel="nofollow noreferrer">registerCpRoutes</a> hook.  From there, you'd define an action route like so:</p>

<pre><code>return array(
    'cocktails/(?P&lt;widgetId&gt;\d+)' =&gt; array('action' =&gt; 'cocktails/editCocktail'),
);
</code></pre>

<p>That will send the request to your plugin's CocktailsController's <code>actionEditCocktail</code>.  There you can prep any data that you want and pass it off to a template to render:</p>

<pre><code>$variables['name'] = 'Martini';
$variables['ingredients'] = array('Gin', 'Olives');

$this-&gt;renderTemplate('cocktails/_edit', $variables);
</code></pre>

<p>And access those from the template via <code>{{ entry.name }}</code> or:</p>

<pre><code>{% for ingredient in entry.ingredients %}
    {{ ingredient }}
{% endfor %}
</code></pre>

<blockquote>
  <p>Additionally this would allow me to use another template engine then twig.</p>
</blockquote>

<p>I'm not seeing how you'd easily be able to do this, though.</p>

<p><strong>Update:</strong></p>

<p>There is also the <a href="http://buildwithcraft.com/classreference/services/TemplatesService#hook-detail" rel="nofollow noreferrer">Template</a> <a href="https://craftcms.stackexchange.com/a/1407/57">Hooks</a>, which might get you where you're going.</p>

CC BY-SA 3.0
2014-12-21T19:35:51.090Z
I'm more looking for a kind of 'catch all' hook where I already know which entity, template e.t.c would get rendered. Alternatively, is there a way to figure out if a given url would match anything craft related? 

I found this answer which describes how to load craft from an external app. http://craftcms.stackexchange.com/questions/4609/how-can-craft-generated-content-html-be-accessed-via-an-external-php-app/4613#4613 I need something like this, but having the ability to find out if the current url matches e.g an entity or category.
Sidney Widmer
CC BY-SA 3.0
2014-12-21T22:16:16.933Z
Updated answer.  I think template hooks will work for you since at that point the route has been resolved and you've got access to the entire Twig context.
Brad Bell
CC BY-SA 3.0
2014-12-22T11:07:49.937Z
great, thanks! I think I can work with that :)
Sidney Widmer
CC BY-SA 3.0
2014-12-21T00:58:18.267Z
2014-12-21T22:13:38.103Z
2017-04-13T12:51:00.253Z
Brad Bell
<p>The only thing I don't really like about Craft is that I have to fetch and eventually manipulate my entry in the twig template itself. </p>

<p>Is there a way to hook into the current request before the template gets rendered? Like that I could create some kind of Controller where I fetch all the necessary data, prepare it and then pass it on to the template. </p>

<p>Additionally this would allow me to use another template engine then twig. </p>

<p>If this is not possible, would there be a way to only use craft as the backend? From outside of craft, is there an easy way to find out if there are matching entries/singles/categories to the current request? I'm not looking for a complete answer here, more some key points where I could start looking. </p>

<templating><hook>
Hook in before template gets rendered
# Hook in before template gets rendered

(Asked by: username_0 on 2014-12-19)

The only thing I don't really like about Craft is that I have to fetch and eventually manipulate my entry in the twig template itself.

Is there a way to hook into the current request before the template gets rendered? Like that I could create some kind of Controller where I fetch all the necessary data, prepare it and then pass it on to the template.

Additionally this would allow me to use another template engine then twig.

If this is not possible, would there be a way to only use craft as the backend? From outside of craft, is there an easy way to find out if there are matching entries/singles/categories to the current request? I'm not looking for a complete answer here, more some key points where I could start looking.

**Tags:** templating, hook


---

## 1 answers

### Answer by username_1 on 2014-12-21. Score: 0 | Accepted answer

Is there a way to hook into the current request before the template gets rendered? 

Sure, Craft does this in several places in the Control Panel itself.
A plugin can registerCpRoutes hook.  From there, you'd define an action route like so:
return array(
    'cocktails/(?P\d+)' => array('action' => 'cocktails/editCocktail'),
);

That will send the request to your plugin's CocktailsController's actionEditCocktail.  There you can prep any data that you want and pass it off to a template to render:
$variables['name'] = 'Martini';
$variables['ingredients'] = array('Gin', 'Olives');

$this->renderTemplate('cocktails/_edit', $variables);

And access those from the template via {{ entry.name }} or:
{% for ingredient in entry.ingredients %}
    {{ ingredient }}
{% endfor %}


Additionally this would allow me to use another template engine then twig.

I'm not seeing how you'd easily be able to do this, though.
Update:
There is also the Template Hooks, which might get you where you're going.

> **Comments:**
> **username_0**: I'm more looking for a kind of 'catch all' hook where I already know which entity, template e.t.c would get rendered. Alternatively, is there a way to figure out if a given url would match anything craft related?   I found this answer which describes how to load craft from an external app. http://craftcms.stackexchange.com/questions/4609/how-can-craft-generated-content-html-be-accessed-via-an-external-php-app/4613#4613 I need something like this, but having the ability to find out if the current url matches e.g an entity or category.  
> **username_1**: Updated answer.  I think template hooks will work for you since at that point the route has been resolved and you've got access to the entire Twig context.  
> **username_0**: great, thanks! I think I can work with that :)  

---

