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’, …); ?> make(‘header’, …)->render(); ?></p><p>Hello My Pren stopSection(‘main’, …); ?></p><p>make(‘layout’)->render(); ?>
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->make(‘header’, …)->render(); echo</p><p>Hello My Pren {$name};$__env->stopSection();</p><p>echo $__env->make(‘layout’)->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>files->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->app[‘view.engine.resolver’];</p><p>$this->app->bindShared(‘xhp.compiler’, function($app) { $cache = $app[‘path.storage’].’/views’; return new XhpBladeCompiler($app[‘files’], $cache); });</p><p>$resolver->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