Skip to content

Instantly share code, notes, and snippets.

@sashabeep
Last active January 19, 2022 12:24
Show Gist options
  • Save sashabeep/e5c15f56c84148f7415712e5b697f1de to your computer and use it in GitHub Desktop.
Save sashabeep/e5c15f56c84148f7415712e5b697f1de to your computer and use it in GitHub Desktop.
Use BLADE engine for static html templates in VSCode

Install VSCode plugins

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment