Dynamic headers in my header.php encoder

I have header.php. I load my controllers for each page. However, I want to have dynamic headers for each page. My idea was to pass the $ title variable to the view when I load it:

//Home Controller function index() { $data['title'] = "Dynamic Title"; $this->load->view('header', $data); $this->load->view('layouts/home'); $this->load->view('footer'); } 

and then check the $ title variable in my header.php

 <title> <?php if ($title) { echo $title; } else { echo 'Default Title'; } endif; ?> </title> 

However, this does not work, and I get a blank page. I think this is my syntax for header.php, but I can not understand why.

+4
source share
2 answers

Well, I would try to make var dump $ title in the view, just to see if it will be passed at all.

In addition, you do not need "endif;" since you end the if statement with the last curly brace.

+2
source

Correct if Syntax

Your if-statement syntax is a bit off. You can use either:

 if (condition) { // do a } else { // do b } 

or

 if (condition) : // do a else : // do b endif; 

It seems you transfer the end of the last to the first.

Use ternary operator in title

Once you make this change, your title can be printed as easily as:

 <title><?php echo isset($title) ? $title : 'Default Title' ; ?></title> 

Alternative view Download

Another method of loading views is to work with a single template file:

 $data['title'] = 'Foo Bar'; $data['content'] = 'indexPage'; $this->load->view('template', $data); 

This loads the template.php file as your view. In this file you download the following parts:

 <?php $this->load->view("_header"); ?> <?php $this->load->view($content); ?> <?php $this->load->view("_footer"); ?> 

This is not necessary, but it can help you keep the brevity in your controller.

+7
source

All Articles