Confused about the best way to include latte files within a foreach loop

TerryTibbs
Member | 2
+
0
-

Hello people I am hoping that you can help me with the best approach to this simple problem.

I have a latte file that loops multiple items (mainpage.latte) and a latte file that contains one item (singlepage.latte). I would like both of those files to include the same child latte file (section.latte) that will contain a simple styled section that I can change an update as needed without having to mess about with the parent latte files

Here is what I have so far

mainpage.latte

// $items
{foreach $items as $item}
	{include '../section.latte'}
{/foreach}

singlepage.latte

// Single $item
{include '../section.latte'}

section.latte

<h1>{$item->title}</h1>
<p>{$item->body}</p>

Now that doesn't work because the foreach loop doesn't work within the mainpage.latte file. It works if I put the loop in the section.latte file but that then throws off things for singlepage.latte

Can anybody tell me what i am doing wrong and possibly provide a solution? I wish to keep it as simple as possible because i will be expanding on it

David Grudl
Nette Core | 7469
+
+2
-

I think that you should pass parameter $item to included template

{include '../section.latte', item: $item}
TerryTibbs
Member | 2
+
0
-

Thanks David, that worked