0

I'm making an admin setting section of my laravel 5.2 app using the storage package from thetispro/laravel5-setting.

I'd like my admin users to be able to update email copy that get sent out to the user, but some of the emails include variables such as the users name. "Thanks for shopping with us, CUSTOMER NAME".

I can easily store the following in a setting, but when blade outputs it it just prints it out as a string instead of a variable. I've tried escaped and nonescaped the characters with {{}} and {{!! !!}. Here's what I have:

Email message an admin user can edit:

<h2>Hi, {{ $user->name }}</h2>
<p>Welcome to my web app</p>

In my view I have:

{!! Setting::get('emailuserinvite') !!}
<br /><br />
<!-- Testing both escaped and nonescaped versions -->
{{ Setting::get('emailuserinvite') }}

What blade renders is just:

echo "<h2>Hi, {{ $user->name }}</h2>
<p>Welcome to my web app</p>";

I was trying to make a custom blade directive that could close the echo, display the variable and open the echo back up, but that doesn't seem to be working correctly either.

// AppServiceProvider
Blade::directive('echobreak', function ($expression) {
  // echo "my string " . $var . " close string";
  $var = $expression;
  return "' . $var . '";
});

// Admin user settings
Hi @echobreak($user->name)
Welcome to my web app

Any advice would be appreciated! Thanks.

Update

I mocked up a simple test case using @abdou-tahiri's example but I'm still getting errors with the eval()'d code.

ErrorException in SettingController.php(26) : eval()'d code line 1:   Undefined variable: user

And here is my simple controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use Blade;

class SettingController extends Controller
{

    public function index() {
        $user = [
            "fname" => "Sam",
            "lname" => "yerkes"];
        $str = '{{ $user }}';
        return $this->bladeCompile($str, $user);
    }

    private function bladeCompile($value, array $args = [])
    {
        $generated = \Blade::compileString($value);
        ob_start() and extract($args, EXTR_SKIP);
        try {
            eval('?>'.$generated);
        } 
        catch (\Exception $e) {
            ob_get_clean(); throw $e;
        }
        $content = ob_get_clean();
        return $content;
    }

}
3
  • To clarify, yes, admin users should be able to edit the html. Commented Jun 12, 2016 at 14:31
  • 1
    You need to manually compile it with blade. Because it no longer goes through the engine the way you want it. Commented Jun 14, 2016 at 14:57
  • Why not taking this setting before generating the view and send it as a parameter to display on the blade side? Commented Jun 15, 2016 at 13:29

2 Answers 2

0
+100

You may need to compile the string using Blade , check this helper function :

function blade_compile($value, array $args = array())
{
    $generated = \Blade::compileString($value);

    ob_start() and extract($args, EXTR_SKIP);

    // We'll include the view contents for parsing within a catcher
    // so we can avoid any WSOD errors. If an exception occurs we
    // will throw it out to the exception handler.
    try
    {
        eval('?>'.$generated);
    }

    // If we caught an exception, we'll silently flush the output
    // buffer so that no partially rendered views get thrown out
    // to the client and confuse the user with junk.
    catch (\Exception $e)
    {
        ob_get_clean(); throw $e;
    }

    $content = ob_get_clean();

    return $content;
}

so in your view file :

{!! blade_compile(Setting::get('emailuserinvite'),compact('user')) !!}

Check this Is there any way to compile a blade template from a string?

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

4 Comments

This is great, I wasn't aware of the Blade::compileString. Where would be the best to add the blade_compile function without overwriting laravel core?
Depends on your app files structure, for me i use a create helper file inside my app directory, and i register him inside my composer file so it can be load automatically
Thanks for the continued help! I've mocked up a test on a fresh Laravel 5.2 project and updated the original post with the error I'm still getting.
Make sure you set the $user variable properly in your view file
0
<h2>Hi, $user->name</h2>
<p>Welcome to my web app</p>

Is this what you are trying to do?

<h2>Hi, {{$user->name}}</h2>
<p>Welcome to my web app</p>

3 Comments

Ahh sorry, I just missed those brackets in my example. I've updated my question to include them as they still will just echo out " Hi, {{ $user->name }}"
you sure your view file has the extension of .blade.php?
Definitely have that. "views/email/user/welcome.blade.php". Good idea though.

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.