When you include your header in PHP, how do you add things?

I create one of my first PHP sites and just play with it. I am going to use PHP, so I only need to change my metamaterial once. I currently have everything from doctype to </head> .

A friend says that I should not include </head> in header.php, but instead include it on every page, so I can add material specific to the page. Is this a good way to do this?

Currently, for things like the headline, I did

 <title><?php echo $page_title; ?> 

and then at the top of each page, I did

 <?php $page_title = 'Blah'; ?> 

How can I add a page specific Javascript file, if so, how do I do it?

+4
source share
4 answers

What happens here, you are trying to inject logic into your templates, which is not necessarily wrong, but it creates more confusing and complex code. This problem is associated not only with the <title> tags, but will continue as your pages become more and more dynamic and complex (dynamic navigation, page layouts, etc.).

The MVC approach perfectly solves this problem. MVC consists of models that talk to your data sources (MySQL, Redis, whatever) and process your logic; Views that display HTML and controllers, which are a kind of glue between models and views. Each request that the user makes is ultimately directed to the controller, and then an action method is applied in your controller (for example: /users/login can be displayed on the User controller and the login action).

You must specify the page title (or any other meta-information) in the action method in your controller, which knows that this request, but is called before rendering the view:

 $request->setTitle('Home page'); 

And then, in your opinion, just do it:

 <title><?php echo $request->getTitle(); ?></title> 

If you are just starting PHP, this is a great time to learn MVC, because it will help you find good habits that affect not only your PHP development, but also any other development.

I recommend you check out CodeIgniter for its simplicity and excellent documentation. I used CodeIgniter for a while, but ended up writing my own frameworks that fit my needs better.

+6
source

I don’t know how you grow your pages. But on a simple site, you can use an associative array as follows:

 $pages = array( 'home' => array( 'title' => 'Home', 'metaDescription' => 'This is the home of my website', 'metaKeywords' => 'A,B,C', 'scripts' => '<script src="src/js/com.jquery/1.7/jquery-1.7.min.js"></script>', 'content' => 'home.phtml' ), 'page_1' => … ); 

And what can you recall:

 <head> […] <title><?php print $pages['home']['title']; ?></title> […] <?php print $pages['home']['scripts']; ?> […] </head> <body> <?php require '/pathToIncludes/' . $pages['home']['content]; ?> </body> 

But I would recommend this only for a site with a number of pages.

+2
source

I think you should do some logic first and then visualize it. Something like that:

 <?php $page = array( 'title' => 'Title', 'js' => array( 'jquery.min.js', ), 'copyright' => 'Copyright 2012', ); if ( $needed ) { $page[ 'js' ][] = 'some-other.js'; } include( 'header.php' ); include( 'content.php' ); include( 'footer.php' ); // header.php <html> <head> <title><?= $page[ 'title' ]; ?></title> <? foreach ( $page[ 'js' ] as $js ) { ?> <script src="<?= $js"></script> <? } ?> </head> 
+2
source

Here is how I solve this problem:

  • start.php connects to my database, includes my classes, sets all of my default metadata to the $page object and runs $_SESSION
  • header.php Splashes out all my top html content (doctype, <head> , etc.) based on the values ​​in $page
  • top.php Includes start.php and header.php

For most files, the default values ​​are fine. I just turn on top.php . For special occasions when I need special values, my page looks like this:

 <?php include("start.php"); $page->title = "My special title"; include("header.php"); // The rest of my content ?> 

This has the added benefit that pages can access $ _SESSION and other custom classes and use this information to change the header without getting a header already sent error. For example, you can check some user input and redirect them with location('header: foo.php'); or change the title based on their login status.

For a specific javascript for the page, I will create an array of URLs to include $page->javascript , and then put something like: $page->javascript[] = "http://example.com/js/myScript.js"; between start.php and header.php

+1
source

All Articles