Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

darkylmnx's avatar

Blade template engine on the browser?

Do any of you know if it's possible to port the blade template engine to be usable on the frontend? In the browser with JS?

Here's why: I'm building a CMS-like app with a visual editor lib.

To render a preview while editing, the lib sends the data to an endpoint on my Laravel app that would then render a blade view.

When users have poor network, each update on the visual editor is slow.

From what I saw on other page editors like Elementor, they seem to have two rendering, on rendering for "real" visitors (server side), and on rendering for previews (client side with mustachjs or something).

That means you have to maintain your UI twice in two different template languages. I don't want to do that.

If blade had a port on browser, it would be very easy to share the template, just the syntax.

Thanks.

0 likes
3 replies
LaryAI's avatar
Level 58

As of my knowledge cutoff in early 2023, there is no official port of the Blade template engine that runs natively in the browser using JavaScript. Blade is a server-side templating engine that is specifically designed for PHP and Laravel, and it relies on PHP's server-side rendering capabilities.

However, there are a few approaches you could consider to achieve a similar result:

  1. Use a JavaScript Templating Engine: You could use a client-side JavaScript templating engine like Mustache.js, Handlebars.js, or Vue.js that has a syntax similar to Blade. You would need to maintain two sets of templates, but you could design them to be as similar as possible to ease maintenance.

  2. Precompile Blade Templates: You could precompile your Blade templates into plain HTML with placeholders and then use JavaScript to fill in the placeholders on the client side. This would allow you to use the same Blade templates for both server-side and client-side rendering, but it would require a custom solution to handle the placeholders in JavaScript.

  3. AJAX and Caching: Instead of porting Blade to the browser, you could optimize the network requests. Use AJAX to send only the data that has changed to the server and cache the rendered HTML on the server side. This way, you can minimize the data sent over the network and improve the responsiveness of the preview.

  4. Server-Side Rendering with Client-Side Hydration: You could render the initial state on the server using Blade and then use a JavaScript framework like Vue.js or React to handle subsequent updates on the client side. This is known as hydration, where the client-side framework takes over a server-rendered page.

  5. Custom Blade to JavaScript Compiler: As a more complex solution, you could attempt to create a custom compiler that converts Blade templates into a JavaScript templating engine format. This would be a significant undertaking and would require deep knowledge of both Blade's and the chosen JavaScript templating engine's internals.

Here's a simple example of how you might use Mustache.js to render templates that are similar to Blade:

<!-- Blade Template -->
<div>
    <h1>{{ $title }}</h1>
    <p>{{ $content }}</p>
</div>

<!-- Mustache.js Template -->
<script id="template" type="x-tmpl-mustache">
<div>
    <h1>{{ title }}</h1>
    <p>{{ content }}</p>
</div>
</script>

<script>
// Assuming you have Mustache.js included in your page
var template = document.getElementById('template').innerHTML;
var rendered = Mustache.render(template, { title: 'Hello World', content: 'This is a Mustache.js template.' });
document.getElementById('target').innerHTML = rendered;
</script>

In this example, the Mustache.js template is similar to the Blade template, but you would still need to maintain both versions. The JavaScript code renders the template with the provided data and inserts it into the page.

Ultimately, the best approach depends on your specific requirements and constraints. If maintaining two sets of templates is a significant concern, you might need to explore more advanced solutions like custom compilers or hybrid rendering strategies.

Snapey's avatar

no. Blade relies on access to server variables and of course is written in php

darkylmnx's avatar

@Snapey I specifically said " if it's possible to port the blade template engine to be usable on the frontend? In the browser with JS?" which means I know it's a backend engine to begin with.

The goal of my post is to find a browser template engine with the exact same syntax.

Please or to participate in this conversation.