rust-sailfish / sailfish

Simple, small, and extremely fast template engine for Rust
https://rust-sailfish.github.io/sailfish/
MIT License
806 stars 54 forks source link

Template composition with child elements #113

Open DrSloth opened 1 year ago

DrSloth commented 1 year ago

Hi, it would be nice if sailfish could be used to build reusable parent containers that are completely independent of their child html. This would require a new token to start a block which accepts child elements, a new token to end the block of child elements and a new token to actually insert the children. In my examples i'm just going to use [ for start of "child block" ] for end of child block and ^ for insert children here (these tokens might be problematic, but that's mainly bikeshedding)

Something along the lines of: In the file main.stpl:

<%[ TemplateWithChildren::new("Bye") %>
    <p> Hello World! </p> 
<%] %>

In the file template_with_children.stpl which is used to render TemplateWithChildren:

<div>
     <%^%>
     <p> <%= field_of_struct %> </p>
</div>

This would then render:

<div>
     <p> Hello World! </p>
     <p> Bye </p>
</div>

is there interest for this feature? This seems as something very useful to make sailfish a lot more flexible. If there is interest for this i would be willing to try implementing this. There are obviously some questions left on how this should be implemented, but i already have some ideas for this.

Thanks :)