Search code examples
phplaraveltemplatinglaravel-3laravel-blade

Laravel3: Loading a content inside a content dynamically


I have a form. When user submits the form and gets error, I show it like this:

Register Controller

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
//

However, I want to move that error div into a blade file. (error.blade.php) and I want to call it with parameters when there is an error.

It will look like this.

NEW register.blade.php

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

MESSAGE_CONTENT will be included via 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>

Let's say form failed and I got some errors. I will load error.blade.php so messages will get RED background etc.

Something like this;

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

If the form succeeds, I'll just load success.blade.php in messages area and messages will look with GREEN background.

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

You probably got the logic.

How can I do this?

Ps. Image example: https://i.sstatic.net/ipzKY.png


Solution

  • I found the solution myself. I needed Nested Views feature.

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

    Refer to Laravel documentation for more information.