1

I want to create custom twig extension without any plugin dependency. Is anyone has solution for this?

2
  • What's the problem in creating a plugin? It takes hardly 1 minute. The only solution that comes in my mind is writing directly in craft files, but they'll be removed as soon as you update the system Commented Oct 27, 2017 at 11:18
  • I just want to remove the dependency because if I disable the plugin it gives me an error on front page for unknow function. Commented Nov 2, 2017 at 5:56

2 Answers 2

3

Short answer:

No.

Longer answer:

Sorry, no... but there's a good reason for that.

Twig is a template language. PHP is business logic.

In proper application design, you separate the business logic from its presentation typically in some sort of MVC structure.

This sounds complicated, but it really makes your website/application a lot easier to maintain over time and keeps the different components from bumping into each other which can also improve security. (I'm looking at you WordPress where template code is often scattered over various plugins, themes, and much more.)

Plugins (and also modules upcoming in Craft 3) typically fall into the business logic category. Twig does a lot by itself but sometimes you need to write some PHP to do more—which I'm assuming you're trying to avoid.

Twig itself is obviously PHP so if you want to extend it, you need to write PHP.

What kind of Twig extension do you want to create?

The most common is a Twig filter, where a template sends Twig a value and then your code wrestles with the data and sends something back.

This is straightforward to do. If you use pluginfactory.io it will generate the necessary bootstrapping needed to get a Twig filter working. Also check out Cameron's Spear's write up on the nitty gritty.

On the other end of the spectrum, creating your own tags (such as {% myTwigTag %}) and tests requires more work.

That should be more of a rare occurrence but it has been done (for example Marion Newlevant's Break/Continue extension).

3
  • 2
    Technically possible in Craft 3 by creating a module instead of a plugin, but it's still PHP code, which I think is what the OP is trying to avoid.
    – Brad Bell
    Commented Oct 27, 2017 at 18:14
  • @BradBell: Boom, noted. Commented Oct 28, 2017 at 17:30
  • Yes @BradBell I was trying to create a module and load it using composer but i couldn't get succeed. Commented Nov 2, 2017 at 5:59
1

I don’t believe there’s a way to create an actual Twig extension without making a plugin. However, depending on what you’re wanting your extension to do, would a Twig macro work? If you change your mind and decide to go the plugin route, creating a Twig extension that way isn’t too difficult.

1
  • I have used a plugin to create twig extension Commented Nov 2, 2017 at 5:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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