Jump to content

Function or render


mel47
 Share

Recommended Posts

Hi,

I'm not sure to know how I should structure my template. I have two pages : publication.php and member.php. I want to display publications of the specific member on his page. What is the difference between using a function or render the template?

For now I have (publication.php):

function renderPubli ($page) {

$out = '';

	foreach($page->author as $a) {   // PHP Warning: Invalid argument supplied for foreach() in .../sites/dev-wire/site/templates/publication.php:8
			$members = wire('pages')->find("template=member, name|alias.title={$a->name}, include=hidden");  // membres qui ont comme titre
			$tous = [];
				foreach($members as $m) { 
						$tous[$m->title] = $m;
					}
			if($members->count()){   //si une page author existe
				foreach($tous as $m) { 
						$out .= "<a href='{$m->url}'> {$a->title}</a>, ";
				}
			}	
$out .= " <span> ({$page->year}). {$page->titre}</span> <em>{$page->journal} </em>";

.......
	return $out; 
}

 

and member.php

	foreach($publis as $p) {
					$out .= "<li>" . renderPubli($p) . "</li>"; 
				}

It is working, but do I should care about this PHP warning on member's page coming from publication's page? Do I could do something better? Does with render() I will have a better outcome (for now, no success)?

Thanks
 

 

 

Link to comment
Share on other sites

It seems to me that $page->author is referencing a single Page and thus not an PageArray you can loop through, please check the author field configuration, on the Details tab of the field, you can select the field type value.

Also, I don't see any other difference rather than having (to my opinion) a better organised code base with the render() function. For example, you could keep your publication.php containing only the markup that makes up an author, and do the render() inside the loop. Another way I have found useful is to use wireIncludeFile() which accepts a second parameter which could be the actual author page found by a selector in case I don't want to actually assign a template file to a template.

 

 

 

 

 

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