2

I work on a Symfony 3 app which uses Twig for rendering. To make the code more readable and reusable, I need to split a screen in small parts, parts which could be used in other screens (for example a clock widget). At first it seems easy, I could use the include Twig method to include that part of code. The problem is that my clock uses some specific CSS and JS scripts to work.

What would be the best way to create a reusable components composed of HTML, CSS and JS ? Thx in advance !

1
  • For people interested in this - Please have a look at UX Twig Components Commented Nov 23, 2024 at 8:10

2 Answers 2

1

There is no "Component" concept or approach in Twig, but still you can encapsulate CSS + HTML + JS code in one file and include it as an isolate piece of code:

<style>...</style> {# or use <link href="..."> #}
<div>...</div>
<script>...<script> {# or use <script src="..."> #}

However, probably Twig is not the correct tool to achieve it and you should look at some frontend framework (e.g. VueJS https://v2.vuejs.org/v2/guide/index.html) by passing the data through HTML attributes.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, I would have imagined that there is a possibility to include HTML and to overload the stylesheets and javascripts blocks. I understand your idea with Vue.js or other front framework, but it's not a possible option for my project. Thx for your answer !
0

Probably you can use include passing your data to a specific partial component and render it dinamically.

Check it : https://twig.symfony.com/doc/3.x/tags/include.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.