【问题标题】:Creating Wordpress like shortcode in laravel blade在 laravel Blade 中创建类似 Wordpress 的简码
【发布时间】:2015-07-29 15:27:36
【问题描述】:

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

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

$dynamic_content_from_database 值来自我使用的 WYSIWYG。这是我工作的管理层:

我需要找到一种方法,管理员用户可以使用内部变量或简码管理模板的内容。就像在 WordPress 中一样,他们使用短代码将动态值放入内容中。我尝试回显 $dynamic_content_from_database。但它只是将[username] 作为字符串回显。我试图通过网络搜索,但我没有运气。顺便说一下,我用的是laravel 4.2,这个表单是注册成功后发送的。

【问题讨论】:

标签: php wordpress laravel laravel-4 shortcode


【解决方案1】:

我认为更好的方法是在将 $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 加载。

【讨论】:

  • 你节省了我的时间!它工作得很好,但我有兴趣在以后以最好的方式做到这一点。我不知道 TemplateService。但我会试着找出来。你能给我们举个例子吗?至少我对此有一个快速的解决方案。再次感谢您。
  • 当我说 TemplateService 时只提到 ServiceProvider 逻辑,请阅读服务提供者 there 你必须在 solo-class 中包含模板函数,它只做一件事:template-things 之后你可以通过 $template = App::make('YourService') 在您的应用程序的任何地方使用此服务,并且模板程序将单独使用和更改模板程序的逻辑
猜你喜欢
  • 2018-02-05
  • 1970-01-01
  • 2017-05-17
  • 2021-04-08
  • 1970-01-01
  • 2021-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多