Welcome Guest, Not a member yet? Register   Sign In
Optionally insert CSS and JS links to the header file
#1

[eluser]igniteflow[/eluser]
Hi, apologies if this is a newbie question. I am using various javascript libraries across my site and I don't want to load all of them for every page. I am passing the variables through the controller methods to a _header.php view which is included in the target view page, for example with something like this

Controller
Code:
function index()
{
        $data['js'] = "js/jquery.js";
        $this->load->vars($data);
        $this->load->view('template');    
}

View
Code:
[removed][removed]

but if the variable is empty the page throws an error. My question is how can I avoid this and what's the best way to achieve this? I know I can use the html helper class to make printing this easier, but I'm more concerned about the blank variables. Thanks
#2

[eluser]danmontgomery[/eluser]
1) lower error reporting to E_ERROR on a production server
2) check if the variable exists in the view before you output it

Code:
<?php if(isset($variable)) echo $variable; ?>
#3

[eluser]Fast Eddie[/eluser]
One way you can avoid the error is to check if the variable is set or empty using the empty() function in php.

Code:
if (!empty($var)) {
    echo '$var is not empty';
}
#4

[eluser]Fast Eddie[/eluser]
noctrum beat me to it Smile
#5

[eluser]Kinsbane[/eluser]
I usually just make the JS & CSS files I need into an array, then pass that to a helper function that loops through the array and outputs the required HTML. If no array is passed, nothing happens.
Code:
function spitJs($scripts = array())
{
    foreach($scripts as $script)
    {
        echo '[removed][removed]';
    }
}

You could even extend this further by adding a case to only output if a certain URI segment matches something, so you could have tailored CSS and JS for different controllers or methods.
#6

[eluser]kirkaracha[/eluser]
I used this in my site template:

Code:
if(isset($stylesheets) && is_array($stylesheets)){
    foreach ($stylesheets as $stylesheet) {
        echo '[link rel="stylesheet" href="/css/' . $stylesheet . '.css"]' . "\r";
    }
}

if(isset($javascripts) && is_array($javascripts)){
    foreach ($javascripts as $javascript) {
        echo '[script src="/js/' . $javascript . '.js"][/script]' . "\r";
    }
}

(I replaced angled brackets with square ones so they show up.)
#7

[eluser]xeroblast[/eluser]
i use it like this in my view

Code:
<?php
// CSS links
if (isset($css)) {
    foreach ($css as $c) {
        echo link_tag('css/'.$c->file)."\n";
    }
}
// JScripts
if (isset($js)) {
    foreach ($js as $j) {
        echo '[removed]file.'">[removed]'."\n";
    }
}
?>
#8

[eluser]Zeeshan Rasool[/eluser]
You can do as you are doing, just assign variable to blank if it is no longer needed for a page
$data['js'] = '';
or if you disable error reporting in index.php then this will not give any error more
#9

[eluser]igniteflow[/eluser]
Thanks everyone, wasn't expecting such a response. Got it working now.
#10

[eluser]Zeeshan Rasool[/eluser]
you are always welcome




Theme © iAndrew 2016 - Forum software by © MyBB