davidecesarano/embryo-view

A PHP renderer for rendering PHP views in PSR response.

1.0.3 2021-04-27 08:08 UTC

This package is auto-updated.

Last update: 2024-10-27 16:42:56 UTC


README

PHP template engine and renderer for PSR-7 response.

Features

  • PSR compatible.
  • Alternative syntax.
  • Views compiled and cached until they are modified.
  • PSR-15 middleware for minify html.

Requirements

Installation

Using Composer:

$ composer require davidecesarano/embryo-view

Example

Create Response object, set views and compilers directory, create a View object. Render view with render() method passing response, template and data. Finally, emit response.

use Embryo\Http\Emitter\Emitter;
use Embryo\Http\Factory\{ResponseFactory, StreamFactory};
use Embryo\View\View;

$response      = (new ResponseFactory)->createResponse(200);
$templatePath  = __DIR__.DIRECTORY_SEPARATOR.'views';
$compilerPath  = __DIR__.DIRECTORY_SEPARATOR.'compilers';
$view          = new View($templatePath, $compilerPath);

$response = $view->render($response, 'page', ['message' => 'Hello World!', 'status' => 1]);

$emitter = new Emitter;
$emitter->emit($response);

Usage

Create and render views

If you want create a template with partials file, you can write this:

<!-- header.php -->
<html>
    <head>
        <title>{{ $title }}</title>
    </head>
    <body>
<!-- home.php -->
@include('header', ['title' => $title])
    
        <h1>{{ $title }}</h1>
    
    </body>
</html>

In this example you can use @include(filename, data) for include header.php in home.php passing data to file. Embryo View will compile the file by replacing the alternative syntax in PHP code. Finally, you may display page with render:

$response = $view->render($response, 'home', ['title' => 'Hello World!']);

Display data

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

{{ $name }} // echo htmlentites($name)

If you want display html content use like so:

{{{ $html }}} // echo $html

If you use VueJs mustache templating, you may use like so:

@{{ myVar }} // echo {{ myVar }}

If statements

You may construct if statements using the @if, @elseif, @else, and @endif directives.

@if ($status == 1)
    Status is 1
@elseif ($status == 2)
    Status is 2
@else
    Status is {{ $status }}
@endif

Loops

Embryo View provides simple directives for working with PHP's loop structures (for, foreach and while):

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

@foreach ($users as $user)
    User id is {{ $user->id }}
@endforeach

@while ($user = $users)
    User id is {{ $user->id }}
@endwhile

PHP

You can use the @php directive to execute a block of plain PHP within your template:

@php 
    $a = 1;
    echo  $a; 
@endphp