duanrang9348 2018-06-14 05:56
浏览 115
已采纳

Laravel 5在动态字段上嵌套花括号

I want to create dynamic field on my controller and pass variable to blade. I have controller like this

public function create()
{
   $AdditionalField=AdditionalFieldController::generateHTML(1);
   return view("addcontact")->with(['AdditionalField'=$AdditionalField]);
}

output of print_r($AdditionalField) is:

<div class="col-lg-1 col-md-12 col-sm-12 text-right">
    <label class="text-gray-dark"  for="address_2">address_2</label>
</div>
<div class="col-lg-3 col-md-8 col-sm-12">
     <input class="form-control " id="address_2" value="{{isset($editContact['address_2'])?$editContact['address_2']:''}}"  name="address_2">
</div>

and on my blade I use this code for show fields.

 {!!  $AdditionalField !!}

Output is

mypic

but I want to show me this on blade

mypic

Updated

  public static function generateHTML($moduleId)
{       
   $AdditionalFieldKeys = AdditionalFieldKey::where("module_id", $moduleId)->get();
    $html = '';
    foreach ($AdditionalFieldKeys as $additionalFieldKey) {
        $type = AdditionalFieldController::getInputType($additionalFieldKey->additional_field_key_name);
        $fieldName = AdditionalFieldController::getInputName($additionalFieldKey->additional_field_key_name);
        $file = AdditionalFieldController::getTemplate($type);
        $variable = AdditionalFieldController::getVariables($fieldName,$this->model_name);
        $html .= AdditionalFieldController::fillTemplate($variable, $file);
    }
    return $html;
}
    public static function getTemplate($type)
{
    $file = base_path('modules/AdditionalField/view/' . $type . '.stub');
    return file_get_contents($file);
}
  • 写回答

1条回答 默认 最新

  • duanhuihui2705 2018-06-14 06:50
    关注

    As I said in my comment, you need to use something like the StringBladeCompiler.

    To install this component you have to execute two steps (which are also documented in the readme of StringBladeCompiler):

    1. Run composer require "wpb/string-blade-compiler" in your project directory
    2. In config/app.php replace the line Illuminate\View\ViewServiceProvider::class with Wpb\String_Blade_Compiler\ViewServiceProvider::class, like that:

      //Illuminate\View\ViewServiceProvider::class,
      Wpb\String_Blade_Compiler\ViewServiceProvider::class,
      

    After that, you can use string based templates. So in your controller you have to use two view calls. In the end it would look like that:

    public function create()
    {
        $AdditionalField=AdditionalFieldController::generateHTML(1);
        $editContact['address_2']='ali';
    
        return view("addcontact")->with([
            'AdditionalField'=>view(['template' => $AdditionalField])->with(['editContact' => $editContact])
        ]);
    }
    

    If you have to supply any variables to the "inner" view you can add a with just like with the outer view call.

    The other solution would be to just refactor your approach and not generate a string-template but use stuff like "includes".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?