在 laravel blade 中创建类似于简码的 Wordpress

Creating Wordpress like shortcode in laravel blade

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

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

$dynamic_content_from_database 值来自所见即所得,我 used.This 是我工作过的管理层:

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

我认为更好的方法是在将 $dynamic_content_from_database 放入控制器中的 blade-template 之前准备好它。

在路线中

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 加载。