<p>That's not the answer to this question, but it's kinda related...</p>
<p>Sometimes its worth to consider using <code>{% do %}</code> statement instead of using a custom tag. No need to handle Twig tokens and other complicated matters related to the custom tag.</p>
<p><a href="https://twig.symfony.com/doc/2.x/tags/do.html" rel="nofollow noreferrer">https://twig.symfony.com/doc/2.x/tags/do.html</a></p>
<p><code>{% do %}</code> can use any variable defined by plugin/module - it just doesn't output anything.</p>

CC BY-SA 4.0
2020-07-04T09:33:56.747Z
2020-07-07T01:01:39.243Z
2020-07-07T01:01:39.243Z
Piotr Pogorzelski
<p>There are two classes behind each Twig tag: a <a href="http://twig.sensiolabs.org/doc/advanced.html#defining-a-token-parser">Token Parser</a> and a <a href="http://twig.sensiolabs.org/doc/advanced.html#defining-a-node">Node</a>.</p>

<p>The Token Parser is responsible for actually parsing the template, once Twig has tokenized it, and returning your Node class to represent it. The Node class is responsible for compiling itself into actual PHP code.</p>

<p><code>{% requireLogin %}</code> is about as simple as it gets, because it doesn’t have to worry about parsing parameters, expressions, sub-content, etc. You can use its Token Parser and Node classes as a starting point - they’re located in craft/app/etc/templating/twigextensions/.</p>

<p>Once you’ve created those classes, you will need to create a <a href="http://twig.sensiolabs.org/doc/advanced.html#creating-an-extension">Twig Extension</a>, which will be used to connect your TokenParser to Craft’s instance of Twig. Give it a getTokenParsers() method which returns an instance of your Token Parser class (<a href="http://twig.sensiolabs.org/doc/advanced.html#id4">example</a>).</p>

<p>(Note that if you use the <code>Craft</code> namespace in these clasess, you will need to prefix all of Twig’s class names with <code>\</code>, e.g. <code>\Twig_TokenParser</code>.)</p>

<p>Finally you need to register your Twig Extension from your plugin’s primary class using the <a href="http://buildwithcraft.com/docs/plugins/hooks-reference#addTwigExtension">addTwigExtension</a> hook:</p>



<pre class="lang-php prettyprint-override"><code>public function addTwigExtension()
{
    // Import the Twig Extension, Token Parser, and Node classes
    Craft::import('plugins.mypluginfolder.MyTwigExtension');
    Craft::import('plugins.mypluginfolder.MyTokenParser');
    Craft::import('plugins.mypluginfolder.MyNode');

    // Return a new instance of it
    return new MyTwigExtension();
}
</code></pre>

CC BY-SA 3.0
2015-11-07T15:37:37.423Z
Wow, thanks for this...I wouldn't have EVER got to that answer! Can I just check though... if I modify files inside craft/app/etc/templating will they get overwritten when craft updates?
H2ONOCK
CC BY-SA 3.0
2015-11-07T17:47:20.063Z
@H2ONOCK yes; you absolutely need to do this with a plugin, and put the classes within its folder.
Brandon Kelly
CC BY-SA 3.0
2015-11-08T09:08:44.513Z
Fantastic, nailed it, over the moon! Nice one, thanks! :)
H2ONOCK
CC BY-SA 3.0
2015-11-07T15:18:57.670Z
2015-11-07T17:48:31.953Z
2015-11-07T17:48:31.953Z
Brandon Kelly
<p>Say I'm building a plugin that adds user memberships to the website. I want to check on my front end templates that user's membership is active and I have built the functionality inside the plugin to do this.</p>
<p>How do I use a tag like <code>{% requireMembership %}</code> in place of the normal <code>{% requireLogin %}</code> to call the function that I've built? I thought it would be simple but can't find documentation on this anywhere.</p>

<plugin-development><templating>
How do you create a custom Twig tag?
# How do you create a custom Twig tag?

(Asked by: username_0 on 2015-11-07)

Say I'm building a plugin that adds user memberships to the website. I want to check on my front end templates that user's membership is active and I have built the functionality inside the plugin to do this.

How do I use a tag like `{% requireMembership %}` in place of the normal `{% requireLogin %}` to call the function that I've built? I thought it would be simple but can't find documentation on this anywhere.

**Tags:** plugin-development, templating


---

## 2 answers

### Answer by username_2 on 2015-11-07. Score: 12 | Accepted answer

There are two classes behind each Twig tag: a Token Parser and a Node.
The Token Parser is responsible for actually parsing the template, once Twig has tokenized it, and returning your Node class to represent it. The Node class is responsible for compiling itself into actual PHP code.
{% requireLogin %} is about as simple as it gets, because it doesn’t have to worry about parsing parameters, expressions, sub-content, etc. You can use its Token Parser and Node classes as a starting point - they’re located in craft/app/etc/templating/twigextensions/.
Once you’ve created those classes, you will need to create a Twig Extension, which will be used to connect your TokenParser to Craft’s instance of Twig. Give it a getTokenParsers() method which returns an instance of your Token Parser class (example).
(Note that if you use the Craft namespace in these clasess, you will need to prefix all of Twig’s class names with \, e.g. \Twig_TokenParser.)
Finally you need to register your Twig Extension from your plugin’s primary class using the addTwigExtension hook:
public function addTwigExtension()
{
    // Import the Twig Extension, Token Parser, and Node classes
    Craft::import('plugins.mypluginfolder.MyTwigExtension');
    Craft::import('plugins.mypluginfolder.MyTokenParser');
    Craft::import('plugins.mypluginfolder.MyNode');

    // Return a new instance of it
    return new MyTwigExtension();
}

> **Comments:**
> **username_0**: Wow, thanks for this...I wouldn't have EVER got to that answer! Can I just check though... if I modify files inside craft/app/etc/templating will they get overwritten when craft updates?  
> **username_2**: @username_0 yes; you absolutely need to do this with a plugin, and put the classes within its folder.  
> **username_0**: Fantastic, nailed it, over the moon! Nice one, thanks! :)  

---

### Answer by username_1 on 2020-07-04. Score: 1

That's not the answer to this question, but it's kinda related...

Sometimes its worth to consider using `{% do %}` statement instead of using a custom tag. No need to handle Twig tokens and other complicated matters related to the custom tag.

<https://twig.symfony.com/doc/2.x/tags/do.html>

`{% do %}` can use any variable defined by plugin/module - it just doesn't output anything.


---

