0

I'm choosing frameworks for my next project and got stuck on a tricky problem: How to share templates between server-side PHP and client-side Javascript?

On server, my application is going to be written in PHP, using Symfony, Twig, and MtHAML. I have few templates as an early GUI prototype and I really like HAML as it cuts my templates in half.

On client, I would like to use Vue.js or React or something like that (and say bye to jQuery spaghetti monsters). I haven't decided what to use here yet. I like ES6 class syntax. The client side is going to be a set of relatively simple SPAs — old-school standalone pages with some JS to make them more interactive. Therefore, there will be no routing nor manipulating browser history on client, but still, I want to update already loaded page here and there.

So, I will need to use templates at two places: server-side in PHP and client-side in Javascript. The question is, how to share one template on both sides?

I'm thinking about compiling the HAML templates with Dust.js/php (Mustache is too dumb) into Javascript or some similar aproach. Another way could be to render Vue templates from HAML (Gitlab does that) and somehow pre/post-process them into PHP templates. But I'm sure there were many people dealing with this problem too. Unfortunately, most resources on the Web are outdated or very messy. Is there some ready-to-use sollution?

Josef Kufner
  • 2,851
  • 22
  • 28

1 Answers1

0

I was solving similar problem before and the best solution for me became to use Symfony to build a decent API and then some js framework for frontend. Juggling with templates between two environments was a real pain. Maybe you'll choose different approach, just a suggestion from me...

Jan Rydrych
  • 2,188
  • 2
  • 13
  • 18
  • I have a nice JSON REST API, of course. The thing is, that once I fetch/update some entity, I need to present it to users — most likely to render it the same way as server-side template did earlier, but with the fresh data. Unless you mean to fetch already rendered HTML fragments of a page. – Josef Kufner Feb 09 '17 at 08:22