Welcome Guest, Not a member yet? Register   Sign In
How to load the css stylesheets ‘dynamically’ ?
#1

[eluser]saby[/eluser]
Hi
I need some explications
This is my view's structure
Layout.php who included
Header.php
Menu.php
content
Footer.php

In the root, an assets folder with subfolders : css, javascript …

My question:
How to load the css stylesheets ‘dynamically’, i mean
for exemple :
when my content = contact.php, load contact.css
then
when my content = services.php, load services.css

I search since this morning, I tried many things, but i am lost.
thanks a lot
#2

[eluser]the_unforgiven[/eluser]
You could if else statements like so:

Code:
if ($this->uri->segment(2) == 'contact') {
//Load contact.css here
}
if ($this->uri->segment(2) == 'services') {
//Load services.css
}
Just an example to get you started!
#3

[eluser]Aken[/eluser]
You should use a single CSS file, it's better for performance and easier to update.
#4

[eluser]saby[/eluser]
Thanks the_unforgiven you have given me a part of the solution

I 've found a solution a temporary(probably not the best).

In my controller:
Code:
$data['seg'] = $this->uri->segment(1);
and in the header view
Code:
<?php
  if(($this->uri->segment(1)) == false )
   echo link_tag('assets/css/welcome.css');
  else
   echo link_tag('assets/css/'.$seg.'.css');
?>
#5

[eluser]PhilTem[/eluser]
Even faster/nicer

Code:
echo link_tag('assets/css/' . $this->uri->rsegment(1)  . '.css');

which will use the re-routed URI to set the stylesheet. If you don't have routes, it's basically just useful since it is translating empty URIs, i.e., the default_controller to what you set in APPPATH/config/routes.php Wink
#6

[eluser]saby[/eluser]
Thanks PhilTefn
It's a better way ;-)




Theme © iAndrew 2016 - Forum software by © MyBB