问题
I have a mail template (in laravel blade) that is need to be dynamic (so it should be saved in database i think). This is the registration_complete.blade.php:
<html>
<body>
{{$dynamic_content_from_database}}
</body>
</html>
And the $dynamic_content_from_database value came from the WYSIWYG i used.This is the management i worked for :
I need to find a way that admin user can manage the content of the template with variable inside or shortcode. Just like in WordPress they used short code to put the dynamic value in the content. I tried echoing the $dynamic_content_from_database. But it was just echoing the [username]
as string. I tried to search trough web but i get no luck. By the way, i used laravel 4.2.This form is sent after successful registration.
回答1:
I think the better way to do is to prepare $dynamic_content_from_database BEFORE you put it in blade-template in your controller.
in route
Route::post('register', ['as'=>'register.post', 'uses'=> 'path\to\register\form\namespace@register']
in controller (easy, but not the best way):
public function replacement($string, array $placeholders)
{
$resultString = $string;
foreach($placeholders as $key => $value) {
$resultString = str_replace('[' . $key . ']' , trim($value), $resultString, $i);
}
return $resultString;
}
public function register() {
$data = Input::all();
//... Dont forget about validation
//loading template from database example
$template = Template::where('name', 'register.success')->firstOrFail();
$content = $this->replacement($template['text'], $data);
View::make('register.success', ['content' => $content]); //or send mail with $content as you want
}
For better solution you might want to create TemplateService, enable it in config and load through App.
来源:https://stackoverflow.com/questions/31691476/creating-wordpress-like-shortcode-in-laravel-blade