Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-04T09:33:56.747Z
Answers.LastActivityDate: 2020-07-07T01:01:39.243Z
Answers.LastEditDate: 2020-07-07T01:01:39.243Z
Answers.UserDisplayName: Piotr Pogorzelski
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-07T15:37:37.423Z
Answers.Text: 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?
Answers.UserDisplayName: H2ONOCK
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-07T17:47:20.063Z
Answers.Text: @H2ONOCK yes; you absolutely need to do this with a plugin, and put the classes within its folder.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-08T09:08:44.513Z
Answers.Text: Fantastic, nailed it, over the moon! Nice one, thanks! :)
Answers.UserDisplayName: H2ONOCK
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-07T15:18:57.670Z
Answers.LastActivityDate: 2015-11-07T17:48:31.953Z
Answers.LastEditDate: 2015-11-07T17:48:31.953Z
Answers.UserDisplayName: Brandon Kelly
Body: <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>
  
Tags: <plugin-development><templating>
Title: How do you create a custom Twig tag?
ThreadText: # 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](http://twig.sensiolabs.org/doc/advanced.html#defining-a-token-parser) and a [Node](http://twig.sensiolabs.org/doc/advanced.html#defining-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](http://twig.sensiolabs.org/doc/advanced.html#creating-an-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](http://twig.sensiolabs.org/doc/advanced.html#id4)).
  
  (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](http://buildwithcraft.com/docs/plugins/hooks-reference#addTwigExtension) hook:
  
  ```php
  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.
  
  
  ---
  
  