twigphp/Twig · error · Twig\Error\RuntimeError

InlineStyle can only be created from iterable values.

Error message

InlineStyle can only be created from iterable values.

What it means

InlineDialeStyle/InlineStyle is a Twig html-extra value object representing CSS inline styles; its constructor only accepts iterable values (arrays, Traversables) which are spread into a list of style declarations. Passing a scalar or null raises this RuntimeError so malformed style data fails fast instead of producing broken HTML attributes.

Solutions

  1. Pass an array or Traversable of style declarations, e.g. new InlineStyle(['color' => 'red']).
  2. Explode a CSS string yourself into an array before constructing.
  3. Guard the value for null/empty before constructing.

Example fix

// before
$style = new InlineStyle('color:red; font-weight:bold');
// after
$style = new InlineStyle(['color' => 'red', 'font-weight' => 'bold']);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!is_iterable($value)) {
    throw new InvalidArgumentException('InlineStyle requires an iterable of style declarations');
}
$style = new InlineStyle($value);

Type guard

function isIterableForInlineStyle(mixed $v): bool {
    return is_iterable($v);
}

Try / catch

try {
    $style = new InlineStyle($value);
} catch (\Twig\Error\RuntimeError $e) {
    if (str_contains($e->getMessage(), 'InlineStyle can only be created')) {
        $style = new InlineStyle((array) ($value ?? []));
    } else {
        throw $e;
    }
}

Prevention

When it happens

Trigger: Calling new InlineStyle('color:red') (string), new InlineStyle(null), new InlineStyle(42), or passing a non-iterable expression to a style attribute in a template via the html-style style() helper.

Common situations: Passing a raw CSS string instead of an array of declarations, or a variable that is null because a lookup failed.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of twigphp/Twig@a414c3a491 (2026-09-13). Data as JSON: /api/errors/f237172a620be91f. Report an issue: GitHub.

Appendix: source

Thrown at extra/html-extra/HtmlAttr/InlineStyle.php:26

 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Twig\Extra\Html\HtmlAttr;

use Twig\Error\RuntimeError;

/**
 * @author Matthias Pigulla <mp@webfactory.de>
 */
final class InlineStyle implements MergeableInterface, AttributeValueInterface
{
    private readonly array $value;

    public function __construct(mixed $value)
    {
        if (!is_iterable($value)) {
            throw new RuntimeError('InlineStyle can only be created from iterable values.');
        }

        $this->value = [...$value];
    }

    public function mergeInto(mixed $previous): mixed
    {
        if ($previous instanceof self) {
            return new self([...$previous->value, ...$this->value]);
        }

        if (is_iterable($previous)) {
            return new self([...$previous, ...$this->value]);
        }

        throw new RuntimeError('Attributes using InlineStyle can only be merged with iterables or other InlineStyle instances.');
    }

View on GitHub (pinned to a414c3a491)