Search code examples
phpcodeigniterif-statementheaderhead

Codeigniter: Head & Title Tags with if/else


I am newbie in CodeIgniter and in PHP as well. I am looking for an example to find out how it is worked before recode it to my project. And also you can explain basically if you want without any example. I have been looking for it on the internet but I couldnt find yet. I don't know how to search about this topic on Google I guess.

I have two header.php files that are renamed as header_post.php and header_main.php in "views" folder.

For header_post.php, the meta tags such as description, title and so on are changed based on the the title and the content of the post I published.

For header_main.php, the meta tags such as description, title and so on are stable based on the the title and the description of my blog.

But I want to create a header.php instead of header_post.php and header_main.php. When it is in homepage of the blog the codes I wrote for header_main.php should work and on the other hand when it is in a post's page the codes I wrote for header_post.php should work.

As an example,

Homepage:  <title>Bari's Blog</title>
In a Post: <title>Post's Title</title>

I need your help about how to write if/else statement?


Solution

  • You could check if the variables for setting the header etc. are set:

    <?php if (isset($title) && $title) { ?>   
    <title><?php echo $title;?></title>
    <?php } else { ?>
    <title>Bari's Blog</title>
    <?php } ?>