<blockquote>
  <p>Is there a way to create custom twig tags?</p>
</blockquote>

<p>Sure.  You can look in <code>craft/app/etc/templating/twigextensions/</code> to see how Craft creates it's own <a href="http://buildwithcraft.com/docs/templating/tags" rel="nofollow">custom Twig tags</a>, which is how a plugin would do it using the <a href="http://buildwithcraft.com/docs/plugins/hooks-reference#addTwigExtension" rel="nofollow">addTwigExtension</a> hook.</p>

CC BY-SA 3.0
2014-12-09T17:19:24.087Z
2014-12-09T17:19:24.087Z
Brad Bell
<p>I'm working on a site with a very complex permissions structure (based on groups, dates, dynamic overrides etc). I've written a custom permissions plugin to handle this in a centralized fashion.</p>

<p>In this plugin I've exposed some of the functionality through twig variables:</p>

<pre><code>{{ craft.permissions.require('update','entry.projects', project.id) }}
</code></pre>

<p>This function is similar to the {% requireLogin %} where it performs redirects and stops craft processing etc if it fails the permissions check.</p>

<p>I need to somehow execute this as a twig tag so that I can place it outside the {% block content %} tags.</p>

<p>Is there a way to create custom twig tags?</p>

<hr>

<p>Alternatively; 
as a work around I'm using the 'if' tag which is a bit messy:</p>

<pre><code>{% if craft.permissions.require('update','entry.projects', project.id) %}{% endif %}
</code></pre>

<p>A different tag dedicated to running functions could be useful in the next release, eg:</p>

<pre><code>{% exec craft.permissions.require('update','entry.projects', project.id) %}
</code></pre>

CC BY-SA 3.0
2014-12-09T21:50:49.953Z
You may also want to look at Twig's [`do` tag](http://twig.sensiolabs.org/doc/tags/do.html)... It basically just executes a statement without returning anything.
Lindsey D
CC BY-SA 3.0
2014-12-10T08:03:27.960Z
awesome thanks mate
Jered Masters
<templating><tags>
Custom Twig Tags
# Custom Twig Tags

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

I'm working on a site with a very complex permissions structure (based on groups, dates, dynamic overrides etc). I've written a custom permissions plugin to handle this in a centralized fashion.
In this plugin I've exposed some of the functionality through twig variables:
{{ craft.permissions.require('update','entry.projects', project.id) }}

This function is similar to the {% requireLogin %} where it performs redirects and stops craft processing etc if it fails the permissions check.
I need to somehow execute this as a twig tag so that I can place it outside the {% block content %} tags.
Is there a way to create custom twig tags?

Alternatively; 
as a work around I'm using the 'if' tag which is a bit messy:
{% if craft.permissions.require('update','entry.projects', project.id) %}{% endif %}

A different tag dedicated to running functions could be useful in the next release, eg:
{% exec craft.permissions.require('update','entry.projects', project.id) %}

**Tags:** templating, tags
> **Comments:**
> **username_1**: You may also want to look at Twig's [`do` tag](http://twig.sensiolabs.org/doc/tags/do.html)... It basically just executes a statement without returning anything.  
> **username_0**: awesome thanks mate  

---

## 1 answers

### Answer by username_2 on 2014-12-09. Score: 3 | Accepted answer

> Is there a way to create custom twig tags?

Sure. You can look in `craft/app/etc/templating/twigextensions/` to see how Craft creates it's own [custom Twig tags](http://buildwithcraft.com/docs/templating/tags), which is how a plugin would do it using the [addTwigExtension](http://buildwithcraft.com/docs/plugins/hooks-reference#addTwigExtension) hook.


---

