twigphp/Twig · error · Twig\Error\RuntimeError
Unknown attribute type
Error message
Unknown attribute type "%s" The only supported types are "sst", "cst" and "style".
What it means
The html_attr() helper supports special attribute-value types via the htmlAttrType() converter: 'sst' (space-separated token list), 'cst' (comma-separated token list) and 'style' (inline CSS). Any other type string hits the match() default arm and throws this RuntimeError.
Solutions
- Use only 'sst', 'cst' or 'style' as the type argument.
- Fix typos ('styles' -> 'style', 'csv' -> 'cst').
- Validate/config-whitelist the type value before passing it through.
- If you need a new type, extend with your own AttributeValueInterface implementation instead of a new string.
Example fix
// before htmlAttrType($value, 'csv') // after htmlAttrType($value, 'cst')
Defensive patterns
Strategy: validation
Validate before calling
if (!in_array($type, ['sst','cst','style'], true)) { throw new \InvalidArgumentException("Unsupported attr type $type"); } Type guard
function isSupportedAttrType(string $t): bool { return in_array($t, ['sst','cst','style'], true); } Try / catch
try { $v = HtmlExtension::htmlAttrType($value, $type); } catch (Twig\Error\RuntimeError $e) { $v = new SeparatedTokenList($value, ' '); } Prevention
- Keep the three allowed type strings in a shared constant
- Whitelist user/config-supplied type values
- grep templates for htmlAttrType usages when refactoring
When it happens
Trigger: Calling HtmlExtension::htmlAttrType($value, $type) with $type not in ['sst','cst','style'], e.g. htmlAttrType($value, 'class') or a typo like 'styles'.
Common situations: Typo in the type string in a Twig template; copying an option name from another helper; passing user-controlled type config that was never validated.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- Only empty strings may be passed as string arguments to…
- Cannot merge incompatible values for key
- The " " attribute value cannot be JSON encoded.
- The " " attribute value should be a scalar, an iterable, or…
- Enum " " does not exist.
AI-assisted analysis of twigphp/Twig@a414c3a491 (2026-09-13).
Data as JSON: /api/errors/76b9c52f5d644c65.
Report an issue: GitHub.
Appendix: source
Thrown at extra/html-extra/HtmlExtension.php:145
* @param array<string, array<string, string|array<string>>> $variants
* @param array<array<string, string|array<string>>> $compoundVariants
* @param array<string, string> $defaultVariant
*
* @internal
*/
public static function htmlCva(array|string $base = [], array $variants = [], array $compoundVariants = [], array $defaultVariant = []): Cva
{
return new Cva($base, $variants, $compoundVariants, $defaultVariant);
}
/** @internal */
public static function htmlAttrType(mixed $value, string $type = 'sst'): AttributeValueInterface
{
return match ($type) {
'sst' => new SeparatedTokenList($value, ' '),
'cst' => new SeparatedTokenList($value, ', '),
'style' => new InlineStyle($value),
default => throw new RuntimeError(\sprintf('Unknown attribute type "%s" The only supported types are "sst", "cst" and "style".', $type)),
};
}
/** @internal */
public static function htmlAttrMerge(iterable|string|false|null ...$arrays): array
{
$result = [];
foreach ($arrays as $array) {
if (!$array) {
continue;
}
if (\is_string($array)) {
throw new RuntimeError('Only empty strings may be passed as string arguments to html_attr_merge. This is to support the implicit else clause for ternary operators.');
}
foreach ($array as $key => $value) {View on GitHub (pinned to a414c3a491)