XHP Template Engine pada Laravel 4

XHP Template Engine pada Laravel 4

Limitations

Semenjak rilis HHVM dan XHP yang baru, agar bisa memakai fitur XHP componentdengan cara composition, pada file view harus diawali dengan open tag hhvm

Pada Blade compiler hal tersebut tidak dimungkinkan sebab secara default tidakada open tag hhvm di awal file. Selain itu setiap syntax blade seperti@include, @section, @if, @foreach, dst., akan diubah dan mengandung tag.

Sementara itu proses migrasi View template akan dilakukan secara perlahan denganmasih menggunakan fitur pada Blade compiler.

Examples

File source: index.blade.php.

@extends(‘layout’)</p><p>@section(‘main’) @include(‘header’)</p><p>Hello My Pren {$name}@stop

File compiled (actual result): storage/…/index.php

startSection(‘main’, …); ?&gt; make(‘header’, …)-&gt;render(); ?&gt;</p><p>Hello My Pren stopSection(‘main’, …); ?&gt;</p><p>make(‘layout’)-&gt;render(); ?&gt;

File template Blade adalah file HTML yang disisipi tag PHP.Sedangkan pada XHP, file template adalah file script PHP dimanaecho berfungsi untuk menghasilkan HTML.

Expected result dari template Blade di atas seharusnya:

startSection(‘main’, …); echo $__env-&gt;make(‘header’, …)-&gt;render(); echo</p><p>Hello My Pren {$name};$__env-&gt;stopSection();</p><p>echo $__env-&gt;make(‘layout’)-&gt;render();

Dengan membandingkan actual result dan expected result, makakonversi dari Blade menjadi XHP pada XhpBladeCompiler(extends dari Illuminate\View\Compilers\BladeCompiler)dengan snippet sebagai berikut:

1. Tambah tag

public function compile(…) { … $this&gt;files-&gt;put(…, “</p><p>2. Buang tag  pada syntax @extends, @section,@stop, dan @include.</p><p>protected function compileExtends(…) {…}protected function compileSection(…) {…}protected function compileStop(…) {…}protected function compileInclude(…) {…}

3. Ubah syntax pada tempate dari Blade menjadi XHP-Blade:File source: index.xhp.php.

@extends(‘layout’)</p><p>@section(‘main’) @include(‘header’) echo</p><p>Hello My Pren {$name};@stop

4. Register XhpBladeCompiler untuk file ber-ekstensi ‘xhp.php’.

use Illuminate\View\Engines\CompilerEngine;use Illuminate\Support\ServiceProvider;</p><p>// @see: Illuminate\View\ViewServiceProviderclass XhpServiceProvider extends ServiceProvider {</p><p>public function register() { $resolver = $this-&gt;app[‘view.engine.resolver’];</p><p>$this-&gt;app-&gt;bindShared(‘xhp.compiler’, function($app) { $cache = $app[‘path.storage’].’/views’; return new XhpBladeCompiler($app[‘files’], $cache); });</p><p>$resolver-&gt;register(‘xhp.engine’, function() use($app) { return new CompilerEngine($app[‘xhp.compiler’], $app[‘files’]); });</p><p>View::addExtension(‘xhp.php’, ‘xhp.engine’); }}

Leave a comment