Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Developer654079525's avatar

The use of string modifiers in a template

Is it ok to inline the String's helper function such as

<tag>{{ Str::modifier_name($myvar) }}</tag>

directly in a template or should it be placed elsewhere?

0 likes
4 replies
Jsanwo64's avatar

It’s fine to inline small presentational helpers

{{ Str::limit($post->title, 50) }}
{{ ucfirst($user->name) }}
{{ number_format($price, 2) }}

So your example:

<tag>{{ Str::modifier_name($myvar) }}</tag>

is perfectly fine as long as it’s just cosmetic formatting. If it grows into something bigger or repeats everywhere, refactor it.

2 likes
Snapey's avatar

For complex views, I have started creating DTO to hold all the view data, and the passing the dto into the view. This allows for some functionality inside the dto. For instance, I can pass the user's timezone into the dto when it is constructed so that every timestamp it returns has the timezone applied.

Using Str inside the view is fine, as others have said, but move complex logic elsewhere.

1 like
martinbean's avatar

@developer654079525 Depends how often you’re going to be doing that.

If you’re constantly applying to the same transformation to a particular piece of data (e.g. a model attribute), then it may be better to add an accessor to the model instead. But given you don’t tell us anything about the problem you’re actually trying to solve here, it’s impossible to say whether this solution is “right” or not.

1 like

Please or to participate in this conversation.