Creating Wordpress like shortcode in laravel blade

不羁的心 提交于 2019-12-11 00:25:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!