I have a paragraph content builder that has a countdown element. Moreover, this element, like other types of paragraphs, can be added an unlimited number of times. This means that the script must be called several times.
The script of the reverse report (https://github.com/PButcher/flipdown#flipdown ) is connected as a library on all pages. In the template of the required paragraph type, I have the following code:
{% block paragraph %}
{% block content %}
<div class="row my-5 pb-0 pb-lg-5">
<div class="col-12 d-flex justify-content-center">
<div id="flipdown" class="flipdown mb-5"></div>
<script>
var myDate = (new Date().getTime() / 1000) + ({{ content.field_timer_date.value|date('U')|format_date('unix') }} * 2) + 1;
new FlipDown(myDate, "flipdown", {
headings: ["Дней", "Часов", "Минут", "Секунд"],
theme: "blue",
}).start();
</script>
</div>
</div>
{% endblock %}
{% endblock paragraph %}
The date in Unix format is inserted as needed, without any wrapper, but the script does not work with the error "Uncaught ReferenceError: FlipDown is not defined"
I usually do all initialization through custom.js, where I have everything wrapped up as needed in Drupal.behaviors. But what if I need a piece of script in the template, as in this example? I have not found a solution for Drupal 10.
drupalSettings
in PHP, and pick them up again in JS, for example HTML IDs to invoke the plugin, if that’s what it needs. If it supports targeting with a class, even easier