How does one avoid duplicated CSS/JS inside a Jinja2 macro?
So lets say I have a Jinja2 macro which contains a CSS <style>
and JS <script>
tag like this:
<code>{% macro render_test(eleID) %}
<style> .testEle {color: red;} </style>
<div class="testEle" id="{{ eleID }}"></div>
<script> function foo() {} </script>
{% endmacro %}
</code>
<code>{% macro render_test(eleID) %}
<style> .testEle {color: red;} </style>
<div class="testEle" id="{{ eleID }}"></div>
<script> function foo() {} </script>
{% endmacro %}
</code>
{% macro render_test(eleID) %}
<style> .testEle {color: red;} </style>
<div class="testEle" id="{{ eleID }}"></div>
<script> function foo() {} </script>
{% endmacro %}
This will duplicate the <style>
and <script>
for every call to the macro on the site.
I tried adding an additional parameter e.g. add_css_js
and according if statements, but this doesn’t seem clean and correct.
So how can one avoid this? Or whats the correct way to handle this?