achou11 / log

Personal log
BSD 3-Clause "New" or "Revised" License
2 stars 0 forks source link

how to create proper layout templates? #1

Open achou11 opened 2 years ago

achou11 commented 2 years ago

Based on examples I've seen provided by Bagatto + Temple, there doesn't seem to be a way to define a layout that can be referenced from another template so that it "wraps" it. Instead, you have to piece together fragments and include them everywhere e.g.

# post.temple

{% include top %}

...content...

{% include bottom %}

what i want is something like this:

# base.temple

top content

{{ injected_content }}

bottom content

and then create a template whose content is inserted into the injected_content in base.temple, e.g.

# post.temple

# some reference to the base.temple template above...

I am the middle content that will sit below the top and above the bottom contents
achou11 commented 2 years ago

ah maybe this will help?

https://git.sr.ht/~bakpakin/temple/tree/master/item/test/templates/hop.temple

achou11 commented 2 years ago

This almost works but can't get the :_slot to actually render when building

# base.temple

<html>
  {{ (get-in args [:_slot]) }}
</html>
# log.temple

<p>Some log</p>
# wrapped.temple

{$
(import ./base :as base)
(import ./log :as log) $}

{% (base/render-dict (merge args {:_slot (log/render-dict args)})) %}

Wondering what I'm missing :thinking:

subsetpark commented 2 years ago

Unfortunately, I think the structure you’re looking for isn’t a feature of Temple. I did a bit of searching - I think jinja refers to that usage as ‘template inheritance’. I asked after that feature myself and I think it’s out of scope for Temple.

In practice , I just include a header and a footer separately , rather than a single skeleton with the header and footer content, and a hole called ‘content’. But I could imagine for more complex layouts, that approach might not scale.

Under the hood temple is just string macros… I wouldn’t be surprised if you could magic a way up to do codegen on individual temple templates!

achou11 commented 2 years ago

Unfortunately, I think the structure you’re looking for isn’t a feature of Temple. I did a bit of searching - I think jinja refers to that usage as ‘template inheritance’. I asked after that feature myself and I think it’s out of scope for Temple.

In practice , I just include a header and a footer separately , rather than a single skeleton with the header and footer content, and a hole called ‘content’. But I could imagine for more complex layouts, that approach might not scale.

makes sense! do you think the approach I'm trying in my previous comment may be an option? (assuming I figure out how to actually make it work 😅)

Under the hood temple is just string macros… I wouldn’t be surprised if you could magic a way up to do codegen on individual temple templates!

probably a way of doing it but my lack of knowledge fails me for now 😅

subsetpark commented 2 years ago

makes sense! do you think the approach I'm trying in my previous comment may be an option? (assuming I figure out how to actually make it work sweat_smile)

It might need more metaprogramming. I think at least some of this is happening in runtime when it needs to happen at macro expansion time.

That said, I'm mildly nerdsniped and might try it out this evening!

achou11 commented 1 year ago

@subsetpark was poking around Mendoza and looks like it supports template inheritance using :template! (see section about {{ node }})

https://bakpakin.github.io/mendoza/templating.html

Haven't taken a look at the implementation but maybe something to look at if you feel curious again 😄

achou11 commented 1 year ago

Todo: look into whether pepe's example is what I'm looking for:

https://git.sr.ht/~pepe/neil/tree/master/item/templates/app.temple

https://git.sr.ht/~pepe/neil/tree/master/item/neil/watch/init.janet

https://janet-lang.org/api/spork/index.html#spork/misc/capout