Install VSCode plugins
- Live Server https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer
- PHP Server https://marketplace.visualstudio.com/items?itemName=brapifra.phpserver
Then run in project directory
composer require eftec/bladeone
Create views
folder in project root
Create base structure file (i'm calling it views/app.blade.php
) and basic partials - header, footer, in views/layout
folder
<html>
<head>
<title>{{ $pagetitle }}</title>
{{--...css and other head things--}}
</head>
<body>
@section('header')
@include('layout.header')
@show
@yield('main')
@include('layout.footer')
</body>
</html>
Then create view file for main page (in this case views/main.blade.php
)
@extends('app')
@section('main')
<h1>{{ $pagetitle }}</h1>
...And other content
@endsection
Configure PHP server to work on the same port with Live Server, for example
"phpserver.port": 5500
Create index.php
in project root file with content:
<?php
require "vendor/autoload.php";
use eftec\bladeone\BladeOne;
$views = __DIR__ . '/views';
$cache = __DIR__ . '/cache';
$blade = new BladeOne($views,$cache); // MODE_DEBUG allows to pinpoint troubles.
echo $blade->run("main",array("pagetitle"=>"My Main Page"));
?>
Open index.php
in editor, start live server, then start PHP server using command palette -> PHP Server: Serve Project
Now you can use extends, include, loops and other blade directives, write less code and have the same versions of included partials across all the templates in the project - write once - use everywhere
To save served pages as html just run php index.php>index.html
from console