pro78
pro78

Reputation: 75

PHP include header and footer with different plugins on some pages

I am building a website with php and it has 20 pages. In the homepage I would like to have a slider and some other plugins which I include them in the head. In the footer I link also some JS files. When we use php we have header.php and footer.php Some of the plugins are not needed on some pages but the header.php and footer.php is included on those pages. Is there a way to remove those plugins on those specific pages? or I have to include everything even if they are not needed? Also I would like to add some other plugins in some of the pages that are not needed in the Home Page. Should I create multiple header.php and footer.php and include them when necessary? What is the solution to this problem?

Thanks.

Example:

Home Page those are needed:

Header.php
<link rel="stylesheet" href="plugin01.css">
<link rel="stylesheet" href="plugin02.css">
<link rel="stylesheet" href="plugin03.css">
<link rel="stylesheet" href="plugin04.css">

Footer.php
<script src="plugin01.js"></script>
<script src="plugin02.js"></script>
<script src="plugin03.js"></script>
<script src="plugin04.js"></script>


Random Page:

I need only

header.php 
<link rel="stylesheet" href="plugin01.css">
<link rel="stylesheet" href="plugin02.css">

footer.php
<script src="plugin01.js"></script>
<script src="plugin02.js"></script>

Upvotes: 0

Views: 739

Answers (3)

Ofir Baruch
Ofir Baruch

Reputation: 10346

You'll have to define for each page its required plugins (resources). In the header and footer files, simply loop through that array and call the resources.

It's a bit more work but it's a better organised method and in case you'll have in the future a new page with other plugins, instead of writing patched, you'll easily update it using this mechanism.

PageX.php

//$this_page = 'pageX';
$plugins = array(
 'css' => array(
   'pluginName1' => 'plugin-name-1.css',
   'pluginName2' => 'plugin-name-2.css', 
 ),
 'js' => array(
   'pluginName1' => 'plugin-name-1.js'
 )
);

require_once 'header.php;
//....
//...
require_once 'footer.php';

Header.php

foreach($plugins['css'] as $name => $resource_url){
  echo '<link rel="stylesheet" href="'.$resource_url.'">';
}

Footer.php

foreach($plugins['js'] as $name => $resource_url){
  echo '<script src="'.$resource_url.'"></script>';
}

Upvotes: 2

Latheesan
Latheesan

Reputation: 24116

What you can do is create a helper function to assist with loading required assets and then have a common header.php / footer.php like this:

helpers.php

<?php

function loadStyles($assets = array()) {
    foreach ($assets as $asset) {
        echo '<link rel="stylesheet" href="'. $asset .'.css">'."\r\n";
    }
}

function loadScripts($assets = array()) {
    foreach ($assets as $asset) {
        echo '<script src="'. $asset .'.js"></script>'."\r\n";
    }
}

?>

Then, update your header.php like this:

<?php

// Load required stylehseets for this page 
if (isset($styleAssets)) {
    loadStyles($styleAssets);
}

// Rest of your common header stuff here

?>

Then do something similar with footer.php like this:

<?php

// Load required scripts for this page
if (isset($scriptAssets)) {
    loadScripts($scriptAssets);
}

// Rest of your common footer stuff here

?>

Then you put it all together like this. For example, this is your "home.php" page:

<?php

// Load helpers
require 'helpers.php'

// Define required styles for this page
$styleAssets = array(
    'plugin01',
    'plugin02',
    'plugin03',
    'plugin04'
);

// Load header
require 'header.php'

// Rest of the "home" page code goes here

// Define required scripts for this page
$scriptAssets = array(
    'plugin01',
    'plugin02',
    'plugin03',
    'plugin04'
);

// Load footer
require 'footer.php'

?>

Now, for another random.php page, you simply re-use the script like this:

<?php

// Load helpers
require 'helpers.php'

// Define required styles for this page
$styleAssets = array(
    'plugin01',
    'plugin02'
);

// Load header
require 'header.php'

// Rest of the "random" page code goes here

// Define required scripts for this page
$scriptAssets = array(
    'plugin01',
    'plugin02'
);

// Load footer
require 'footer.php'

?>

Upvotes: 2

Aiyaz Khorajia
Aiyaz Khorajia

Reputation: 598

In your Header.php you can check for the current page.

if( basename($_SERVER['PHP_SELF']) == 'homepage.php')
{ ?>
    <link rel="stylesheet" href="plugin01.css">
    <link rel="stylesheet" href="plugin02.css">
    <link rel="stylesheet" href="plugin03.css">
    <link rel="stylesheet" href="plugin04.css">
<?php 
}

Upvotes: 1

Related Questions