Integrating twig with react.js

元气小坏坏 提交于 2020-04-11 17:22:32

问题


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.


回答1:


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.




回答2:


Follow the following steps

  1. Create api router version of your normal routers
  2. Redirect Router /api to the router
  3. Set _format default parameter in your api detail definition to json
  4. Build your ReactJS to communicate with your server with Json

These were my emergency response steps when I had the same problem, but I still later went through the rigorous work of converting everything later, because my json were filled with html tags. it helped at time, it gave me chance to buy time



来源:https://stackoverflow.com/questions/43819213/integrating-twig-with-react-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!