Skip to content

Commit

Permalink
replace all tabs with spaces (PSR-2)
Browse files Browse the repository at this point in the history
  • Loading branch information
= Geo = committed Jul 2, 2015
1 parent ffe5123 commit 75651ca
Show file tree
Hide file tree
Showing 47 changed files with 5,302 additions and 5,302 deletions.
2 changes: 1 addition & 1 deletion authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ You will need to provide an HTML view for the password reset request form. This
{!! csrf_field() !!}

<div>
Email
Email
<input type="email" name="email" value="{{ old('email') }}">
</div>

Expand Down
248 changes: 124 additions & 124 deletions billing.md

Large diffs are not rendered by default.

214 changes: 107 additions & 107 deletions blade.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

- [Introduction](#introduction)
- [Template Inheritance](#template-inheritance)
- [Defining A Layout](#defining-a-layout)
- [Extending A Layout](#extending-a-layout)
- [Defining A Layout](#defining-a-layout)
- [Extending A Layout](#extending-a-layout)
- [Displaying Data](#displaying-data)
- [Control Structures](#control-structures)
- [Service Injection](#service-injection)
Expand All @@ -22,22 +22,22 @@ Blade is the simple, yet powerful templating engine provided with Laravel. Unlik

Two of the primary benefits of using Blade are _template inheritance_ and _sections_. To get started, let's take a look at a simple example. First, we will examine a "master" page layout. Since most web applications maintain the same general layout across various pages, it's convenient to define this layout as a single Blade view:

<!-- Stored in resources/views/layouts/master.blade.php -->
<!-- Stored in resources/views/layouts/master.blade.php -->

<html>
<head>
<title>App Name - @yield('title')</title>
</head>
<body>
@section('sidebar')
This is the master sidebar.
@show
<html>
<head>
<title>App Name - @yield('title')</title>
</head>
<body>
@section('sidebar')
This is the master sidebar.
@show

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

As you can see, this file contains typical HTML mark-up. However, take note of the `@section` and `@yield` directives. The `@section` directive, as the name implies, defines a section of content, while the `@yield` directive is used to display the contents of a given section.

Expand All @@ -48,76 +48,76 @@ Now that we have defined a layout for our application, let's define a child page

When defining a child page, you may use the Blade `@extends` directive to specify which layout the child page should "inherit". Views which `@extends` a Blade layout may inject content into the layout's sections using `@section` directives. Remember, as seen in the example above, the contents of these sections will be displayed in the layout using `@yield`:

<!-- Stored in resources/views/layouts/child.blade.php -->
<!-- Stored in resources/views/layouts/child.blade.php -->

@extends('layouts.master')
@extends('layouts.master')

@section('title', 'Page Title')
@section('title', 'Page Title')

@section('sidebar')
@@parent
@section('sidebar')
@@parent

<p>This is appended to the master sidebar.</p>
@endsection
<p>This is appended to the master sidebar.</p>
@endsection

@section('content')
<p>This is my body content.</p>
@endsection
@section('content')
<p>This is my body content.</p>
@endsection

In this example, the `sidebar` section is utilizing the `@@parent` directive to append (rather than overwriting) content to the layout's sidebar. The `@@parent` directive will be replaced by the content of the layout when the view is rendered.

Of course, just like plain PHP views, Blade views may be returned from routes using the global `view` helper function:

Route::get('blade', function () {
return view('child');
});
Route::get('blade', function () {
return view('child');
});

<a name="displaying-data"></a>
## Displaying Data

You may display data passed to your Blade views by wrapping the variable in "curly" braces. For example, given the following route:

Route::get('greeting', function () {
return view('welcome', ['name' => 'Samantha']);
});
Route::get('greeting', function () {
return view('welcome', ['name' => 'Samantha']);
});

You may display the contents of the `name` variable like so:

Hello, {{ $name }}.
Hello, {{ $name }}.

Of course, you are not limited to displaying the contents of the variables passed to the view. You may also echo the results of any PHP function. In fact, you can put any PHP code you wish inside of a Blade echo statement:

The current UNIX timestamp is {{ time() }}.
The current UNIX timestamp is {{ time() }}.

> **Note:** Blade `{{ }}` statements are automatically sent through PHP's `htmlentities` function to prevent XSS attacks.
#### Blade & JavaScript Frameworks

Since many JavaScript frameworks also use "curly" braces to indicate a given expression should be displayed in the browser, you may use the `@` symbol to inform the Blade rendering engine an expression should remain untouched. For example:

<h1>Laravel</h1>
<h1>Laravel</h1>

Hello, @{{ name }}.
Hello, @{{ name }}.

In this example, the `@` symbol will be removed by Blade; however, `{{ name }}` expression will remain untouched by the Blade engine, allowing it to instead be rendered by your JavaScript framework.

#### Echoing Data If It Exists

Sometimes you may wish to echo a variable, but you aren't sure if the variable has been set. We can express this in verbose PHP code like so:

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

However, instead of writing a ternary statement, Blade provides you with the following convenient short-cut:

{{ $name or 'Default' }}
{{ $name or 'Default' }}

In this example, if the `$name` variable exists, its value will be displayed. However, if it does not exist, the word `Default` will be displayed.

#### Displaying Unescaped Data

By default, Blade `{{ }}` statements are automatically sent through PHP's `htmlentities` function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax:

Hello, {!! $name !!}.
Hello, {!! $name !!}.

> **Note:** Be very careful when echoing content that is supplied by users of your application. Always use the double curly brace syntax to escape any HTML entities in the content.
Expand All @@ -130,74 +130,74 @@ In addition to template inheritance and displaying data, Blade also provides con

You may construct `if` statements using the `@if`, `@elseif`, `@else`, and `@endif` directives. These directives function identically to their PHP counterparts:

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

For convenience, Blade also provides an `@unless` directive:

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

#### Loops

In addition to conditional statements, Blade provides simple directives for working with PHP's supported loop structures. Again, each of these directives functions identically to their PHP counterparts:

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

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

@forelse ($users as $user)
<li>{{ $user->name }}</li>
@empty
<p>No users</p>
@endforelse
@forelse ($users as $user)
<li>{{ $user->name }}</li>
@empty
<p>No users</p>
@endforelse

@while (true)
<p>I'm looping forever.</p>
@endwhile
@while (true)
<p>I'm looping forever.</p>
@endwhile

#### Including Sub-Views

Blade's `@include` directive, allows you to easily include a Blade view from within an existing view. All variables that are available to the parent view will be made available to the included view:

<div>
@include('shared.errors')
<div>
@include('shared.errors')

<form>
<!-- Form Contents -->
</form>
</div>
<form>
<!-- Form Contents -->
</form>
</div>

Even though the included view will inherit all data available in the parent view, you may also pass an array of extra data to the included view:

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

#### Comments

Blade also allows you to define comments in your views. However, unlike HTML comments, Blade comments are not included in the HTML returned by your application:

{{-- This comment will not be present in the rendered HTML --}}
{{-- This comment will not be present in the rendered HTML --}}

<a name="service-injection"></a>
## Service Injection

The `@inject` directive may be used to retrieve a service from the Laravel [service container](/docs/{{version}}/container). The first argument passed to `@inject` is the name of the variable the service will be placed into, while the second argument is the class / interface name of the service you wish to resolve:

@inject('metrics', 'App\Services\MetricsService')
@inject('metrics', 'App\Services\MetricsService')

<div>
Monthly Revenue: {{ $metrics->monthlyRevenue() }}.
</div>
<div>
Monthly Revenue: {{ $metrics->monthlyRevenue() }}.
</div>

<a name="extending-blade"></a>
## Extending Blade
Expand All @@ -206,40 +206,40 @@ Blade even allows you to define your own custom directives. You can use the `dir

The following example creates a `@datetime($var)` directive which formats a given `$var`:

<?php

namespace App\Providers;

use Blade;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot()
{
Blade::directive('datetime', function($expression) {
return "<?php echo with{$expression}->format('m/d/Y H:i'); ?>";
});
}

/**
* Register bindings in the container.
*
* @return void
*/
public function register()
{
//
}
}
<?php

namespace App\Providers;

use Blade;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot()
{
Blade::directive('datetime', function($expression) {
return "<?php echo with{$expression}->format('m/d/Y H:i'); ?>";
});
}

/**
* Register bindings in the container.
*
* @return void
*/
public function register()
{
//
}
}

As you can see, Laravel's `with` helper function was used in this directive. The `with` helper simply returns the object / value it is given, allowing for convenient method chaining. The final PHP generated by this directive will be:

<?php echo with($var)->format('m/d/Y H:i'); ?>
<?php echo with($var)->format('m/d/Y H:i'); ?>


Loading

0 comments on commit 75651ca

Please sign in to comment.