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

fesja's avatar

Example of a project with Mustache/Handlebars on PHP & JS on L5?

Hi!

I'm trying to have some templates executed both on the server and client side. I am trying to achieve it with Mustache or Handlebars using laravel-handlebars but I am enable to configure it.

Do you have seen or do you have an example of a project with Mustache/Handlebars/other that renders those templates on PHP and JS on laravel 5?

It will help a lot to see the code.

thanks!

0 likes
7 replies
kkiernan's avatar

Not sure about Mustache exactly but I use VueJS a lot now after watching Jeffrey's videos. What are you having trouble with exactly? One cool tip I didn't think of was to use the @{{ variableName }} syntax so that blade ignores the variable and let's the JS templating engine handle it.

See the blade and JavaScript frameworks section.

http://laravel.com/docs/5.1/blade#displaying-data

If that's not any help, maybe you can post a bit more about your issue?

fesja's avatar

Thanks @kkierman, but it's a different problem. What I want is to have a post.hbs partial template (hbs comes from Handlebars but there are several templates) instead of a Blade template that can be rendered in PHP (by laravel) and , with the same code, rendered in JS (on the client). Imagine we have the following URL http://example.com/search?s=foo.

On server, Laravel uses this partial template:

<ul>
{{#each results}}
   <li>{{ this.name }}</li>
{{/each}}
</ul>

Then if I search again, I use JS to do an AJAX call to an external service that returns JSON. I want to render that JSON with that same partial template.

With such a simple template it's easy, but I'm having problems to share code if I add helpers to add extra functionality to the rendering engine. Like for example: comparing variables, limiting the length of a text, comparing dates or even setting a temp variable for the loop. I don't know how to structure the code so I share those helpers and templates for both PHP and JS.

thanks

pmall's avatar

Like for example: comparing variables, limiting the length of a text, comparing dates or even setting a temp variable for the loop.

One of the fundamental concept of mustache is this is a logic-less template engine, like many js template engine. So no if, not temp variable, nothing like that, just echoing values. I think you can call js helpers in templates but thats all.

I don't know how to structure the code so I share those helpers and templates for both PHP and JS.

Until now sharing template code between php and js is very cumbersome to do. It adds more problems than solutions. For your search example above, a common pattern is to always render the content with js templates.

There is many way of doing it. For instance you can put json in the blade view when you request the url with the browser, this json gets instantly rendered with js. Then when you update the query, you send an ajax request to the same url to update the json and re-render the result list. Other approach is to send an ajax request even the first time, which mean your page gets rendered with blank results then gets updated as soon as the ajax request finished.

fesja's avatar

@pmall thanks. That's exactly what I saw 2 years ago. I thought maybe it had changed.

lorvent's avatar

@fesja I am looking for a similar solution, let me know if you found any, i am also thinking of customizing handlebars syntax so we can use it directly to compile html from .hbs or use directly in laravel just by changing extension

Alsemany's avatar

I already stuck on the same issue , I'm trying to use another JS template engine however I didn't found any , please let me know if you found a solution thanks

Alsemany's avatar

The only solution that I got is to change the Blade brackets

    Blade::setContentTags('<%', '%>');        // for variables and all things Blade
    Blade::setEscapedContentTags('<%%', '%%>');   // for escaped data


Please or to participate in this conversation.