SmootQ
SmootQ

Reputation: 2122

How to make php layouts in Laravel 5?

A couple of days ago, I decided to start using laravel for the next project, but I'm confused as I don't find the documentation very compelling and I'm still a laravel beginner .

So, I didn't find a solution for how to create a layout using PHP (and not built in blade templating engine).

How can I do that? What's the best way to organize layouts in a big project?

Thank you

Upvotes: 2

Views: 377

Answers (1)

Sulthan Allaudeen
Sulthan Allaudeen

Reputation: 11310

There are many methodologies that deal with handling templates.

Here are few,

1. Using a regular include or require

You can include the header.php , sidebar.php and footer.php and as many files that you prefer for each sector(It depends on the size of the template)

2. Using a common file and having classes inside it

Include a single file and call the classes to render each area

like

class Head {
    public function render($_page, $_data) {
        extract($_data);
        include($_page);
    }
}

3. Use a Templating Engine

You shall prefer few templating engine like smart, raintpl etc., (I guess you don't prefer it ;) )

4. Acquiring by inc

You can include as suggested here

<html>
      <head>
         <title><?=$this->title</title>
      </head>
      <body>Hey <?=$this->name?></body>
</html>

And the php area would be

$view = new Template();
$view->title="Hello World app";
$view->properties['name'] = "Jude";
echo $view->render('hello.inc');

5. By having template segments in db

Believe me, I saw many good sites which stores the template in the database and it will be rendered each time. It might look like strange idea, but even i tried it for one of my project.

Conclusion :

But if i use Laravel, for sure i will prefer the Blading Tempalte Engine and I recommend you the same.

Update :

Few benefits of Using Blade Templates

1. Easy Setting of attributes

Set the attributes on the go

<title>App Name - @yield('title')</title>

2. Easy yielding

   <body>
        @section('sidebar')
            This is the master sidebar.
        @show

        <div class="container">
            @yield('content')
        </div>
    </body>

3. Simple echoing

Like this

Hello, {{ $name }}

4. Easy Condition

Like this

{{ isset($name) ? $name : 'Default' }}

5. Never Escape

Like this

Hello, {!! $name !!}.

6. Beautiful If Statements

I prefer this way to make my code more beautiful

@if (count($records) === 1)
    I have one record!
@elseif (count($records) > 1)
    I have multiple records!
@else
    I don't have any records!
@endif

7. Checking Authentication

The simplest way to check the authentication

@unless (Auth::check())
    You are not signed in.
@endunless

8. Easy For Loop

How this for loop looks like

@for ($i = 0; $i < 10; $i++)
    The current value is {{ $i }}
@endfor

9. Awesome foreach statement

Splitting the key and value can't be more easy than this

@foreach ($users as $user)
    <p>This is user {{ $user->id }}</p>
@endforeach

10. Include the files

How about include file like this

@include('view.name')

11. Passing parameters to views

Can Pass this array to your view

@include('view.name', ['some' => 'data'])

Source : Laravel Templates

Upvotes: 4

Related Questions