0

I am working on revamping an older CMS and I am wondering how I should handle things like multiple css and javascript files that only need to be included on certain pages.

Let's say for example I have the following pages:

  • services.php -> requires jquery-1.4.2.min.js
  • index.php -> no javascript required

However, on each of these pages, I am including a file from an includes folder

include 'includes/inc_header.php';

This file contains code like the following:

<!doctype ...>
<html>
<head>
<script type="text/javascript">...

Here is where I run into the problem. I have thought about using an array and looping through and printing each element required. This way each page could simply add the scripts they need to the array. I thought this might get very complicated very fast however.

Can anyone give me a good approach to solving this problem?

2
  • How many files are you talking? and how much do they vary from page to page? is it just your index that is significantly different? How big are the files? do they vary greatly? could you combind them into single files? Commented Nov 25, 2010 at 17:34
  • There are probably around 30 files or so at the moment, but the number will go up over time. The types of scripts can vary at times. There are a few files included all the time no matter what, but then we use jquery on certain pages, and specific jquery plugins only on a few pages like jqueryui or plupload for example. Commented Nov 25, 2010 at 17:38

2 Answers 2

1

Well depending on how much work you wanna do i would reverse the view processing and move to a Front Controller that uses a 2-step view. This way your outer layout with the overall structure (doctype, head, etc.) is rendered last, allowing you to modify the output for this outer layout form within the inner layout.

While this will require potentially intensive restructuring it will give you a lot more flexibility in what you can do. Then again this probably going to be a lot of work so you would have to decide whether it is worth the investment.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 For mentioning "Front Controller", and implicitly the MVC, the most important design patterns in web software.
0

You could reverse your logic on your array script idea

Inside your header output files conditionally depending on a flag

eg. (psuedocode)

array core (scripts and css for all pages) array index (particular index scripts) array page1 (etc)

then

if page1 merge_array core+page1 loop through and output

So your header has all the permutations but is context aware - maybe through a php variable on each page or by passing a variable to the include or reading some url parameter so only loads those required

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.