<p>I'd say; use twig functions/filters when the data you want to operate on comes from your template, plugin variables when you need to operate on data from your plugin/another source. </p>

<p>You can, to my knowledge, do the exact same things with both. But the purpose of twig filters is to manipulate data in your templates. If the filter relies on tapping into Craft to work, I'd rethink using one. </p>

<p>But that's just my opinion, I don't think there's been a lot of discussion about this in the community. :)</p>

CC BY-SA 3.0
2015-05-07T21:04:32.930Z
2015-05-07T21:04:32.930Z
André Elvan
<p>The following answer is all personal opinion of course, but here's a key quote from the <a href="http://buildwithcraft.com/docs/plugins/variables" rel="nofollow">docs</a>:</p>

<blockquote>
  <p>Many of your template variable methods will likely be simple wrappers for your service API methods.</p>
</blockquote>

<p>If you look at <a href="https://buildwithcraft.com/classreference#craft.app.variables" rel="nofollow">Craft's built-in Variables</a>, they're all about <em>exposing data</em>. Personally, I like to think of Variables as <em>template APIs</em>.</p>

<p>Twig Filters, on the other hand, is all about <em>modifying</em> data (e.g. capitalizing strings, rounding numbers, formatting dates, sorting arrays etc). If you compare Craft's built-in Variables to its <a href="https://buildwithcraft.com/classreference/etc/templating/twigextensions/CraftTwigExtension" rel="nofollow">Filters</a>, I think the distinction between the two starts to become fairly clear.</p>

<p>To exemplify:</p>

<p><strong>Bad use of Variables:</strong></p>

<pre><code>{% set sortedEntries = craft.myPlugin.sortByAwesomeLevel( entries ) %}
{{ craft.myPlugin.makeEntryTitleAwesome( entry.title ) }}
</code></pre>

<p><em>The above template code use</em> Variable <em>methods to modify data already present in the template. It works, but it looks pretty convoluted and doesn't really make sense, neither from a</em> Twig <em>nor</em> Craft <em>point of view. Here, using</em> Filters <em>would be much more appropriate:</em></p>

<pre><code>{% set sortedEntries = entries | sortBy( 'awesomeLevel' ) %}
{{ entry.title | makeAwesome }}
</code></pre>

<p><strong>Good use of Variables:</strong></p>

<pre><code>{% set awesomeEntries = craft.myPlugin.getAwesomeEntries %}
{% set sortedAwesomeEntries = craft.myPlugin.getAwesomeEntries( { 'sortBy' : 'awesomeLevel' } ) %}
</code></pre>

<p><em>Here, MyPlugin exposes a "template API" method called <code>getAwesomeEntries</code>. The only thing it does, is to pull entries that are awesome, optionally taking an arguments object for sorting or the like. Obviously, this would not be feasible to implement as a Filter:</em> </p>

<pre><code>{% set awesomeEntries = entries | getAwesomeEntries %}
</code></pre>

<p><strong>In short – if you need to <em>expose</em> data, use a Variable. If you need to <em>modify</em> data, use Filters.</strong></p>

CC BY-SA 3.0
2015-05-08T09:39:54.600Z
2015-05-08T09:39:54.600Z
Mats Mikkel Rummelhoff
<p>Is there a particular school of thought on which is best to use?</p>

<p>They both appear to offer the same thing - exposing data and functionality to templates.</p>

<plugin-development><templating>
Plugin variables class versus twig functions/filters class
# Plugin variables class versus twig functions/filters class

(Asked by: username_0 on 2015-05-06)

Is there a particular school of thought on which is best to use?

They both appear to offer the same thing - exposing data and functionality to templates.

**Tags:** plugin-development, templating


---

## 2 answers

### Answer by username_2 on 2015-05-08. Score: 4 | Accepted answer

The following answer is all personal opinion of course, but here's a key quote from the docs:

Many of your template variable methods will likely be simple wrappers for your service API methods.

If you look at Craft's built-in Variables, they're all about exposing data. Personally, I like to think of Variables as template APIs.
Twig Filters, on the other hand, is all about modifying data (e.g. capitalizing strings, rounding numbers, formatting dates, sorting arrays etc). If you compare Craft's built-in Variables to its Filters, I think the distinction between the two starts to become fairly clear.
To exemplify:
Bad use of Variables:
{% set sortedEntries = craft.myPlugin.sortByAwesomeLevel( entries ) %}
{{ craft.myPlugin.makeEntryTitleAwesome( entry.title ) }}

The above template code use Variable methods to modify data already present in the template. It works, but it looks pretty convoluted and doesn't really make sense, neither from a Twig nor Craft point of view. Here, using Filters would be much more appropriate:
{% set sortedEntries = entries | sortBy( 'awesomeLevel' ) %}
{{ entry.title | makeAwesome }}

Good use of Variables:
{% set awesomeEntries = craft.myPlugin.getAwesomeEntries %}
{% set sortedAwesomeEntries = craft.myPlugin.getAwesomeEntries( { 'sortBy' : 'awesomeLevel' } ) %}

Here, MyPlugin exposes a "template API" method called getAwesomeEntries. The only thing it does, is to pull entries that are awesome, optionally taking an arguments object for sorting or the like. Obviously, this would not be feasible to implement as a Filter: 
{% set awesomeEntries = entries | getAwesomeEntries %}

In short – if you need to expose data, use a Variable. If you need to modify data, use Filters.


---

### Answer by username_1 on 2015-05-07. Score: 2

I'd say; use twig functions/filters when the data you want to operate on comes from your template, plugin variables when you need to operate on data from your plugin/another source.

You can, to my knowledge, do the exact same things with both. But the purpose of twig filters is to manipulate data in your templates. If the filter relies on tapping into Craft to work, I'd rethink using one.

But that's just my opinion, I don't think there's been a lot of discussion about this in the community. :)


---

