Keats / tera2

28 stars 3 forks source link

Could Macros accept arrays of maps/arrays? #21

Closed jamiedumont closed 5 months ago

jamiedumont commented 7 months ago

Before I start, thank you for both Tera and Zola! They are among the best open source libraries I’ve used.

I encountered a limitation with Tera 1 macros whilst using Zola, where I wanted to pass an array of maps as an argument.

I can work around it fairly easily by moving the macro abstraction one level down (call the macro x times rather than pass an array of x maps to the macro that is called only once and loops through the array).

Is there any reason or major blocker to macros in Tera 2 accepting an array of maps?

I would love to contribute myself and provide a PR but I’m still quite early in my learning with Rust and bumping up against the lower-level-learning-curve!

Cheers!

Keats commented 7 months ago

I believe it should work in tera2? I haven't tried it though

Keats commented 7 months ago

If you have an example template i can add it as a testcase

jamiedumont commented 6 months ago

I was trying to generate art-directed <picture> elements, passing an array of maps for the various different versions the browser could select from. This was the error that I was running into: https://github.com/Keats/tera/issues/791

Following is pseudo-code, but what I was initially trying to do was something like:

{% macro picture(sources=[], alt="Description of image") %}
  <picture>
  {% for src in sources %}
    <source>
    ...
    </source>
  {% endfor %}
  </picture>
{% endmacro picture %}

Where the sources arg would look something like this when called:

[
  {path="person_square.jpg", media="max-width: 320px", size="calc(100vw - 2rem)", generated_size="608"},
  {path="person.jpg, media="min-width: 320px", size="50vw", generated_size="1200"},
  ...
]

Instead I ended up with this, moving the loop outside the macro and into my shortcode/template:

{% macro source(path="portfolio/0T4A1536.jpg", media="max-width: 600px", size="580px", generate_size="580px") %}
<source>
...
</source>
{% endmacro source %}

Eventually I abandoned the idea as there was enough variance in formats, generated size and rendered size at viewport widths that it made more sense to write the HTML by hand and generate images manually.

But I thought that passing an array of maps as a macro argument would be a useful addition to Tera in plenty of other situations.

Keats commented 6 months ago

I'll add a testcase but that should already work in tera2

jamiedumont commented 6 months ago

Excellent, thanks very much! Is there a plan to move Zola to Tera 2 at some point?

Keats commented 6 months ago

Ok looks like it was not supported! I'll add it.

Is there a plan to move Zola to Tera 2 at some point?

Yes Zola is going to be the beta test for Tera2

Keats commented 6 months ago

That should implemented in master if you want to try

jamiedumont commented 5 months ago

Thanks, will do!