askama icon indicating copy to clipboard operation
askama copied to clipboard

Recursive includes end up in endless loop

Open mashedcode opened this issue 7 years ago • 12 comments
trafficstars

Recursive includes don't work. Without those one can't build tree structures. This is just a note. I'm currently not interested in implementing this. Alternatively or rather additionally there's the jinja2 recursive keyword for loops one could implement.

mashedcode avatar Jul 27 '18 08:07 mashedcode

Okay, thanks for the feedback! I'll take a look and see if there's an easy fix. Off the top of my head I don't see why it wouldn't work... Do you happen to have an error message for me?

djc avatar Jul 27 '18 12:07 djc

No error message. But it comes as a surprise to me that you think it should work as handle_include generates inline code without generating a separate function.

mashedcode avatar Jul 27 '18 12:07 mashedcode

Hmm, yeah, that makes sense.

djc avatar Jul 27 '18 12:07 djc

This might be working after #114.

djc avatar Sep 07 '18 18:09 djc

It does not solve this issue, but I think it is similar. In the test I will put several recursives included, that is, two more templates and included.html. I will keep you informed

zzau13 avatar Sep 07 '18 20:09 zzau13

@mashedcode The tests work correctly, give me the exact use case please.

zzau13 avatar Sep 07 '18 21:09 zzau13

As already mentioned it's for tree data structures such as:

struct RecursiveInclude<'a> {
    data: &'a str,
    childs: &'a [RecursiveInclude<'a>],
}

mashedcode avatar Sep 08 '18 11:09 mashedcode

I guess this structure is different from the template? In principle the tests, and pick up this case, with an nested slice. Anyway now I add this.

zzau13 avatar Sep 08 '18 11:09 zzau13

I understand that is this case? That case and all that occurs a recursion loop of includes returns error at overflow multiply. This is the case? since you say that there is no error message. But you can clarify it in a with the code.

zzau13 avatar Sep 08 '18 12:09 zzau13

@botika Exactly, that's what this issue is about.

mashedcode avatar Sep 08 '18 13:09 mashedcode

The same thing happens with macros. But I do not see the behavior in pallet/jinja. And for recursive macros or loops they use a different syntax. It is difficult to detect the recursion loop without this. Anyway, I think this behavior is of the macro or loop because they have their own scope.

zzau13 avatar Sep 08 '18 13:09 zzau13

Hello everybody. I have been facing this problem, and I found a solution: call the render method from the template itself.

The struct definition would be something like this:

#[derive(Template)]
#[template(path = "recursive_template.html", escape = "none")]
struct RecursiveInclude<'a> {
    data: &'a str,
    children: &'a [RecursiveInclude<'a>],
}

And then the template:

<li><span>{{ data }}</span>
    <ul>
            {% for c in children %}
            {{ c.render().unwrap() }}
            {% endfor %}
    </ul>    
</li>

josefrcm avatar Apr 17 '19 04:04 josefrcm