body %} <div> Hello world <div> <input type="text" placeholder="Color in hexadecimal" /> </div> </div> {% endblock %} This could be a Symfony Form field
body %} <div> Hello world <div data-color-chooser> <input type="text" placeholder="Color in hexadecimal" /> </div> </div> {% endblock %} Marker to find where to inject a React component
'./ColorChooser/ColorChooser'; $('div[data-color-chooser]').each((key, node) => { const input = $(node).find('input'); ReactDOM.render( <ColorChooser color={input.val()} onColorChange={(color) => input.attr('value', color)} />, node ); }); Replacing the content of the node by the React tree
stored as attributes of the node ✓ HTML5 is semantic: many use cases are already considered in the standard ✓ HTML5 is flexible: you can add your own attributes
body %} <div> Hello world <div data-color-chooser data-color-chooser-type="circles"> <input type="text" placeholder="Color in hexadecimal" /> </div> </div> {% endblock %} Option which could be coming from a Twig variable
input = $(node).find('input'); ReactDOM.render( <ColorChooser type={node.getAttribute('data-color-chooser-type')} color={input.val()} onColorChange={(color) => input.attr('value', color)} />, node ); }); Using the option from the data attribute is easy because it’s scoped to the node