Jump to content

Repeater output is not working (I'm misunderstanding it)


Boost
 Share

Recommended Posts

Hei,

A little help here would be nice. So, I created a repeater field with a body and image fields. I named this repeater field sections. I added the field to my home template and use it on my page normally.

Now on home.php template file, I try to output it like this:

<div id="content">
	<?php foreach($page->sections as $x) : ?>
		<?= $x->body ?>
	<?php endforeach ?>	
</div>

But nothing shows up. Yes, I read the page https://processwire.com/docs/fields/repeaters/

Am I missing something?

Link to comment
Share on other sites

nothing shows up? or do you see

<div id="content"></div>

As in: it's just $x->body that's not outputting anything?

It looks right to me; my code that uses repeater is similar. So to debug I would:

  • check that you see the <div> - it's obvious, but I for one often hammer the keyboard for ages and then realise I've edited the wrong file or such...!
  • use Tracy console on the page to inspect $page->sections and $page->sections[0] and $page->sections[0]->body. Is there data in there?
  • not sure what my next step would be if there was data in there but it wasn't output as you have done. Check the text format filters?

Sorry, probably not much help, but thought I'd chip in. (I'm relatively new here.)

Link to comment
Share on other sites

You say you created the field body in the repeater, but in that case, I would expect it to appear here besides the image field:

image.png.e3edf81a190ff4aa88af70f150e5c4d2.png

To test the foreach, can you change your code to $x->id instead of $x->body to see if the IDs show up?

Link to comment
Share on other sites

1 minute ago, Boost said:

@diogo sorry, but which Id are you talking about? Where can I find it?

Every repeater item is given an ID that you can access inside a foreach with $item->id (or $x->id, in the case of your foreach). You can see on the screenshot that you posted, that the ID of your single repeater item is 1110. This would be just to see if your foreach is correct, but as I pointed out before, the most important is to understand why the body isn't even appearing in the array. You are sure you added the body to the repeater field, and not to the home template itself?

Link to comment
Share on other sites

Ok, let me take a step back here:

My repeater field:
146365730_Screenshotfrom2023-05-3108-49-01.thumb.png.363cdab3029d8f2b06aa94e7e6762b4e.png

My template:
674131713_Screenshotfrom2023-05-3108-49-17.thumb.png.f0bb84d9672ba13fb3f2663383a4dea0.png

 

My page:
829098759_Screenshotfrom2023-05-3108-50-14.thumb.png.53fe4263cd9d747219721d21b5e6a8f3.png

 

And here is my home.php

<?php namespace ProcessWire;

// Template file for “home” template used by the homepage

?>

<div id="content">
	<?php foreach($page->sections as $x) : ?>
		<?= $x->body ?>
	<?php endforeach ?>
</div>

My issue here is that if I changed the output to image, it works:

<div id="content">
	<?php foreach($page->sections as $x) : ?>
		<?= $x->image ?>
	<?php endforeach ?>
</div>

But it doesn't for body 😞

Link to comment
Share on other sites

Have you tried with simpler content for body? Seems like table is stripped from the output. Possibly the body field needs to allow some extra html tags (see body field settings).

Link to comment
Share on other sites

Do you get any result when you output the body unformatted?

<div id="content">
	<?php foreach($page->sections as $x) : ?>
		<?= $x->getUnformatted('body') ?>
	<?php endforeach ?>
</div>
  • Like 1
Link to comment
Share on other sites

Hm, the content is being striped by a text formatter or by some setting in TinyMCE. Do you have any text formatter added in the body field? You could also create a new item in the repeater and write only "test" in the body to see if works with the simplest content possible.

Link to comment
Share on other sites

1 hour ago, Boost said:

Funny, because I don't remember changing any text formatter setting.

And, according to your screenshot, you didn't. The text formatters selector is empty.

Since you have Tracy installed, What do you get in the tracy dumps if you bd() the body?

<div id="content">
	<?php foreach($page->sections as $x) : ?>
  		<?php bd($x->body)?>
		<?= $x->body ?>
	<?php endforeach ?>
</div>

1072646187_Screenshot2023-05-31at14_34_01.png.dc0623198151e6bff24be3363893a9f5.png

Link to comment
Share on other sites

Ok, this is a bit puzzling. The string is there, but empty. If you add more items to the repeater, you get an empty string on all of them? Maybe you could also create a new tinymce field and add it to the repeater to see if it behaves in the same way.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

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