Site Helpers

While addons can encompass a large feature set that might be shared, sold, or reused; site helpers provide a quick way to add functionality.


Overview

Site helpers can be thought of as one catch-all addon for your site.

If you have a little piece of functionality you need to add - like a quick filter or tag - throw the code into the helper file instead of crafting an entire addon.

Files

A number of addon aspects (tags, filters, and modifiers) will have a single corresponding helper file. These are located in site/helpers.

site
`-- helpers
    |-- Tags.php
    |-- Modifiers.php
    `-- Filters.php

Generating

You may generate the helper files by running their corresponding commands:

php please make:tags-helper
php please make:modifiers-helper
php please make:filters-helper
php please make:controller-helper name

If the files already exist, Statamic will prompt you for a method name which will be appended to the file.

Tags

Tags are used in your templates with the site: prefix. Much like how an addon’s tag prefix would be the addon name.

{{ site:your_tag }}

This corresponds to the camelCased method:

<?php

namespace Statamic\SiteHelpers;

use Statamic\Extend\Tags as AbstractTags;

class Tags extends AbstractTags
{
    public function yourTag()
    {
        //
    }
}

Tags inside a site helper work almost identically to how Tags work within the context of an addon.

Modifiers

Modifiers are used in your templates just like bundled or addons’ modifiers would. They correspond to the camelCased method name.

<!-- Pipe syntax -->
{{ variable | your_modifier:paramOne:paramTwo }}

<!-- Parameter syntax -->
{{ variable your_modifier="paramOne|paramTwo" }}
<?php

namespace Statamic\SiteHelpers;

class Modifiers
{
    public function yourModifier($value, $params, $context)
    {
        //
    }
}

Filters

Filters are used in your templates just like addons’ filters.

{{ collection:blog filter="your_filter" }}
    ...
{{ /collection:blog }}

They correspond to the camelCased method name.

<?php

namespace Statamic\SiteHelpers;

use Statamic\Extend\Filter;

class Filters extends Filter
{
    public function yourFilter($collection)
    {
        return $collection->filter(function ($entry) {
            return $entry;
        });
    }
}

Filters inside a site helper work almost identically to how Filters work within the context of an addon.

Controllers

You may have multiple controller classes, which can be used for the Controller Routes feature.

<?php

namespace Statamic\SiteHelpers;

use Statamic\Extend\Controller;

class FoodController extends Controller
{
    public function bacon()
    {
        return 'Bacon!';
    }
}

For example, when adding /foods/bacon: FoodController@bacon to your routes file, the contents of the bacon method will be returned.

Last modified on February 16, 2018