Limenius / ReactRenderer

Client and Server-side React rendering from PHP
MIT License
238 stars 37 forks source link

Usage in PHP without Twig templates? #25

Closed charles4221 closed 5 years ago

charles4221 commented 5 years ago

I'm wondering if it is possible to use this library to perform the React server-side rendering within a standard PHP file without Twig templates? I've spent over a week trying to compile the V8 PHP extension on multiple Ubuntu and CentOS servers and even tried the Docker container with no luck at all, so I've come across this library in search which seems very promising, but I'm looking to use it within a project which just uses standard PHP with no templating engine. Is this possible? I couldn't quite tell from the documentation.

nacmartin commented 5 years ago

Yes, compiling the V8js extension is a pain. I have also spent many hours trying to make it work. It is a pity, because it looks very promising.

This project is mainly about using Twig (at the end this is a twig extension).

In your case I'd try to do the following:

Use an external node.js server for SSR like this one: https://github.com/Limenius/ReactRenderer/blob/master/doc/external-renderer.js

And then use in the PHP side a code similar to this one: https://github.com/Limenius/ReactRenderer/blob/master/src/Limenius/ReactRenderer/Renderer/ExternalServerReactRenderer.php#L56

to send some JS code to that server, and get a string back. The JS code can be something like "render this react component", and the string that you get back is the html representation, but it can be used to execute any javascript, not only React.