2

There is a new feature in Laravel 9 that allows inline blade template rending. Link: https://laravel.com/docs/9.x/blade#rendering-inline-blade-templates

Let’s say I have a value stored in the database (as the body of the email) I want to render from database as email.

E.g <p> Dear {{$first_name}} {{$last_name}} </p>

I would use this;

Blade::render($body, [‘first_name’ => $first_name, ‘last_name’ => $last_name]);

With this I’m able to output something like;

Dear John Doe

In the body of the email sent.

However, what if I want to use <p> Dear {{first_name}} {{last_name}} </p>

in my email template stored in the database (without the dollar sign, making it a constant rather than a variable). How can I render this constant using the Blade::render()?

5
  • You most likely can't. Commented Jun 11, 2022 at 19:28
  • 1
    Does this answer your question? How to get Blade template view as a raw HTML string? Commented Jun 11, 2022 at 19:50
  • Besides, "Class constants MUST be declared in all upper case with underscore separators". Commented Jun 11, 2022 at 19:52
  • you would have to define those constants before calling render as {{ ... }} is replaced with <?php echo e(...); ?> and then that is evaluated Commented Jun 11, 2022 at 20:41
  • Why is this even relevant? Thinking there is another solution about the reasoning to avoiding the $first_name approach. Commented Jun 11, 2022 at 21:57

1 Answer 1

2

I think you will have to manipulate the templates, before sending output to blade.

Laravel has many helpers to do things like that.

use Illuminate\Support\Str;
 
$string = '{{first_name}} {{last_name}}';
 
$replaced = Str::replace('{{', '{{$', $string);

// in $replaced you'll have '{{$first_name}} {{$last_name}}'

Above code will do the job.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.