cambis/silverstripe-classnames

Classnames package for Silverstripe.

v0.1.1 2023-09-16 03:10 UTC

This package is auto-updated.

Last update: 2024-10-16 05:53:39 UTC


README

This package is a Silverstripe port of php-classnames.

Getting started

Install the package via composer.

composer require cambis/silverstripe-classnames

Usage

This package provides the $Cn method for use Silverstripe templates. Arguments must be parsed as a JSON encoded array.

One way to accomplish this is to create a function that returns the appropriate encoded values, i.e. getClassnames().

<?php

use JsonSerializable;
use SilverStripe\ORM\DataObject;

class MyObject extends DataObject implements JsonSerializable
{
    public function getClassnames(): string
    {
        return json_encode($this);
    }

    public function jsonSerialize(): mixed
    {
        return [
            ['foo' => true],
            'bar',
        ];
    }
}

Then call the function in your template file.

<!-- MyObject.ss -->
<p class="{$Cn($Classnames)}">Classnames</p>