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) %}
  • 1
    You may also want to look at Twig's do tag... It basically just executes a statement without returning anything.
    – Lindsey D
    Dec 9 '14 at 21:50
  • awesome thanks mate Dec 10 '14 at 8:03

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, which is how a plugin would do it using the addTwigExtension hook.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.