<h1>Short answer:</h1>
<p>No.</p>
<h1>Longer answer:</h1>
<p>Sorry, no... but there's a good reason for that.</p>
<p>Twig is a <em>template</em> language. PHP is <em>business logic</em>.</p>
<p>In proper application design, you separate the business logic from its presentation typically in some sort of MVC structure.</p>
<p>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.)</p>
<p>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.</p>
<p>Twig itself is obviously PHP so if you want to extend it, you need to write PHP.</p>
<h1>What kind of Twig extension do you want to create?</h1>
<p>The most common is a <a href="https://craftcms.com/docs/templating/filters" rel="nofollow noreferrer">Twig filter</a>, where a template sends Twig a value and then your code wrestles with the data and sends something back.</p>
<p>This is straightforward to do. If you use <a href="https://pluginfactory.io" rel="nofollow noreferrer">pluginfactory.io</a> it will generate the necessary bootstrapping needed to get a Twig filter working.  Also check out Cameron's Spear's <a href="https://cameronspear.com/blog/craft-cms-twig-filter-extension/" rel="nofollow noreferrer">write up</a> on the nitty gritty.</p>
<p>On the other end of the spectrum, creating your own <a href="https://twig.symfony.com/doc/2.x/advanced.html#tags" rel="nofollow noreferrer">tags</a> (such as <code>{% myTwigTag %}</code>) and tests requires more work.</p>
<p>That should be more of a rare occurrence but it <a href="https://github.com/marionnewlevant/craft-twig_perversion" rel="nofollow noreferrer">has been done</a> (for example Marion Newlevant's Break/Continue extension).</p>

CC BY-SA 3.0
2017-10-27T18:14:21.837Z
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
CC BY-SA 3.0
2017-10-28T17:30:23.070Z
@BradBell: Boom, noted.
RitterKnight
CC BY-SA 3.0
2017-11-02T05:59:05.167Z
Yes @BradBell I was trying to create a module and load it using composer but i couldn't get succeed.
Kantesh Hotchandani
CC BY-SA 3.0
2017-10-27T15:09:33.213Z
2017-10-28T17:28:03.157Z
2020-06-17T08:56:27.903Z
RitterKnight
<p>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. </p>

CC BY-SA 3.0
2017-11-02T05:51:35.387Z
I have used a plugin to create twig extension
Kantesh Hotchandani
CC BY-SA 3.0
2017-10-27T11:21:09.473Z
2017-10-27T11:21:09.473Z
Tim Knight
<p>I want to create custom twig extension without any plugin dependency. Is anyone has solution for this?</p>

CC BY-SA 3.0
2017-10-27T11:18:25.957Z
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
Robin Schambach
CC BY-SA 3.0
2017-11-02T05:56:51.090Z
I just want to remove the dependency because if I disable the plugin it gives me an error on front page for unknow function.
Kantesh Hotchandani
<templating><twigextension>
Is there any way to create twig extension without a plugin?
# Is there any way to create twig extension without a plugin?

(Asked by: username_0 on 2017-10-27)

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

**Tags:** templating, twigextension
> **Comments:**
> **username_1**: 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  
> **username_0**: I just want to remove the dependency because if I disable the plugin it gives me an error on front page for unknow function.  

---

## 2 answers

### Answer by username_2 on 2017-10-27. Score: 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](https://craftcms.com/docs/templating/filters), 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](https://pluginfactory.io) it will generate the necessary bootstrapping needed to get a Twig filter working. Also check out Cameron's Spear's [write up](https://cameronspear.com/blog/craft-cms-twig-filter-extension/) on the nitty gritty.

On the other end of the spectrum, creating your own [tags](https://twig.symfony.com/doc/2.x/advanced.html#tags) (such as `{% myTwigTag %}`) and tests requires more work.

That should be more of a rare occurrence but it [has been done](https://github.com/marionnewlevant/craft-twig_perversion) (for example Marion Newlevant's Break/Continue extension).

> **Comments:**
> **username_3**: 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.  
> **username_2**: @BradBell: Boom, noted.  
> **username_0**: Yes @BradBell I was trying to create a module and load it using composer but i couldn't get succeed.  

---

### Answer by username_4 on 2017-10-27. Score: 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.

> **Comments:**
> **username_0**: I have used a plugin to create twig extension  

---

