Render a Blade template from a Jigsaw event listener

Jigsaw (a statitc site generator) has 3 events you can listen to and run custom code when these events fire. One thing you may want to do in an event listener is render some HTML using a templating language. It makes sense to use Blade for this just…


This content originally appeared on Hussein Al Hammad and was authored by Hussein Al Hammad

Jigsaw (a statitc site generator) has 3 events you can listen to and run custom code when these events fire. One thing you may want to do in an event listener is render some HTML using a templating language. It makes sense to use Blade for this just like the rest of the project.

To do this, use BladeCompiler in bootstrap.php at the root directory of your Jigsaw project:

1<?php
2 
3use Illuminate\View\Compilers\BladeCompiler;
4use TightenCo\Jigsaw\Jigsaw;
5 
6 
7$events->afterBuild(function ($jigsaw) {
8 // get the Blade template as a string
9 $bladeTemplate = $jigsaw->readSourceFile('path/to/template.blade.php');
10 
11 // render the template
12 $renderedHTML = BladeCompiler::render($bladeTemplate, [
13 // data to pass to the template
14 ]);
15 
16 // write the file (path relative to the build directory)
17 $jigsaw->writeOutputFile('index.html', $renderedHTML)
18});

You're not going to automatically get all of Jigsaw's global variables though. You can pass a $page object with all your global config variables available as properties (mimicking what Jigsaw does when rendering a Blade template):

1$renderedHTML = BladeCompiler::render($bladeTemplate, [
2 'page' => $jigsaw->getConfig(),
3]);


This content originally appeared on Hussein Al Hammad and was authored by Hussein Al Hammad


Print Share Comment Cite Upload Translate Updates
APA

Hussein Al Hammad | Sciencx (2024-06-10T00:00:00+00:00) Render a Blade template from a Jigsaw event listener. Retrieved from https://www.scien.cx/2024/06/10/render-a-blade-template-from-a-jigsaw-event-listener/

MLA
" » Render a Blade template from a Jigsaw event listener." Hussein Al Hammad | Sciencx - Monday June 10, 2024, https://www.scien.cx/2024/06/10/render-a-blade-template-from-a-jigsaw-event-listener/
HARVARD
Hussein Al Hammad | Sciencx Monday June 10, 2024 » Render a Blade template from a Jigsaw event listener., viewed ,<https://www.scien.cx/2024/06/10/render-a-blade-template-from-a-jigsaw-event-listener/>
VANCOUVER
Hussein Al Hammad | Sciencx - » Render a Blade template from a Jigsaw event listener. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/10/render-a-blade-template-from-a-jigsaw-event-listener/
CHICAGO
" » Render a Blade template from a Jigsaw event listener." Hussein Al Hammad | Sciencx - Accessed . https://www.scien.cx/2024/06/10/render-a-blade-template-from-a-jigsaw-event-listener/
IEEE
" » Render a Blade template from a Jigsaw event listener." Hussein Al Hammad | Sciencx [Online]. Available: https://www.scien.cx/2024/06/10/render-a-blade-template-from-a-jigsaw-event-listener/. [Accessed: ]
rf:citation
» Render a Blade template from a Jigsaw event listener | Hussein Al Hammad | Sciencx | https://www.scien.cx/2024/06/10/render-a-blade-template-from-a-jigsaw-event-listener/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.