1

I am new to latte template engine and fiddle around with it since some days. I found a lot of nice and usefull things to make my projects easier and cleaner. But there is one thing I did not find or miss an idea how to handle it.

Lets imagine my sites has this basic layout template:

<header><h1>{$title}</h1></header>
<nav n:inner-foreach="$navigation as $link">
<a href="{$link->url}" n:class="$link->active ? active" n:attr="data-icon: $link->icon">{$link->name}</a>
</nav>
<aside>{include aside}</aside>
<aside>{include aside}</aside>
<aside>{include aside}</aside>
<content>{include content}<content>
<footer>{include footer}</footer>

the content is handled within another template for each site. every one of them looks like this:

{layout 'layout.tpl'}
{$navigation[3]->active=true}
{$title="this page title"}

{block content}
    <p>here comes the content</p>
{/block}

{block aside}
    <p>here is f.e. a sidebar</p>
{/block}

{block aside}
    <p>this is some adverticement</p>
{/block}

now my question is this: how can I use one or more blocks of "aside" within my template which are defined as "block". the best solution whould be something like: "block aside[]" and I handle it inside the main template somehow with a loop. Is there an usefull way to do it? I dont want to use it with variables like the navigation because the content is defined within the template.

thx for ideas and greetings Makka

6
  • What do you expect to achieve by having multiple blocks with the same name? Why not just have a single aside block in the child template containing both sidebar and advertisement? Commented Jul 3, 2021 at 22:10
  • because depending on css the aside-blocks can appear left and/or right. html5 allow multiple aside-sections. Commented Jul 3, 2021 at 23:42
  • Sure, but you only have a single aside in the layout. If you want multiple ones, you will need to distinguish them somehow (e.g. by adding a class) and at that point you can just use differently named blocks. Commented Jul 4, 2021 at 0:48
  • that was my question ... how can i do this, but without using another assignment method than every other block. (i editted the first post for better clarity) Commented Jul 4, 2021 at 6:24
  • Well, I still question the benefit of doing that rather than having different blocks with different names. I posted an answer with a hack that achieves what you say you want but that still sounds like XY problem. Commented Jul 4, 2021 at 12:29

1 Answer 1

0

Well, you could do something like, abusing dynamic block names:

{var $maxAside = 10}
{for $i = 0; $i < $maxAside; $i++}
  {ifset aside-$i}
    {include "aside-$i"}
  {else}
    {breakIf true}
  {/ifset}
{/for}

and define blocks like {block "aside-{++$i}"}…{/block} but I would not recommend this because it just adds pointless complexity.

Either, majority of the asides will be the same and then you could have just used a single aside block for them:

{block aside}
  <section>
    <p>here is f.e. a sidebar</p>
  </section>

  <section>
    <p>this is some adverticement</p>
  </section>
{/block}

Or you you will want to be distinguishing them somehow (e.g. styling them differently) and then you would assign them classes, at which point you can just use different blocks:

<aside class="sidebar" n:ifset="sidebar">{include sidebar}</aside>
<aside class="advertisement" n:ifset="advertisement">{include advertisement}</aside>

Unless you are matching them using :nth-child CSS selector. But that is fragile because you would now need to match the order to the one defined in the CSS.


The problem only starts to become interesting when you have a hierarchy of pages where the pages inherit asides from pages in parent layers. But then it is still nicer to use template inheritance.

Sign up to request clarification or add additional context in comments.

6 Comments

The first part of your answer seems to be what I'm looking for. Thanks a lot for this. Can you explain to me what you mean by "pointless complexity"? At first glance, I don't see any great problems.
The second part is also a nice solution that I had already thought of. I just define a set of names for the asides and treat them as normal blocks. But then I hear my customer asking: "I still need an area named X, Y and Z". Or maybe that can be done simply by giving him an editable list of names.
The main complexity is the need to manually increase the counter. Also it will likely be less efficient than just having a static list of blocks (but probably not too much, would have to measure).
Editable list of names would be nice since you could just do <aside n:foreach="$asides as $asideName" n:class="$asideName" n:ifset="$asideName">{include $asideName}</aside>, that is probably the easiest solution.
Alternately, you could define custom aside tag, using the block macro as a starting point.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.