acalvino4/craft-twig-ruleset

A twig coding style ruleset for the Craft community

1.0.3 2023-05-04 19:22 UTC

This package is auto-updated.

Last update: 2024-11-04 22:33:54 UTC


README

In the effort to automate as much style checking as possible, twig files would ideally not be missed in a Craft CMS project.

There is a good package for this, but it misses Craft conventions on a couple points.

  • camelCase variable names, instead of snake_case
  • use of the fairly ubiquitous empty coalesce operator from Andrew Welch

The ruleset provided here maintains the official ruleset, with the exception of addressing the two issues above. I am happy to consider further modifications requests / PR's if it can be demonstrated that they would be warranted by the Craft community.

Usage

Install

composer require --dev acalvino4/craft-twig-ruleset

Configure

<?php

// .twig_cs.php in project root

declare(strict_types=1);

use acalvino4\crafttwigruleset\CraftRuleset;
use FriendsOfTwig\Twigcs\Config\Config;
use FriendsOfTwig\Twigcs\Finder\TemplateFinder;

return Config::create()
    ->addFinder(TemplateFinder::create()->in('templates'))
    ->setRuleSet(CraftRuleset::class)
;

This config format is straight from twigcs, so any customizations or alternatives file names noted there will work as well. The only unique thing here is use of CraftRuleset.

Run

./vendor/bin/twigcs

Alternatively, add a composer script that calls twigcs, and/or use an IDE extension like Twigcs Linter.