2

How do I share template information between my PHP backend and JavaScript / AJAX requests?

Times ago I just sent my AJAX requests and had the HTML generated by the server and sent as such. Today I have my AJAX data as JSON but I have no idea how to use the same template (e.g. a users list) at the server side and (for refreshing, filtering etc.) at client side without creating redundant layout code.

Is there a template language with parsers as well for PHP/Laravel and JavaScript?

The Laravel template engine Blade is obviously not usable in JavaScript. The only sharing template language I found via Google was Mustache, but the parser for Laravel was outdated.

Is there anything else out there and which approach do you use for that?

random
  • 9,774
  • 10
  • 66
  • 83
Loilo
  • 13,466
  • 8
  • 37
  • 47
  • Having been down this same road before, I *highly* recommending not using a hybrid approach. Render your templates on the server or on the client; don't try to do both. – Jordan Running Jul 02 '14 at 19:06
  • Interesting to hear. So which way would you recommend? I don't feel comfortable with rendering everything client-sided, can't even explain why. On the other hand I see it as a (partially) immense waste of bandwidth to render stuff on the server side. – Loilo Jul 02 '14 at 19:10
  • That's a tough question. Personally I'd go with client, but if you're not comfortable with that I'm not sure what to tell you. There is a third option, which is to render JavaScript templates both on the client and the server side, but I don't know if there's a good way to do that in PHP-land. – Jordan Running Jul 02 '14 at 19:14
  • 1
    Yep, neither do I. I'm actually pretty new to the whole template stuff and at the moment it's my first try NOT to reinvent the wheel for every project I develop. So Laravel is pretty promising in that question but I actually wonder that there doesn't seem to be a sane solution for this client-server issue. ;) – Loilo Jul 02 '14 at 19:21

3 Answers3

1

Your boiled down question:

Is there a template language with parsers as well for PHP/Laravel and JavaScript?


Laravel and Mustache | server side:

  • conarwelsh/mustache-l4 is a Mustache.php wrapper for Laravel 4. They seems to keep up very well as opposed to what you tell (I presume you mean michaelenger/mustacheview which is actually a Laravel 3 bundle). I stand corrected if am wrong.

  • Laravel's Blade doesn't rule out Mustache at all. You just have to create a Mustache partial without using blade.php extension and include it within a regular Blade template using @include (More details here)

Serving Mustache template:

  • You can even coin any custom Response you need using Response Macros such Response::mustache(...) leveraging Response::make(...) (see here for more details).

Some samples of interest:


My short answer (Updated):

Don't look elsewhere: Laravel + Mustache + Javascript if a mix of server|client side rendering is part of your requirements.

Get your hands dirty! :)

menjaraz
  • 7,551
  • 4
  • 41
  • 81
  • Thanks for your answer. This was pretty much exactly what Google gave me. I just wasn't able to install conarwelsh's Mustache.php wrapper because my console gives me the following error: conarwelsh/mustache-l4 dev-master requires mustache/mustache dev-master -> no matching package found. Seems to have something to do with his composer.json but I have near to zero experience with git and no idea how to fix this - I can probably figure it out by some more googling around. ;) So just for comparing to the answers above: Would you actually *recommend* to do the rendering on client AND server side? – Loilo Jul 03 '14 at 13:59
  • You are welcome. I would say that's pretty another new opinion based question, not a very good fit for SO. If I were to use a mix of Mustache template Server/Client side, I would opt for [mustache.js](https://www.npmjs.org/package/mustache) [NodeJS] for consistency. It's all up to your requirements. – menjaraz Jul 03 '14 at 14:41
  • Okay, then let me clarify this a bit and put it into a not-really-opinion question: Is there an established convention that classifies the sharing of rendering between server and client as good or bad practice? – Loilo Jul 03 '14 at 15:07
  • Good point, why not edit your question or even more post a new one to receive firsthand advice from people well versed in the matter? Thanks to your posts, I will focus on [tag:dust.js] which I found promising as far as I am concerned. – menjaraz Jul 04 '14 at 04:42
0

I had the same issue with Laravel and Angularjs, what I did is that I created a route to return templates http://domain.com/templates/name

This route will View::make('templates'.$name);, this is an AngularJs template that will be filled with data returned by JSON API. Remember to use non conflicting tags I used {{ for Laravel and <% for Angular.

Issam Zoli
  • 2,724
  • 1
  • 21
  • 35
  • Well but this actually sounds like my problem: It means you have to provide a different template for the client side than you are using on your server. The whole point in my question is if tehre is a way to reasonably use the same template code to render as well on the server and on the client side. Thanks though for sharing your thoughts. :) – Loilo Jul 02 '14 at 19:18
  • AFAIK every templating engine has different conventions, so unless you want only to use {{}}, you can't find a Js engine with the same conventions as Blade. I had built some of Blade things in Angular to support I18n, I suggest you do the same if you really need that – Issam Zoli Jul 02 '14 at 19:23
  • If you are building a SPA you have to render templates on the client. – Issam Zoli Jul 02 '14 at 19:25
  • It wouldn't necissarily need the same conventions as Blade, it just should run on both stieds. I actually thought about {{}} stuff (like Mustache) but as Jordan already mentioned, it's probably better just to decide for one side to do the rendering. Giving rendered HTML to the client just seemed a bit "dirty" for me. ;) – Loilo Jul 02 '14 at 19:33
  • FWIW Mustache probably has the most consistent cross-platform support, probably because it allows (by design) very little logic in the template. That is, Mustache templates that work with a JavaScript parser will probably work with a PHP parser, too, so it's one potential solution to your quandary. – Jordan Running Jul 03 '14 at 01:25
-1

RENDER YOUR TEMPLATE SERVER SIDE! I'm not sure at what point someone decided you needed to send JSON to the front end, but that's not how they do it in Rails. In Rails, they actually render the template server side, then send it back to the front end and your JS appends it to the page (or, actually sends back the JS + the markup to attach). This saves a ton of time and headache, trust me.

Sabrina Leggett
  • 9,079
  • 7
  • 47
  • 50