0
{% set title_org = nav_item.title.split(" ") %}
{% set title_mod = "" %}
{% for i in title_org %}
  {% if loop.index > 3 %}
    {% set title_mod = title_mod + ' ' + i %}
  {% endif %}
{% endfor %}
<a href="{{ nav_item.url }}" id="{{ id }}">{{ title_mod }}</a>

{{ title_mod }} is an empty string despite concatenated within the loop. How can I retrieve the updated {{ title_mod }} outside the for loop?

notalentgeek
  • 4,939
  • 11
  • 34
  • 53

1 Answers1

0

You should use array/dictionary like

<p style="display:none;">
{% set title_data = {'org': nav_item.title.split(" "), 'mod' : ""} %}
{% for i in title_data.org %}
  {% if loop.index > 3 %}
    {{ title_data.update({'mod' : title_data.mod + ' ' + i}) }}
  {% endif %}
{% endfor %}
</p>
<a href="{{ nav_item.url }}" id="{{ id }}">{{ title_data.mod }}</a>

Looks like a dupe

notalentgeek
  • 4,939
  • 11
  • 34
  • 53
vqw
  • 41
  • 5
  • I am not sure why this need to be inside a dictionary in the first place. Nevertheless, I changed `'mod' = ` into `'mod' : ` in your first line. It then returns this error, `jinja2.exceptions.TemplateSyntaxError: expected token 'end of statement block', got '.'`. – notalentgeek Jul 04 '17 at 20:35
  • The error located in `{% set title_data.update({'mod' : title_data.mod + ' ' + i}) %}`. I am not sure which one, I cannot find any documentations for `update()` :(. – notalentgeek Jul 04 '17 at 20:42
  • _I am not sure why this need to be inside a dictionary in the first place_ This is how Jinja handles global/local context vars - just a workaround. Please try now, as I have included `set` which wasn't needed – vqw Jul 04 '17 at 20:45
  • Without `set` it returns another error, `jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'title_data'. Jinja was looking for the following tags: 'elif' or 'else' or 'endif'. The innermost block that needs to be closed is 'if'.`. Where is the documentation actually? I cannot find this in Jinja2 website. – notalentgeek Jul 04 '17 at 20:48
  • Looks like me being mobile and cannot test it asap is error prone. Missing `do` keyword now, hope it works now :P – vqw Jul 04 '17 at 20:50
  • It is now `jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'do'. Jinja was looking for the following tags: 'elif' or 'else' or 'endif'. The innermost block that needs to be closed is 'if'.`. No hurry mate, thank you for spending time a bit. – notalentgeek Jul 04 '17 at 20:51
  • It seems I need to enable Jinja2 `do` extension. But I don't know how because the Python codes are handled in MKDocs (this means I need to recompile MKDocs or something). – notalentgeek Jul 04 '17 at 21:05
  • I solved the problem with a "hacky" solution, check here, https://pastebin.com/KZavcErF. Since I cannot access Jinja2's `do` extension, I need to do `update()` in `{{ }}` brackets. So, I put the update operation in `

    ` so that the update counts yet it is not displayed. If you update your solution I will mark it as answer.

    – notalentgeek Jul 04 '17 at 21:21
  • @notalentgeek glad you have solved it, answer updated! :) – vqw Jul 04 '17 at 21:32