Support the ongoing development of Laravel.io →
Requests Views Blade
Last updated 2 years ago.
0

So there's this method available on the View facade that does something pretty neat.

This method is called renderSections()

Accessing this method goes a little bit like this:

// Make view, with variable for example
$view = View::make('path.to.view')->with('name', 'JohnDoe');
return $view->renderSections()

Before we take a look at what this returns us, we must first look at our application layout.

If we have this for example:

<html>
   <body>
   <div>@yield('content')</div>
  @section('sidebar')

  @show
  </body>
</html>

Our renderSections() method will return us an array of the content we filled these sections with, for example:

array (size=2)
  'content' => string ' <div class='left well pull-left'>
      <h2>Sheikh Heera</h2>
      <p>Contact: 0123456789</p>
   </div>
' (length=100)
  'sidebar' => string ' <div class='right well pull-right'>
      <h1>Sidebar</h1>
      <u>
         <li>ListOne</li>
         <li>ListTwo</li>
         <li>ListThree</li>
      </ul>
   </div>
' (length=148)

So, we can effectively build our html string for use with ajax like so :

if(Request::ajax()) {
    $view = View::make('card');
    $view = $view->renderSections();
    return join($view, '');
}

0

This seem awesome, I'll definitely take a look at renderSection. Thanks a lot.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

poudigne poudigne Joined 31 Jan 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.