Jump to content

[solved] Am I missing something with WireArray::each() ?


bernhard
 Share

Recommended Posts

Here are the docs: https://processwire.com/api/ref/wirearray/each/

I simply want to create a footer-menu... nothing fancy, but I thought instead of foreach I'd use the each() method... but some details are missing for this use: The method can return a concatenated string or an array of items. What I'd need - or at least what I think what I'd need - is that it returns an array of generated strings.

The Problem:

$pages->each("<a href='{$url}'>{$title}</a> | ");

Results in Item 1 | Item 2 |

If it returned an array with those strings I could simply do this:

echo implode(" | ", $items);

Using this syntax does also not work:

$pages->each(["<a href='{url}'>{title}</a>"]));

As it returns this crazy array where I can't use implode() either:

7no51L3.png

 

My traditional way of doing this would be this:

$del = "";
foreach($uk->home->rocktheme_footermenu as $p) {
  echo "$del<a href='{$p->url}'>{$p->title}</a>";
  $del = " | ";
};

But I think this would be nicer and feel more like PW:

echo implode(" | ", $uk->home->rocktheme_footermenu->each("<a href='{url}'>{title}</a>"));

 

Am I missing anything or would it be nice to have the Option of returning a simple array of strings?

Link to comment
Share on other sites

Thx again! I knew there was such a possibility but was looking all over the wrong spots ?

echo $uk->home->rocktheme_footermenu->implode(" | ", function($p) {
	return "<a href='{$p->url}'>{$p->title}</a>";
});

Now it feels like PW again ?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...