Jump to content

Using $page Functions in Templates


louisstephens
 Share

Recommended Posts

After getting a lot more confident with my php skills, I thought I would try condensing my file structure by using the _func.php in my templates. I thought this would be a great way to cut down on my includes (Currently at around 12). However, I ran into a bit of a snag. From my reading, I understand that $page is not a global variable, and that I could use something like:

$pages = wire('pages');

to get the desired result. However, I must not be using this right :).  Here Is what I have:

function basicPage(){
$pages = wire('pages');
$output = "";
$output .="<div class=\"container\">";
$output .="<div class=\"row\">";
$output .="<div class=\"col-md-12\">";
$output .="<h1><?php echo $page->title; ?></h1>";
$output .="</div>";
$output .="</div>";

$output .="<div class=\"row\" id=\"tinyInfo\">";
$output .="<div class=\"col-md-12\">";
$output .="<ul class=\"about\">";
$output .="<li>{$pages->about}</li>";
$output .="</ul>";
$output .="</div>";
$output .="</div>";

$output .="<div class=\"row\">";
$output .="<div class=\"col-md-12\" id=\"maincopy\">";
$output .="{$pages->maincopy}";
$output .="</div>";
$output .="</div>";
$output .="<div class=\"row\">";
$output .="<div class=\"col-md-12\">";
	include './includes/slider.php';
$output .="</div>";
$output .="</div>";
echo $output;
}

This doesn't seem to be outputing anything into my template. Have I missed a crucial step here, or is the "output" method I have chosen not even a great way to set this up? I understand includes wont work this way, but if anyone has suggestions on this I would gladly like to hear them. Sorry for a post with so many questions, but I am just stumped.

Link to comment
Share on other sites

Instead of echo $output, you must return the variable and in your template, you echo your function.

in _func.php you have your function :

function basicPage(){
	// ...
	return $output;
}

and in your template file you call the function :

<?php

$out = basicPage();
echo $out; 

 

Link to comment
Share on other sites

flydev is right for the basic usage of the function with echo but additionally you define $pages but you did'nt that with $page:

//you have this in your code...
echo $page->title;

so what is $page in this function?

I think the whole idea is to build a function for the output of the basic page template...so i think this is the wrong way...

I use in my templates the delayed output with a _main.php file (and _init.php and _func.php) and the interesting part different layouts on the equal templates...this idea is from @horst and the basic concept could read there:

 

 

so you can split the template logic from the HTML output without to build a custom function for every template....since PW rendering the templates this is just unnecessary.

You could build functions for special rendering that is needed in different templates or all over the place for example:

/**
 * Show last Posts in several templates
 * @var $limit (Int) set the limit of displayed posts
 * @var $headline (string)set the headline of the postlist
 */

function renderArticles($limit = 3, $headline = 'New Articles') {
	//get articles
	$articles = wire('pages')->find("template=article,limit=$limit,sort=-publish_from");
	if (count($articles)) {
	//get the rootpage of the articles
	$article_root = wire('pages')->get(1068);
	//open article list
	$out = '<h4 class="subtitle">'.$headline.' - <a href="'.$article_root->url.'">Overview</a><h4><div class="side-list"><ul>';
	//get list items
	foreach ($articles as $a) {
		// get the image instance of the cropped version 70px x 70px
		$thumb = $a->artikel_bild->size(70);
			$out .= '<li>';
			$out .= '<a href="'.$a->url.'"><img alt="'.$a->headline.'" src="'.$thumb->url.'"></a>';
			$out .= '<h5><a href="'.$a->url.'">'.$a->title.'</a></h5>';
			$out .= '<p>'.$a->article_cat->title.'</p>';
			$out .= '</li>';
	}
	//close list
	$out .= '</ul></div>';
	return $out;
	}
}

Best regards mr-fan

  • Like 2
Link to comment
Share on other sites

A couple of errors in your function...

$output .="<h1><?php echo $page->title; ?></h1>";

The PHP tags shouldn't be here and you cannot echo inside a variable declaration.

 

{$pages->about}
...
{$pages->maincopy}

These don't make sense - maybe you meant $page ?

 

And totally a matter of preference, but I find...

$output = "
<div class='container'>
    <div class='row'>
        <div class='col-md-12'>
            <h1>{$page->title}</h1>
        </div>
    </div>
</div>
";

...more readable than...

$output = "";
$output .="<div class=\"container\">";
$output .="<div class=\"row\">";
$output .="<div class=\"col-md-12\">";
$output .="<h1>{$page->title}</h1>";
$output .="</div>";
$output .="</div>";

 

13 hours ago, louisstephens said:

I thought this would be a great way to cut down on my includes (Currently at around 12)

If you're considering switching to functions instead of includes because of performance concerns I wouldn't bother. It's true that you would avoid some file loads but you'd have to have a lot more includes before this would make a difference worth caring about.

One thing to think about when considering a switch from includes to functions is variable scope. Basically, includes have access to variables defined outside of them but functions do not unless you pass the variable to the function as a parameter. This can be a help or a hindrance depending on your needs.

Lastly, you could consider using $files->render() (aka wireRenderFile). A file rendered this way has access to all API variables and you can pass in an array of your own variables for use inside the file. I'm not 100% clear on the benefits of this over a normal include but I guess it has to do with the isolation of variable scope (to avoid the risk of overwriting variables of the same name in your template).

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