Jump to content

showing repeater content from subpages


neophron
 Share

Recommended Posts

Hi,

I'm trying to build a publication page, where the lists of publications are structured like this:

1997
more Publications

1996
Another Publication
Another Publication 01

1995
Publication 01

There is a page – »Publications« with subpages. All subpages from »Publications« exists only as content holders. They're not preview-able. 
Now, I created a template »publication« with a field for the year and a repeater field (»Repeater_publication«).
The repeater contains two fields: »repeater_description« and »repeater_download_file«.

What I'm trying is to echo the whole stuff on the Publications page with a double foreach.

My question is: Is this the right approach? Shall I generate for the publication subpages a template.php?

I tried this, but it doesn't work:
 

foreach($page->children() as $subpage):
		  $publications = $subpage->publication_year();
		  foreach($publications as $publication):
		    echo $publication->Repeater_publication();
		  endforeach;
		endforeach;

 

publication.jpg

Link to comment
Share on other sites

I think you might have a mistake here: 

$publications = $subpage->publication_year();

If I understand your setup correctly that returns a year integer, so looping on that will have no relevant effect. 

I think the final code could look like this:

<?php foreach($page->children() as $subpage): ?> <!-- $subpage are the "year" pages -->
	<h1>Publications on: <?php $subpage->title </h2> <!-- eg. Publications on 1997 -->
	<?php foreach($subpage->Repeater_publications as $publication): ?>
            
			<p><?=$publication->repeater_description?></p>
			<a href="<?=$publication->repeater_download->url?>">Download</a>
      
	<?php endforeach; ?>
<?php endforeach;

 

There is a way to render the repeater field as its own template (or any field for what matters) with a feature called field rendering, you would have to create a template file as: templates/fields/Repeater_publication.php that actually renders some markup when calling the render method on the repeater field reference.

<?php foreach($page->children() as $subpage): ?> <!-- $subpage are the "year" pages -->
	<h1>Publications on: <?php $subpage->title </h2> <!-- eg. Publications on 1997 -->
	<?php echo $page->render("Repeater_publications"); ?>
<?php endforeach;

 

And inside site/template/fields/Repeater_publication.php :

<?php foreach($value as $publication):?>
<p> <?=$publication->publication_year?></p>
<?php endforeach?>

 

Check this blog post for more info on field rendering, to see which variables are available in the template file:

https://processwire.com/blog/posts/more-repeaters-repeater-matrix-and-new-field-rendering/#processwire-3.0.5-introduces-field-rendering-with-template-files

 

  • Like 1
Link to comment
Share on other sites

Hola elabx,
thanks for your advice.

After some debugging, this code finally works:

<?php foreach($page->children() as $subpage): ?>
		<h2><?php echo $subpage->publication_year ?></h2>
			<?php foreach($subpage->Repeater_publication as $publication): ?>
				<p><?php echo $publication->repeater_description ?></p>
				<a href="<?php $publication->repeater_download->url ?>">Download</a>
			<?php endforeach; ?>
	<?php endforeach; ?>

 

My next goal is to create a real download link for pdf or doc files. And the link must be visible, if it's populated.

 

 

Quote

There is a way to render the repeater field as its own template (or any field for what matters) with a feature called field rendering, you would have to create a template file as: templates/fields/Repeater_publication.php that actually renders some markup when calling the render method on the repeater field reference.

What I could't understand is, after creating such a template.php, do I have to tell Processwire somewhere, that this template exists?

 

Link to comment
Share on other sites

1 hour ago, neophron said:

My next goal is to create a real download link for pdf or doc files. And the link must be visible, if it's populated.

 

Doesn't the previous output the link already for download?? You can use an if statement to check if there is an uploaded file:

<?php if($publication->repeater_download):?>
			<p><?=$publication->repeater_description?></p>
			<a href="<?=$publication->repeater_download->url?>">Download</a>
<?php endif ?>
1 hour ago, neophron said:

What I could't understand is, after creating such a template.php, do I have to tell Processwire somewhere, that this template exists?

 

It uses naming convention, the following code will render the file content in site/template/fields/Repeater_publication.php :

(EDIT: Edited some code on previous post, I was mixing Repeater Matrix field rendering with normal Repeaters)

<?php 
//Render the repeater field in the current page
$page->render("Repeater_publication");

And inside site/template/fields/Repeater_publication.php :

<?php foreach($value as $publication):?>
<p> <?=$publication->publication_year?></p>
<?php endforeach?>

Maybe you can save this for later if you already got the rendering almost done with a more common approach :)

  • Like 1
Link to comment
Share on other sites

Hi,

Quote

It uses naming convention, the following code will render the file content in site/template/fields/Repeater_publication.php :

ah, ok, now I'm getting it.

About the download link: I created a field (type: files). The frontend shows me this:

<a href="&lt;br /&gt;&#10;&lt;b&gt;Notice&lt;/b&gt;:  Trying to get property of non-object in &lt;b&gt;/var/www/web27428586/html/processwire/site/assets/cache/FileCompiler/site/templates/publications.php&lt;/b&gt; on line &lt;b&gt;18&lt;/b&gt;&lt;br /&gt;&#10;">Download</a> 

I found this article: https://jensmartsch.de/blog/simple-file-downloads-with-processwire/
I'll try it tomorrow.

 

Quote

Maybe you can save this for later if you already got the rendering almost done with a more common approach

Thanks, I'll do this :-) 

download mess.jpg

  • Like 1
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...