twigphp/Twig · error · Twig\Error\RuntimeError

Only iterable values can be appended to InlineStyle.

Error message

Only iterable values can be appended to InlineStyle.

What it means

InlineStyle::appendFrom adds new style declarations to this object; the new value must be iterable so it can be spread into the internal declaration list. Passing a non-iterable (string, null, scalar) raises this RuntimeError.

Solutions

  1. Append an array of declarations, e.g. appendFrom(['font-weight' => 'bold']).
  2. Convert the string to an array first (explode/explode-style parsing).
  3. Use mergeInto with an InlineStyle instance if appending another style object's value.

Example fix

// before
$style->appendFrom('font-weight:bold');
// after
$style->appendFrom(['font-weight' => 'bold']);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!is_iterable($newValue)) {
    throw new InvalidArgumentException('appendFrom requires an iterable of declarations');
}
$style->appendFrom($newValue);

Type guard

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

Try / catch

try {
    $style->appendFrom($newValue);
} catch (\Twig\Error\RuntimeError $e) {
    if (str_contains($e->getMessage(), 'can be appended to InlineStyle')) {
        // convert the string to declarations or skip
    } else {
        throw $e;
    }
}

Prevention

When it happens

Trigger: Calling $inlineStyle->appendFrom('font-weight:bold') with a raw string, appendFrom(null), or appendFrom(123).

Common situations: Appending a hand-written CSS snippet string instead of an array of declarations.

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/7c0e1dd68ed27854. Report an issue: GitHub.

Appendix: source

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

    }

    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.');
    }

    public function appendFrom(mixed $newValue): mixed
    {
        if (!is_iterable($newValue)) {
            throw new RuntimeError('Only iterable values can be appended to InlineStyle.');
        }

        return new self([...$this->value, ...$newValue]);
    }

    public function getValue(): ?string
    {
        $style = '';
        foreach ($this->value as $name => $value) {
            // `0`, `0.0` and `'0'` are valid CSS values, so only the values that carry
            // no declaration at all are skipped here
            if (null === $value || false === $value || true === $value || '' === $value || [] === $value) {
                continue;
            }
            if (is_numeric($name)) {
                $style .= trim($value, '; ').'; ';
            } else {
                $style .= $name.': '.$value.'; ';

View on GitHub (pinned to a414c3a491)