You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TL;DR: It'd be great if the |attr filter could stop logging a warning if the input string doesn't contain any HTML tags.
This is a thing that has been bugging me for a while... 😇
If the attr Twig filter is unable to find an HTML tag in the input string, Craft will log a WARNING that tends to pollute logs if one isn't careful:
2025-07-16 13:19:47 [web.WARNING] [craft\web\twig\Extension::attrFilter] Could not find an HTML tag in string: Hello there. I am the rendered output from foo.twig. {"memory":2717160}
I tend to write fairly defensive Twig, and typically will have a lot of "component" templates that won't render any HTML if some conditional isn't met. E.g. something like
In the example above, I don't really care if the shortcut menu component rendered or not (maybe the client deleted all the shortcut menu items 🤷).
But, in order to avoid having warnings from |attr polluting the logs, I have to make sure that anything I'm using |attr on, actually contains HTML before I use the filter 😩
In the example above, that could mean wrapping the include() statement in a conditional, checking if shortcutMenuItems is empty or not (which is the same thing happening inside the template, so otherwise redundant in this case):
But, creating a conditional like that isn't always feasible. In some cases I might instead have to check if the output is empty, by using the |spaceless filter (now deprecated! 😰) – which is a lot more awkward, e.g.
...it's a bit annoying. More to the point, it feels inconsistent with how Twig works generally, where filters are either very lenient about "invalid" input, or throws an actual exception.
For example, something like {{ 'Hamburgers are good'|round }} will not log a warning that the input is not a number – it will just return 0 🤷
On the flip side, doing something like {{ ['Hamburger!'] }} actually throws an exception (Array to string conversion), as Twig isn't able to reconcile the input to anything that can be rendered (I'm not suggesting the |attr filter starts doing that!).
To me, it feels like the |attr filter should behave like the |round example and not log a warning (especially not outside of devMode), in cases where it's used on something that doesn't actually contain HTML 🤷 This would allow the filter to be used a little less defensively and more in line with how other Twig filters work, without worrying about logs getting filled up by these non-errors.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
TL;DR: It'd be great if the
|attr
filter could stop logging a warning if the input string doesn't contain any HTML tags.This is a thing that has been bugging me for a while... 😇
If the
attr
Twig filter is unable to find an HTML tag in the input string, Craft will log a WARNING that tends to pollute logs if one isn't careful:I tend to write fairly defensive Twig, and typically will have a lot of "component" templates that won't render any HTML if some conditional isn't met. E.g. something like
I also like to use
|attr
a lot, often when including reusable Twig components, for example something likeIn the example above, I don't really care if the shortcut menu component rendered or not (maybe the client deleted all the shortcut menu items 🤷).
But, in order to avoid having warnings from
|attr
polluting the logs, I have to make sure that anything I'm using|attr
on, actually contains HTML before I use the filter 😩In the example above, that could mean wrapping the
include()
statement in a conditional, checking ifshortcutMenuItems
is empty or not (which is the same thing happening inside the template, so otherwise redundant in this case):Which... isn't too bad, but feels a bit clumsy.
But, creating a conditional like that isn't always feasible. In some cases I might instead have to check if the output is empty, by using the
|spaceless
filter (now deprecated! 😰) – which is a lot more awkward, e.g....it's a bit annoying. More to the point, it feels inconsistent with how Twig works generally, where filters are either very lenient about "invalid" input, or throws an actual exception.
For example, something like
{{ 'Hamburgers are good'|round }}
will not log a warning that the input is not a number – it will just return0
🤷On the flip side, doing something like
{{ ['Hamburger!'] }}
actually throws an exception (Array to string conversion
), as Twig isn't able to reconcile the input to anything that can be rendered (I'm not suggesting the|attr
filter starts doing that!).To me, it feels like the
|attr
filter should behave like the|round
example and not log a warning (especially not outside ofdevMode
), in cases where it's used on something that doesn't actually contain HTML 🤷 This would allow the filter to be used a little less defensively and more in line with how other Twig filters work, without worrying about logs getting filled up by these non-errors.Beta Was this translation helpful? Give feedback.
All reactions