0

If template_from_string is inside {% macro ... %} string is not parsed. Example of may code:

example.yaml

items:
    item1: item1
    item2: |
        {{ AI }} lorem ipsum dolor sit amet...

php function

function AI() {
    return 'AI<span class="ai-tm">&#8482;</span>';
}

Timber function.php

 // Global $context
 $context['AI'] = AI();

Twig

{# If outside macro, template_from_string parses ok #}
<pre>dump(include(template_from_string(items.item2)))</pre>

{% macro example_macro(items) %}
   {# items is php array parsed from yaml file
and passed as parameter to macro #}
  {% for item in items %}
    <p>{{ include(template_from_string(item)) }}</p>
  {% endfor %}
{% endmacro %}

Results:

{# Notices that {{ AI }} isn't parsed #}
<p>lorem ipsum dolor sit amet...</p>
Luka Juras
  • 25
  • 4
  • Does this answer your question? [Can't access global data or macro within macro](https://stackoverflow.com/questions/60391819/cant-access-global-data-or-macro-within-macro) – DarkBee Dec 27 '22 at 21:57
  • @DarkBee not exactly. I have the content of an array, but for some reason `include(template_from_string()` doesn't parses it correctly it inside macro. If it is outside macro, the output is ok. – Luka Juras Dec 27 '22 at 22:21
  • Read the answer again, variables are not accessible unless you pass them – DarkBee Dec 27 '22 at 23:13

0 Answers0