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

Tarasovych's avatar

Passing variable value in blade template

I have some html stored in db:

Hello, $user! ...

I retreive this html, put it to $text and pass to blade template. In my template I have this:

@include('other.blade', ['text' => $text])

But exactly in my $text I want to replace $user by Auth::user()->name. So I have this working example now:

@include('other.blade', ['text' => $text, 'user' => Auth::user()->name])

and in other.blade:

{!! preg_replace('/$(user)/', $user, $text) !!}

But I don't like my sollution. Even if I want to pass more variables to $text, my current code won't work. Any better sollutions and advices appreciated! Thanks!

0 likes
8 replies
Tarasovych's avatar

I guess I can make a new separate blade and go from {!! preg_replace('/$(user)/', $user, $text) !!} to @include('text.blade', ['user' => Auth::user()->name]). Maybe this sollution'd be better than my current...

Cronix's avatar
Cronix
Best Answer
Level 67

The simplest solution would be to not store your "template" in the database. Make a regular blade view for it. Nothing else would need to be done.

Hello, {{ Auth::user()->name }}, how are you?
1 like
Tarasovych's avatar

@Cronix I have near 500 symbols in my stored "template". I thought it's bad like to "hardcode" such amount of text right in blade. If it's ok to have a separate view, I think I'll go this way. Thanks!

Cronix's avatar

I don't think it's bad practice. That's how views are meant to be used. If you have that many "placeholders" in your text, and you're using preg_replace(), which is an expensive operation in and of itself, I think that would be a bad practice. Look at the problems you are running into with just replacing 2 variables...now do it for 500?

You could just have many "subviews" of templates, with a main template that includes them all in order. The "main" template could just look something like:

Hello, {{ Auth::user()->name }},

@if (someVariableExists)
    @include(someTemplate, [someVariable => $someVariable])
@endif 

We are very happy that you subscribed.
@if (someOtherVariableExists)
    @include(someOtherTemplate, [someOtherVariable => $someOtherVariable])
@endif 

@if (Auth::user()->something === 'complete')
    @include(blah)
@endif

Sincerely,
Company

Where someVariable(s) is coming from your controller. Maybe a more detailed explanation would help. I've never needed to use 500 placeholders for views.

1 like
click's avatar

And besides all of the downsides what cronix mentioned. Storing full html is also not future proof. What if you want to change the greeting from Hello John to a more formal Dear John. Or what if you want to greet Hawaiian visitors with Aloha John?

1 like
Tarasovych's avatar

@m-rk there is also a problem I can't store such string as {{ $user }} in db because of I'm using Vue component on that page. {{ $user }}could ease my tries.

click's avatar

Sorry I don't understand your question

Tarasovych's avatar

@m-rk I meant I can't store such template in database:

Hello, {{ $user }}! ...

because of I'm using Vue component on the page where I want to show this database stored text and Vue returns an error.

{{ $user }} allows to avoid using preg_replace() to pass variable data.

Please or to participate in this conversation.