Passing variables into a template

Hello, I’m following along with PHP & MySQL Novice to Ninja, and attempting to adapt it to build a business website with a blog page. On page 484 it describes how to add a Log Out button, and adds it to the layout.html.php template.

However I would like to add it to one of the individual page templates (the equivalent of joke.html.php in the books example).

So I’m wondering how you can pass a variable into this page template?

To achieve passing the variable into the main layout:

   if (isset($page['variables'])) {
        $output = $this->loadTemplate($page['template'], $page['variables']);
    else {
        $output = $this->loadTemplate($page['template']);

    include  __DIR__ . '/../../templates/layout.html.php';


the include is replaced with

    echo $this->loadTemplate('layout.html.php', ['loggedIn' => $authentication->isLoggedIn(),
		                                             'output' => $output,
		                                             'title' => $title, 'title' => $title ]);

And the individual page template is called as follows (in a public function called list in Joke.php)

 return ['template' => 'jokes.html.php', 
			'title' => $title, 
			'variables' => [
					'totalJokes' => $totalJokes,
					'jokes' => $jokes

I’ve tried a largely trial and error attempt at putting the LoggedIn condition into the ‘variables’ array, with no luck

Here is the code from the book at that point:

And here is my adaptation:

I am not sure what you are having trouble with. As stated in the book, loadTemplate takes a page and the variables you want to “inject” into that page template, which is then included as the $output variable inside layout.html.php. So when you do the code in your first example there, you ARE putting the variables into a given page and in turn that page is being included into layout as the $output variable.

Are you trying to put those variables into the layout file itself? For that all you have to do is define your variables before you include the layout.html.php file so it can see the variables when it is included.

If that is not what you are looking for, maybe offer some more information? :slight_smile:

I’m trying to put the login button on just one page on my website (i.e on an individual page template), as opposed to on every page (as the book describes by putting it in the layout template). The layout and individual templates are called in a different manner, and I can’t figure out how to pass variables into the individual page template where I’m putting the Login button. This page is the equivalent of joke.html.php, but in my case is called blog.html.php

I managed to figure it out, by copying how $totalJokes is included as a variable:

    public function list () {
     $totalBlogs = $this->blogsTable->total();
    $loggedIn = $this->authentication->isLoggedIn();

    return ['template' => 'blog.html.php',
            'title' => $title,
            'variables' => [
                'totalBlogs' => $totalBlogs,
                'blogs' => $blogs,
                'loggedIn'=> $loggedIn,
                'userId' =>$author['id'] ?? null

Yeah and that is how it was always done. Sure you are returning an array of variables, but you are then taking those variables and injecting them into a single page through the layout right? The setup goes that a page is within the layout’s $output variable. You can either create a variable for the layout itself by defining a variable before including the layout.html.php so that the layout can see it, or you use loadTemplate (which is for an individual page) and include the variables in the array of parameters as the second argument.

Not sure what was different here. Glad it worked out for you in the end. :slight_smile: