rusttera

How to create array using Tera in Rust?


I am stuck in a simple problem but not able to figure it. I am not sure if this is the right place to ask the question on a package in Rust. Most of the time, in the template, we will want to transform our data. For example, I wanted to concat n arrays in one line. I can use ~ operator only if I know the number of arrays. Below is the requirement I am looking for,

{% macro generate_table(table) %}
    {% for rows in 0..table.length %}
        {{ table[table.col_header[0]][row] ~ "     ||      " ~ [table.col_header[1]][row] }}
    {% endfor %}
{% endmacro input %}

I want to do.

{% macro generate_table(table) %}
    {% for rows in 0..table.rlength %}
        {% for cols in 0..table.clength %}
            {{ arr.insert(table[table.col_header[cols]][row]) }}
        {% endfor %}
        {{ arr | join(sep="     ||      ") }}
    {% endfor %}
{% endmacro input %}

Solution

  • I figured it out. Using concat(with="")

    {% macro generate_table(table) -%}
    
        {% for row in [0,1,2] -%}
            {% set_global row_val = [] -%}
            {% for cols in [0,1,2] -%}
                {% set_global row_val = row_val | concat(with= table.col_values[table.col_header[cols]][row]) -%}
            {% endfor -%}
            {{ row_val | join(sep=" ") }}
        {% endfor -%}
    {% endmacro generate_table -%}