Jump to content

Completely stuck with a templage coding matter - help appreciated


totoff
 Share

Recommended Posts

Hi Forum,

it seems as if I have a PHP question I'm unable to answer myself. I've tried for almost three hours now but I can't find the answer, why my code isn't working as it should. This is, what's going on:

I have a central _functions.inc with this function:

function renderReferenceList() {
	
	$page = wire('page');
	$pages = wire('pages');
	$page = $pages->get(1065);
	$page = $page->children("limit=15, sort=-date");

	foreach($page as $child) {
		$out = "<div class='referenceTeaser post'>";
		$out .= "<h3 class='articlehead'>{$child->headline}</h3>";
		$out .= "<p>" . $child->summary . "<a href='{$child->url}'><i class='more'>Referenz ansehen</i></a></p>";
		$out .= "</div>";
		echo $out;	
	}

}

Next I have a page template with this code:

<?php
include_once("./_functions.inc");

/**
 * Referenzen template
 *
 */

if ($page->id !== 1065) {
	$content = $page->body;
	$headline = $page->headline;
	$sectionhead = "Projekt";
	$logo = $page->widget_logo;
	$logo = $logo ->single_image;
	$sidebar = "<div class='widget'>" . 
	"<h2 class='sectionhead'>Kunde</h2>" .
	"<img class='logofile' src='{$logo->url}' title='$page->title' alt='$page->title' >" . 
	"</div>";
} else {
	$sectionhead = "Referenzen";
	$content = renderReferenceList();
}

include("./_page.inc");

And I have a _page.inc with this code:

<div id="main" class="grid-66 mobile-grid-100">
	<?php
	echo "<h1 class='sectionhead'>$sectionhead</h1>";
	if($headline) echo "<h2 class='articlehead'>$headline</h2>";
	echo $content;
	?>
</div> <!-- /#main -->

The problem is: The code for the else statement is working perfectly fine, it renders all div.referenceTeaser with the appropriate content, except that it does so on top of my markup directly after the opening body tag and not - as it should - within div#main ...

var_dump($content=== null); returns true but no notice.

What am I doing wrong?

Thanks for your help before I'm going mad about this...

Link to comment
Share on other sites

Try instead of:

echo $out;

this:

return $out;

You are now echo'ing the output within the function which is called before in your template file. With return it is outputted within the function renderReferenceList().

  • Like 2
Link to comment
Share on other sites

It's in the "echo $out" portion -- just assign that to a variable via "return" and echo it later rather than doing it inside the function's foreach loop. (Oops, duplicate of arjen...anyway I think he's right)

Link to comment
Share on other sites

Hello arjen and MarkC,

thanks for answering. Unfortunately that doesn't work for me. It renders only one of several entries, if I replace echo with return. This is what I have now (renders first entry of the loop in the right place):

function renderReferenceList() {
	
	$page = wire('page');
	$pages = wire('pages');
	$page = $pages->get(1065); // Referenzen
	$page = $page->children("limit=15, sort=-date");

	foreach($page as $child) {
		$out = "<div class='referenceTeaser post'>";
		$out .= "<h3 class='articlehead'>{$child->headline}</h3>";
		$out .= "<p>" . $child->summary . "<a href='{$child->url}'><i class='more'>Referenz ansehen</i></a></p>";
		$out .= "</div>";
		return $out;	
	}

}

I also tried

function renderReferenceList() {
	
	$page = wire('page');
	$pages = wire('pages');
	$page = $pages->get(1065); // Referenzen
	$page = $page->children("limit=15, sort=-date");

	foreach($page as $child) {
		$out = "<div class='referenceTeaser post'>";
		$out .= "<h3 class='articlehead'>{$child->headline}</h3>";
		$out .= "<p>" . $child->summary . "<a href='{$child->url}'><i class='more'>Referenz ansehen</i></a></p>";
		$out .= "</div>";
			
	}

	return $out;

}

But that returns only the last post ...

I'm not very experienced with PHP. So if you don't mind an example how it is done right would be greatly appreciated.

Thanks

Link to comment
Share on other sites

I see two problems
 
One, on this line, since this is within the foreach, everytime it gets a new record (page) - i.e. on each new loop, you are wiping out the previous value of $out...hence, in your last code, it will only output the last record having wiped out all previous ones. 

$out = "<div class='referenceTeaser post'>";

The second problem is with your first code; you are returning $out within the foreach. It should be outside the foreach. The idea is that you need to return what you want to finally output/use...So, we don't return anything too early...
 
Try the below code, modified from your second code attempt. We first initialise $out outside the foreach...Subsequently, we concatenate it. In addition, it is not always a good idea to use the same variable name for different things. E.g., you already have $page = wire('page'); In your selector, I would use $children = ...blah blah.

function renderReferenceList() {
        
        $out = '';//initialise the variable $out
	
	//$page = wire('page');//no need for this as well
	$pages = wire('pages');
	//$page = $pages->get(1065); // Referenzen//no need for this here; combined below
	$children = $pages->get(1065)->children("limit=15, sort=-date");//changed variable name to $children

	foreach($children as $child) {
		$out .= "<div class='referenceTeaser post'>";//note the concatenation
		$out .= "<h3 class='articlehead'>{$child->headline}</h3>";
		$out .= "<p>" . $child->summary . "<a href='{$child->url}'><i class='more'>Referenz ansehen</i></a></p>";
		$out .= "</div>";
			
	}

	return $out;

}
Edited by kongondo
  • Like 4
Link to comment
Share on other sites

@kongondo I award you with my personal live saver medal!

I had the understanding that it has got something to do with the string being "reset" on each loop - but I would never had the idea to intialise $out outside the loop and than to iterate over it.

Thanks a lot for bringing me back on track!

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

×
×
  • Create New...