Email templates in blade (Sage9)

Hi,

I am trying to send a mail from a controller, but I want the mail template to be blade.
Can’t figure out how to do this.

Any tips ?

You can use the template function found in app/helpers.php to render a Blade template found in your resources/views directory.

For example you could do something like:

$data = []; // Whatever data you want to pass to the view
$html = \App\template('emails/welcome', compact('data'));

This would render the Blade template found in resources/views/emails/welcome.blade.php

You might not need to include the full namespace reference to the function if you’re already inside the App namespace but this should work wherever you need.

3 Likes

Ohh i thing, no need for the compact function here as a data array is associative :wink:

True - that was just an example :slight_smile: It really depends on what data you need to pass and how you structure it.

If you have a bunch of separate variables, compact is nice because you can do compact('var1', 'var2', 'var3') etc and those variables will be available in the template with the same names. It’s just shorthand (see: http://php.net/compact)

I know this is super old, but I’m trying to do exactly this in Sage 10. Is there a similar function?

For rendering a view in a Sage 10 theme:

\Roots\view('path/to/view.blade.php')->render();

This will render the template and return the markup string.

view(...) also accepts a second argument for view data,
you can find an example of its usage in a Sage 10 default index.php:

<?php echo view(app('sage.view'), app('sage.data'))->render(); ?>
2 Likes