Skip to content

Instantly share code, notes, and snippets.

@pauleveritt
Created September 8, 2024 17:13
Show Gist options
  • Save pauleveritt/278a93ff441dcd8b68c7e51774cf16bc to your computer and use it in GitHub Desktop.
Save pauleveritt/278a93ff441dcd8b68c7e51774cf16bc to your computer and use it in GitHub Desktop.
Nested template
# We've generally thought of nesting being an interpolation
# that called the html "tag function" (now template function)
def Sidebar(heading: str, items: list[Any]) -> HTMLTemplate:
return t"""
<aside>
<h1>{heading}</h1>
<ul>
{html(t"<li>{item}</li>" for item in items}")}
</ul>
</aside>
"""
# But does it need to do the HTML conversion inside
# the template? Could it instead just nest a template,
# and the outermost `html()` would process it?
#
# A good bit nicer than having an extra html(). But
# do we think the DSL info in the typing would extend
# to nested templates?
def Sidebar(heading: str, items: list[Any]) -> HTMLTemplate:
return t"""
<aside>
<h1>{heading}</h1>
<ul>
{t"<li>{item}</li>" for item in items}"}
</ul>
</aside>
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment