Views
Poison Template Engine
Poison is the simple, yet powerful templating engine provided with Atom. Unlike other popular PHP templating engines, Atom does not restrict you from using plain PHP code in your views. In fact, all Poison views are compiled into plain PHP code and cached until they are modified, meaning Poison adds essentially zero overhead to your application. Poison view files use the .poison.php
file extension and are typically stored in the resources/views
directory.
Template Inheritance
Defining A Layout
Two of the primary benefits of using Poison 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 Poison view:
<!-- Stored in resources/views/layouts/app.poison.php -->
<html>
<head>
<title>App Name - @show('title')</title>
</head>
<body>
@yield('sidebar')
<div class="container">
@yield('content')
</div>
</body>
</html>
As you can see, this file contains typical HTML mark-up. However, take note of the @show
and @yield
directives. The @show
directive, as the name implies, show a variable value, while the @yield
directive is used to display the contents of a given section.
Extending Layouts
When defining a child view, use the Poison @extend
directive to specify which layout the child view should "inherit". Views which extend a Poison layout may inject content into the layout's sections using @section
directives and define variables using @var
directives. Remember, as seen in the example above, the contents of these sections will bedisplayed in the layout using @yield
:
<!-- Stored in resources/views/home.poison.php -->
@var('title', 'Page Title')
@section('sidebar')
<p>This is shown as the sidebar.</p>
@endsection
@section('content')
<p>This is my body content.</p>
@endsection
@extend('layouts.app')
Poison views may be returned from routes using the global view
helper:
router()->get('home', function() {
return view('home');
});
Displaying Datas
You may display data passed to your Poison views by wrapping the variable in curly braces. For example, given the following route:
router()->get('greeting', function() {
return view('welcome', ['name' => 'Marc']);
});
You may display the contents of the name
variable like so:
Hello, {{ $name }}
Poison
{{ }}
statements aren't sent through PHP'shtmlspecialchars
function to prevent XSS attacks
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 Poison echo statement:
The current UNIX timestamp is {{ time() }}
Displaying Escaped Data
By default, Poison {{ }}
statements aren't automatically sent through PHP's htmlspecialchars
function to prevent XSS attacks. If you want your data to be escaped, you may use the following syntax:
Hello, {!! $name !!}
Control Structures
In addition to template inheritance and displaying data, Poison also provides convenient shortcuts for common PHP control structures, such as conditional statements and loops. These shortcuts provide a very clean, terse way of working with PHP control structures, while also remaining familiar to their PHP counterparts.
If Statements
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
Foreach Loop
In addition to conditional statements, Poison provides simple directives for working with PHP's loop structures. Again, each of these directives functions identically to their PHP counterparts:
@foreach ($users as $user)
<p>This is user {{ $user->id }}</p>
@endforeach
Comments
Poison also allows you to define comments in your views. However, unlike HTML comments, Poison comments are not included in the HTML returned by your application:
{{-- This comment will not be present in the rendered HTML --}}
PHP
In some situations, it's useful to embed PHP code into your views. You can use the Poison @php
directive to execute a block of plain PHP within your template:
@php
//
@endphp
Including Sub-Views
Poison's @include
directive allows you to include a Poison view from within another view. All variables that are available to the parent view will be made available to the included view:
<div>
@include('shared.errors')
<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'])
Dernière mise à jour
Cet article vous a-t-il été utile ?