0
foreach ($this->intro_items as $key => &$item) {
    $this->item = &$item;
    echo $this->loadTemplate('item');
}

I would like to be able to change the template used for specific items in the foreach statement. The first 2 $items should use template A, then switch to template B for $items 3,4. Then back to template A.

3
  • thinking this is going to work: foreach ($this->intro_items as $key => &$item) : $this->item = &$item; if (($key/2)%2) { echo $this->loadTemplate('item'); } else { echo $this->loadTemplate('other item'); } endforeach; Feb 17 '18 at 21:38
  • Please provide more details in your questions. What framework or cms (+ version number) you use for example and please consider to wrap code with the given preformat function for code (the curly brackets icon in the editor). Feb 18 '18 at 13:01
  • @ChrisArmstrong If your commented solution works, you should post it as an answer. If it doesn't work, please add it to your question via Edit and explain that that is your best coding attempt, then try to clarify what is left to fix AND respond to Dennis' request for additional details. Please always endeavor to progress your question(s) toward an accepted answer.
    – mickmackusa
    May 27 '18 at 12:55
0

this ended up working for me.

foreach ($this->intro_items as $key => &$item) : 
 $this->item = &$item;
  if (($key/2)%2) {
   echo $this->loadTemplate('item');
  } else {
   echo $this->loadTemplate('item2');
  }
endforeach;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.