😅Laravel-Blade for php full (ok)

https://github.com/PhiloNL/Laravel-Blade

Sử dụng PhiloNL/Laravel-Blade

C:\xampp\htdocs\test\composer.json

{
    "name": "philo/laravel-blade",
    "description": "Use the simple and yet powerful Laravel Blade templating engine as a standalone component.",
    "keywords": ["laravel", "blade"],
    "license": "MIT",
    "authors": [
        {
            "name": "Philo Hermans",
            "email": "me@philohermans.com"
        }
    ],
    "require": {
        "illuminate/view": "~5",
        "illuminate/events": "~5"
    },
    "autoload": {
        "psr-4": {
            "Philo\\Blade\\": "src"
        }
    }
}

C:\xampp\htdocs\test\index.php

<?php
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader
| for our application. We just need to utilize it! We'll require it
| into the script here so that we do not have to worry about the
| loading of any our classes "manually". Feels great to relax.
|
*/
require 'vendor/autoload.php';
use Philo\Blade\Blade;
$views = __DIR__ . '/views';
$cache = __DIR__ . '/cache';
$blade = new Blade($views, $cache);
echo $blade->view()->make('hello', ['name' => 'John Doe'])->render();
// echo $blade->view()->make('hello')->render();

C:\xampp\htdocs\test\views\layouts\master.blade.php

<!-- Stored in resources/views/hello.blade.php -->
@extends('layouts.master')
@section('title', 'Page Title')
@section('sidebar')
    @parent
    <p>This is appended to the master sidebar.</p>
    {{ $name }}
@endsection
@section('content')
    <p>This is my body content.</p>
@endsection

C:\xampp\htdocs\test\views\hello.blade.php

<!-- Stored in resources/views/hello.blade.php -->
@extends('layouts.master')
@section('title', 'Page Title')
@section('sidebar')
    @parent
    <p>This is appended to the master sidebar.</p>
@endsection
@section('content')
    <p>This is my body content.</p>
@endsection

C:\xampp\htdocs\test\src\Blade.php

<?php namespace Philo\Blade;
use Illuminate\Container\Container;
use Illuminate\Events\Dispatcher;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\MessageBag;
use Illuminate\Support\ServiceProvider;
use Illuminate\View\Engines\PhpEngine;
use Illuminate\View\Engines\CompilerEngine;
use Illuminate\View\Engines\EngineResolver;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\FileViewFinder;
use Illuminate\View\Factory;
class Blade {
	/**
	 * Array containing paths where to look for blade files
	 * @var array
	 */
	public $viewPaths;
	/**
	 * Location where to store cached views
	 * @var string
	 */
	public $cachePath;
	/**
	 * @var Illuminate\Container\Container
	 */
	protected $container;
	/**
	 * @var Illuminate\View\Factory
	 */
	protected $instance;
	/**
	 * Initialize class
	 * @param array  $viewPaths
	 * @param string $cachePath
	 * @param Illuminate\Events\Dispatcher $events
	 */
	function __construct($viewPaths = array(), $cachePath, Dispatcher $events = null) {
		$this->container = new Container;
		$this->viewPaths = (array) $viewPaths;
		$this->cachePath = $cachePath;
		$this->registerFilesystem();
		$this->registerEvents($events ?: new Dispatcher);
		$this->registerEngineResolver();
		$this->registerViewFinder();
		$this->instance = $this->registerFactory();
	}
	public function view()
	{
		return $this->instance;
	}
	public function registerFilesystem()
	{
		$this->container->singleton('files', function(){
			return new Filesystem;
		});
	}
	public function registerEvents(Dispatcher $events)
	{
		$this->container->singleton('events', function() use ($events)
		{
			return $events;
		});
	}
	/**
	 * Register the engine resolver instance.
	 *
	 * @return void
	 */
	public function registerEngineResolver()
	{
		$me = $this;
		$this->container->singleton('view.engine.resolver', function($app) use ($me)
		{
			$resolver = new EngineResolver;
			// Next we will register the various engines with the resolver so that the
			// environment can resolve the engines it needs for various views based
			// on the extension of view files. We call a method for each engines.
			foreach (array('php', 'blade') as $engine)
			{
				$me->{'register'.ucfirst($engine).'Engine'}($resolver);
			}
			return $resolver;
		});
	}
	/**
	 * Register the PHP engine implementation.
	 *
	 * @param  \Illuminate\View\Engines\EngineResolver  $resolver
	 * @return void
	 */
	public function registerPhpEngine($resolver)
	{
		$resolver->register('php', function() { return new PhpEngine; });
	}
	/**
	 * Register the Blade engine implementation.
	 *
	 * @param  \Illuminate\View\Engines\EngineResolver  $resolver
	 * @return void
	 */
	public function registerBladeEngine($resolver)
	{
		$me = $this;
		$app = $this->container;
		// The Compiler engine requires an instance of the CompilerInterface, which in
		// this case will be the Blade compiler, so we'll first create the compiler
		// instance to pass into the engine so it can compile the views properly.
		$this->container->singleton('blade.compiler', function($app) use ($me)
		{
			$cache = $me->cachePath;
			return new BladeCompiler($app['files'], $cache);
		});
		$resolver->register('blade', function() use ($app)
		{
			return new CompilerEngine($app['blade.compiler'], $app['files']);
		});
	}
	/**
	 * Register the view finder implementation.
	 *
	 * @return void
	 */
	public function registerViewFinder()
	{
		$me = $this;
		$this->container->singleton('view.finder', function($app) use ($me)
		{
			$paths = $me->viewPaths;
			return new FileViewFinder($app['files'], $paths);
		});
	}
	/**
	 * Register the view environment.
	 *
	 * @return void
	 */
	public function registerFactory()
	{
		// Next we need to grab the engine resolver instance that will be used by the
		// environment. The resolver will be used by an environment to get each of
		// the various engine implementations such as plain PHP or Blade engine.
		$resolver = $this->container['view.engine.resolver'];
		$finder = $this->container['view.finder'];
		$env = new Factory($resolver, $finder, $this->container['events']);
		// We will also set the container instance on this view environment since the
		// view composers may be classes registered in the container, which allows
		// for great testable, flexible composers for the application developer.
		$env->setContainer($this->container);
		return $env;
	}
	public function getCompiler()
	{
		return $this->container['blade.compiler'];
	}
}

Sử dụng jenssegers/blade

C:\xampp\htdocs\test\views\homepage.blade.php

<!-- Stored in resources/views/hello.blade.php -->
@extends('layouts.master')
@section('title', 'Page Title')
@section('sidebar')
    @parent
    <p>This is appended to the master sidebar.</p>
    {{ $name }}
@endsection
@section('content')
    <p>This is my body content.</p>
@endsection

C:\xampp\htdocs\test\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
    <div class="container">
      @yield('content')
    </div>
  </body>
</html>

Sử dụng BladeOne

C:\xampp\htdocs\wpclidemo\composer.json

{
    "name": "jorge/example.cupcakes",
    "description": "Example of a catalogo made with php",
    "type": "project",
    "license": "MIT",
    "authors": [
        {
            "name": "Jorge Castro",
            "email": "jorge.castro.c@gmail.com"
        }
    ],
    "require": {
        "eftec/bladeone": "^3.21",
        "eftec/daoone": "^3.26",
        "eftec/documentstoreone": "^1.9",
        "eftec/validationone": "1.13"
    }
}

C:\xampp\htdocs\wpclidemo\catalog_mysql.php

<?php
include "app.php";
$cupcakes = $db->select("*")->from("cupcakes")->order("name")->toList();
echo $blade->run("cupcakes.catalog", ['cupcakes' => $cupcakes, 'postfix' => 'mysql']);

C:\xampp\htdocs\wpclidemo\views\cupcakes\catalog.blade.php

<!doctype html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="//stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
    <title>Hello, world!</title>
</head>
<body>
<div class="container">
    <h1>Cupcakes World
        <small class="text-muted">Pick your cupcake</small>
    </h1>
    <div class="row">
        @foreach($cupcakes as $cupcake)
        <div class="col-md-4">
            <div class="card">
                <a href="detail_{{$postfix}}.php?id={{$cupcake['IdCupcake']}}">
                    <img src="img/{{$cupcake['Image']}}" class="card-img-top" alt="{{$cupcake['Image']}}">
                </a>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-7">
                            <h5 class="card-title">{{$cupcake['Name']}}</h5>
                        </div>
                        <div class="col-md-5 text-right">
                            <p class="card-text">Price ${{$cupcake['Price']}}</p>
                        </div>
                    </div>
                    <a href="detail_{{$postfix}}.php?id={{$cupcake['IdCupcake']}}" class="btn btn-primary">Eat Me</a>
                </div>
            </div>
            <br>
        </div>
        @endforeach()
    </div>
</div>
</body>
</html>

Last updated