Integrating twig with react.js

php, reactjs, symfony, twig

Solution

Simply put you can't do it quickly. React is a front-end JS framework. Twig is a front-end templating engine. While it is possible to make them work together it doesn't make sense to approach your project from this stance. Using Twig and React together is akin to placing a screwdriver in a screw and then beating on it with a hammer.

It would make more sense to replace your Twig usage entirely with React.

Problem

I have successfully built a webapp with php symphony and its twig template system, but now requirement has changed, and I have to make use of node react.js. I have read a handful of documentation on react.js but I am still lost on the best approach for my strict timeframe. Any help on how I can do this safely and quickly is appreciated, even in advanced. How can someone implement react.js in a template like this? ``` {% extends 'admin/base.html.twig' %} {% block content %} Fill This form {{ form(form, {'attr': {'novalidate': 'novalidate'}}) }} {{ form_start(form) }} {{ form_widget(form) }} <input type="hidden" name="token" value="{{ csrf_token('action') }}" /> {{ form_end(form) }} {% endblock %} ``` EDIT Due to the size of my project, restarting form, table and others in react will take months... So I make my symphony app to return the forms and tables html as JSON string and I process the forms in React as if JADE content... I hope this will not surface security threat and I am also asking if it is not too lame. Thanks.

Original source