Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6147011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:02:32+00:00 2026-05-23T19:02:32+00:00

I have an interesting problem. I currently have a basic template library that renders

  • 0

I have an interesting problem. I currently have a basic template library that renders out a bunch of modules for header and footer templates, then sandwiches a view I specify in between them.
Example:

$this->load->view('header.php', $headerstuff);
$this->load->view($contentView);
$this->load->view('footer.php', $footerstuff);

The problem is that I need to put some javascript (that is specific to each content view) into the header. I have been doing this with a switch statement containing the js inside the template library. But that makes no sense in the mvc model.

Example (of what I’ve been doing), (in template library, above previous code):

$headerstuff['js'] = '';
switch ($contentView)
{
    case 'main':
        $headerstuff['js'] = 'JAVASCRIPT INCLUDE CODE 1';
        break;
    case 'product':
        $headerstuff['js'] = 'JAVASCRIPT INCLUDE CODE 2';
        break;
}

I can’t think of another way to do this though. I would like to (ideally) store the js in a variable inside the content view file, and (somehow) load that into the header view. To be honest though, I don’t even think that is possible.

Does anybody have a better way of doing this then my current solution?

Thanks,
Max

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:02:33+00:00Added an answer on May 23, 2026 at 7:02 pm

    I created a helper file to do this for my sites and I think we have a similar MVC template layout:

    Asset Helper:

    <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    if ( ! function_exists('css'))
    {
        function css($array) {
            // If the object passed is a string, convert it into an array
            if ( is_string($array) ) {
                $array = explode(" ", $array);
            }
    
            // Add additional CSS Files
            if ( isset($array) ) {
                foreach ( $array as $i => $file ) {
                    // If it's not the first one add a tab character.
                    if ( $i > 0 ) echo "\t";
                    echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"". $file ."\">\n";
                }
            }
        }
    }
    
    if ( ! function_exists('js'))
    {
        function js($array) {
            // If the object passed is a string, convert it into an array
            if ( is_string($array) ) {
                $array = explode(" ", $array);
            }
    
            // Add additional JavaScript Files
            if ( isset($array) ) {
                foreach ( $array as $i => $file ) {
                    // If it's not the first one add a tab character.
                    if ( $i > 0 ) echo "\t";
                    echo "<script src=\"". $file ."\"></script>\n";
                }
            }
        }
    }
    

    This does one of two things. I will allow you to add files in the controller file which is nice. I do this by creating a data object:

    $data['css'] = array('/path/to/styles.css', '/path/to/otherstuff.css');
    $data['js'] = array('/path/to/javascript.js');
    

    Then in your header include do the following:

    <?
        $defaultCSS = array("/assets/css/global.css");
        $css = (isset($css)) ? array_merge($defaultCSS, $css) : $defaultCSS;        
        css($css);
    
        $defaultJS = array("/assets/js/global.js");
        $js = (isset($js)) ? array_merge($defaultJS, $js) : $defaultJS;
        js($js);
    ?>
    

    I’m settings some defaults that will load on each page and then I can add in different files based on which controller I’m loading.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

He're an interesting problem that looks for the most Pythonic solution. Suppose I have
I have an interesting problem with XSL. We have an XML Input file that
I have run into an interesting problem. I am currently developing php page and
Here's an interesting problem, I have a list of users that I list in
I have an interesting problem and I was looking for a solution; Hoping that
I have an interesting problem and would appreciate your thoughts for the best solution.
I have one interesting problem. I must parse mail body (regular expression), get some
I have a very interesting problem on my LinqToSql model. On some of my
So I came across an interesting problem today. We have a WCF web service
In some library code, I have a List that can contain 50,000 items or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.