在Laravel Blade中像shortcode一样创建Wordpress [英] Creating Wordpress like shortcode in laravel blade

查看:61
本文介绍了在Laravel Blade中像shortcode一样创建Wordpress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要动态的邮件模板(在laravel刀片中)(所以我认为应该将其保存在数据库中).这是registration_complete.blade.php:

      <html>
       <body>
         {{$dynamic_content_from_database}}
        </body>
      </html>

$ dynamic_content_from_database的值来自我使用的所见即所得,这是我所从事的管理工作:

我需要找到一种方法,让管理员用户可以使用内部变量或简码来管理模板的内容.就像在WordPress中一样,他们使用短代码在内容中添加了动态值.我尝试回显$ dynamic_content_from_database.但这只是将[username]作为字符串回显.我试图搜索低谷网站,但没有运气.顺便说一句,我使用的是laravel 4.2.此表格是在成功注册后发送的.

我认为更好的方法是在将$ dynamic_content_from_database放入控制器的刀片模板之前,准备好$ dynamic_content_from_database.

在途中

Route::post('register', ['as'=>'register.post', 'uses'=> 'path\to\register\form\namespace@register']

在控制器中(简单但不是最好的方法):

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
}

为获得更好的解决方案,您可能需要创建TemplateService,在配置中启用它并通过App加载.

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.

解决方案

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.

这篇关于在Laravel Blade中像shortcode一样创建Wordpress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆