diff --git a/composer.json b/composer.json
index 0775c638..83415f2a 100644
--- a/composer.json
+++ b/composer.json
@@ -20,7 +20,7 @@
"php": "^7.1 || ^8.0",
"symfony/filesystem": "^4.1 || ^5.0",
"symfony/finder": "^4.1 || ^5.0",
- "twig/twig": "^2.5",
+ "twig/twig": "^2.9 || ^3.3",
"doctrine/event-manager": "^1.0"
},
"require-dev": {
diff --git a/lib/Templates/default/html/definition-list.html.twig b/lib/Templates/default/html/definition-list.html.twig
index 388277dd..bea3bcb7 100644
--- a/lib/Templates/default/html/definition-list.html.twig
+++ b/lib/Templates/default/html/definition-list.html.twig
@@ -1,4 +1,4 @@
-{% spaceless %}
+{% apply spaceless %}
{% for definitionListTerm in definitionList.terms %}
{% if definitionListTerm.classifiers is empty %}
@@ -31,4 +31,4 @@
{% endif %}
{% endfor %}
-{% endspaceless %}
+{% endapply %}
diff --git a/lib/Templates/default/html/figure.html.twig b/lib/Templates/default/html/figure.html.twig
index 1ca778fc..c97df561 100644
--- a/lib/Templates/default/html/figure.html.twig
+++ b/lib/Templates/default/html/figure.html.twig
@@ -1,4 +1,4 @@
-{% spaceless %}
+{% apply spaceless %}
-{% endspaceless %}
+{% endapply %}
diff --git a/lib/Templates/default/html/paragraph.html.twig b/lib/Templates/default/html/paragraph.html.twig
index d2670419..00b9a7b7 100644
--- a/lib/Templates/default/html/paragraph.html.twig
+++ b/lib/Templates/default/html/paragraph.html.twig
@@ -1,7 +1,7 @@
-{% spaceless %}
+{% apply spaceless %}
{% set text = paragraphNode.value.render()|trim %}
{% if text %}
{{ text|raw }}
{% endif %}
-{% endspaceless %}
+{% endapply %}
diff --git a/lib/Templates/default/html/toc-level.html.twig b/lib/Templates/default/html/toc-level.html.twig
index a0620de5..e8f4d880 100644
--- a/lib/Templates/default/html/toc-level.html.twig
+++ b/lib/Templates/default/html/toc-level.html.twig
@@ -1,7 +1,7 @@
-{% spaceless %}
+{% apply spaceless %}
{% for tocItem in tocItems %}
{% include "toc-item.html.twig" %}
{% endfor %}
-{% endspaceless %}
+{% endapply %}
diff --git a/lib/Templates/default/html/toc.html.twig b/lib/Templates/default/html/toc.html.twig
index e0cccd3c..20ea5932 100644
--- a/lib/Templates/default/html/toc.html.twig
+++ b/lib/Templates/default/html/toc.html.twig
@@ -1,5 +1,5 @@
-{% spaceless %}
+{% apply spaceless %}
{% include "toc-level.html.twig" %}
-{% endspaceless %}
+{% endapply %}
diff --git a/lib/Templates/default/tex/paragraph.tex.twig b/lib/Templates/default/tex/paragraph.tex.twig
index f026e968..4d5b5b5d 100644
--- a/lib/Templates/default/tex/paragraph.tex.twig
+++ b/lib/Templates/default/tex/paragraph.tex.twig
@@ -1,8 +1,8 @@
-{% spaceless %}
+{% apply spaceless %}
{% set text = paragraphNode.value.render() %}
{% if text|trim %}
{{ text|raw }}
{% endif %}
-{% endspaceless %}
+{% endapply %}