Keats / tera

A template engine for Rust based on Jinja2/Django
http://keats.github.io/tera/
MIT License
3.5k stars 281 forks source link

Tera macros how they are working #870

Closed holotrack closed 11 months ago

holotrack commented 11 months ago

Hello. I can not find how Tera macros are working. Mainly what im asking about, how its happening if i have eg Zola STATIC pregenerated page and i can with macro change class depending on which site is open raight now. Example code doing this:

  {% for item in menu_items -%}
          <li class="mr-3 text-sm">
            <a href={{ get_url(path=item.path, trailing_slash=false, lang=lang) }} class={%if current_path and current_path is starting_with(item.path) %}"inline-block py-2 px-4 text-sky-600 font-bold no-underline"{% else %}"inline-block text-gray-600 no-underline hover:text-gray-900 hover:text-underline py-2 px-4"{%endif%}>
              {{item.name}}
            </a>
          </li>
          {% endfor -%}

How that "%if" is working/generating site after site generation/deployment? Is there some hidden javascript or how its happening?

Keats commented 11 months ago

I don't see any macro in the code sample?

How that "%if" is working/generating site after site generation/deployment?

It is not present after site generation, no JS involved or anything.

holotrack commented 11 months ago

So how its working, how its regenerating site depends on condition? I saw peoples using it t change classes depends on which site you are, normaly we need JS for it so how it is solved here that site is changing look depends on macro condition? Or am i missing something obvious here?

Here is another example of such usage: https://github.com/pawroman/zola-theme-terminimal/blob/master/templates/macros/menu.html

And its working that way its changing look of menu item, and wondering how its working after site generation?

Keats commented 11 months ago

Each page in that menu will be rendered to HTML. When they get rendered, zola will know the current_path/url and you can toggle the menu for that page. It's not a single output page, it's n pages

holotrack commented 11 months ago

Each page in that menu will be rendered to HTML. When they get rendered, zola will know the current_path/url and you can toggle the menu for that page. It's not a single output page, it's n pages

Ahh thank you, this is answaring my question.