【问题标题】:Laravel3: Loading a content inside a content dynamicallyLaravel3:动态加载内容内的内容
【发布时间】:2013-03-29 18:28:42
【问题描述】:

我有一个表格。当用户提交表单并出现错误时,我会这样显示:

注册控制器

return View::make('theme-admin.user_add')->with('error_msg', validation->errors->first());

register.blade.php

@if($error_msg !== null)
  <div class="alert red hideit max">
     <div class="left">
            <span class="red-icon"></span>
            <span class="alert-text">{{ $error_msg }}</span> <-- Error message is visible here.
     </div>
     <div class="right">
          <a class="close-red"></a>
     </div>
 </div>
@endif

//
    Actual HTML Form
//

但是,我想将该错误 div 移动到刀片文件中。 (error.blade.php),我想在出错的时候带参数调用。

它看起来像这样。

新的 register.blade.php

{{ MESSAGE_CONTENT }} //This should be replaced with error.blade.php dynamically
//
    Actual HTML Form
//

MESSAGE_CONTENT 将通过 error.blade.php 包含

error.blade.php

<div class="alert red hideit max">
     <div class="left">
            <span class="red-icon"></span>
            <span class="alert-text">{{ $message }}</span> <-- Error message is visible here.
     </div>
     <div class="right">
          <a class="close-red"></a>
     </div>
 </div>

假设表单失败并且我遇到了一些错误。我将加载 error.blade.php,因此消息将获得 RED 背景等。

类似的东西;

return View::make('theme-admin.user_add')->with(message_content', (Load error.blade.php here))->with('message', $validation->errors->first();

如果表单成功,我将在消息区域加载success.blade.php,消息将以绿色背景显示。

 return View::make('theme-admin.user_add')->with(message_content', (Load success.blade.php here))->with('message', 'You successfully registered');

你可能明白了。

我该怎么做?

附言。图片示例:http://i.imgur.com/QExAiuA.png

【问题讨论】:

    标签: php laravel templating laravel-3 blade


    【解决方案1】:

    我自己找到了解决方案。我需要嵌套视图功能。

    $view = View::make('home');
    $view->nest('content', 'orders', array('orders' => $orders));
    

    有关更多信息,请参阅 Laravel 文档。

    【讨论】:

    • 很好,你找到了你需要的东西,尽管我仍然建议你不要创建用于显示错误/成功消息的视图 - 而是像我的示例中那样重定向。 Nest 只是 with() 的快捷方式,其中第二个参数用于创建 View 实例。因此,在您已经有一个视图实例的示例中,您不能在其中嵌套一个新视图。见:github.com/laravel/laravel/issues/1183
    • 我还没试过用巢。如果它没有在视图中加载视图并解析子视图的变量,我将使用您的解决方案。它比您的解决方案好一步,也就是说,我将在我的控制器上拥有一个完全模块化的视图控件。而不是在每个视图上打开添加包含(if / else if / else 等),我可以让一个变量直接通过控制器监听视图。没有更多的如果或其他。如果验证失败,我只会嵌套->错误。如果它通过了,我就会嵌套->成功。两者都将通过控制器处理。不过,我非常感谢您的回复。很有启发性
    • 酷,我猜每个人都有自己的喜好。 :) 可能可以破解 if/else 并将刀片模板作为 with() 或 with_errors() 的第二个参数 - 你至少可以将内联 html 放在这里,所以应该可以包含一个我猜也是刀片模板。即:->with_errors($validation, static::make('error', $data));不过,我仍然更喜欢 if/else,它非常清楚,我认为它们是“表示逻辑”,它比您的路线更好地放在视图中。 :)
    【解决方案2】:

    您应该在为 GET 定义的路由中创建视图 (View::make()),然后在 POST 路由中处理表单输入:

    //routes.php
    Route::get('admin/adduser', array('as' => 'adduser', 'do' => function()
    {
        return View::make('theme-admin.user_add');
    }));
    
    //route for handling form input
    Route::post('register', array('before' => 'csrf', function()
     {
         $rules = array(
             //your vailidation rules here..
         );
    
         $validation = Validator::make(Input::all(), $rules);
    
         if ($validation->fails())
         {
             //re-populate form if invalid values, so add flashing input by with_input()
             //also pass errors by using with_errors
             return Redirect::to('adduser')->with_input()->with_errors($validation);
         }
         else {
             //use the named route we defined in Route:get above
             return Redirect:to('adduser')->with('message', 'Successfully added the new user!');
         }
     }));
    

    无需创建新视图来显示错误和成功消息。如果您想将成功和错误分开到他们自己的刀片模板中,那么您可以在 adduser.blade.php 中使用以下内容:

    //adduser.blade.php
    @if($errors->has())
        @include('errormsg'); //use {{ $errors->first() }} or @foreach($errors->all() as $message) to print error message(s)
    @else
        @include('success'); //use {{ $message }} to print success
    @endif
    

    但是,您也可以在视图中使用部分,而是将成功和错误消息放在同一个视图中:

    //feedback.blade.php
    @section('error')
        <em class="error">{{ $errors->first() }}</em>
    @endsection
    
    @section('success')
        <em class="success">{{ $message }}</em>
    @endsection
    
    //adduser.blade.php
    @include('feedback');
    
    @if($errors->has())
        @yield('error');
    @else
        @yield('success');
    @endif
    

    希望对您有所帮助。

    【讨论】:

    • 在某些情况下它不会被包含,所以我不能使用@include。
    • 问题是,如果我使用 $error_msg !== null,我也必须使用 $success_msg !== null。这就是我现在正在使用的。 (eburhan.com/wp-content/ekler/81/demo) @include($type) 之类的东西可能会起作用,但这远非我要问的。
    • 我用一个关于如何处理这个问题的完整示例更新了我的答案。希望这能让事情更清楚。
    【解决方案3】:

    一个干净的解决方案可能是有一个带有类型和消息的简单警报对象。

    //in controller
    $alert->type = 'error'; // or 'success'
    $alert->class = 'red'; // or 'green'
    $alert->msg = $validation->errors->first(); // or 'You successfully registered'
     return View::make('theme-admin.user_add')->with('alert', $alert);
    
    //register.blade.php
    @include('alert')
    
    //Actual HTML Form
    
    //alert.blade.php
    @if(isset($alert))
      <div class="alert {{$alert->class}} hideit max">
         <div class="left">
                <span class="red-icon"></span>
                <span class="alert-text">{{ $alert->msg }}</span>
         </div>
         <div class="right">
              <a class="close-{{$alert->class}}"></a>
         </div>
     </div>
    @endif
    

    【讨论】:

    • 不是我正在寻找的确切解决方案。不过,谢谢。
    猜你喜欢
    • 2020-10-07
    • 2011-06-14
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多