twigphp/Twig · error · Twig\Error\RuntimeError
Attributes using InlineStyle can only be merged with…
Error message
Attributes using InlineStyle can only be merged with iterables or other InlineStyle instances.
What it means
InlineStyle::mergeInto merges this style object into a previous attribute value. A previous value that is neither iterable nor an InlineStyle instance cannot be combined, so a RuntimeError is thrown. This guards against overwriting a rendered string attribute with incompatible types.
Solutions
- Ensure the previous value is an InlineStyle instance or an iterable of declarations.
- Convert a plain CSS string into an iterable (explode on ';') before merging.
- Initialize the attribute value as an empty InlineStyle instead of null/string.
Example fix
// before
$merged = $style->mergeInto('color:red');
// after
$merged = $style->mergeInto(new InlineStyle(['color' => 'red'])); Defensive patterns
Strategy: type-guard
Validate before calling
if (!$previous instanceof InlineStyle && !is_iterable($previous)) {
throw new InvalidArgumentException('Cannot merge InlineStyle into non-iterable previous value');
}
$merged = $style->mergeInto($previous); Type guard
function canMergeInlineStyle(mixed $previous): bool {
return $previous instanceof InlineStyle || is_iterable($previous);
} Try / catch
try {
$merged = $style->mergeInto($previous);
} catch (\Twig\Error\RuntimeError $e) {
if (str_contains($e->getMessage(), 'can only be merged')) {
$merged = $style->mergeInto(new InlineStyle(is_iterable($previous) ? [...$previous] : []));
} else {
throw $e;
}
} Prevention
- Keep attribute values as InlineStyle/iterables until final rendering
- Never merge into already-rendered HTML strings
- Initialize previous values as empty InlineStyle rather than null/string
When it happens
Trigger: Calling $inlineStyle->mergeInto('color:red') with a plain string, mergeInto(null), or merging into any scalar/object that is not InlineStyle or iterable.
Common situations: Merging into an attribute value that a template already rendered as a plain string, or into a null from an unset variable.
Understand the failure class
Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.
Related errors
- InlineStyle can only be created from iterable values.
- Only iterable values can be appended to InlineStyle.
- SeparatedTokenList can only be constructed from iterable or…
- SeparatedTokenList can only be merged with iterables or…
- Only iterable values can be appended to SeparatedTokenList.
AI-assisted analysis of twigphp/Twig@a414c3a491 (2026-09-13).
Data as JSON: /api/errors/a990ab7c86b8ac2e.
Report an issue: GitHub.
Appendix: source
Thrown at extra/html-extra/HtmlAttr/InlineStyle.php:42
{
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.');
}
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) {View on GitHub (pinned to a414c3a491)