CodeIgniter Forums

Full Version: Asset_Helper For Newbie to setting up external CSS
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
Hello guys,
I'm newbie and i cannot find detail step by step guide how set up asset_helper until it works load my css. i used code igniter ver.1.7.2.
I downloaded the asset_helper from:
asset_helper.zip

This is the settings of my codeigniter in my personal computer :
1)I build my application at http://localhost/codeigniter/
2)I have set system/application.config/config.php like this
$config['base_url'] = "http://localhost/codeigniter";
$config['index_page'] = "";

3)I have set system/application.config/routes.php like this
$route['default_controller'] = "hello";
4)I have used .htaccess placed in the root where there are placed together with some files and folders like index.php,system,user_guide and license.txt.
my content of .htaccess file is:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js\robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

5)In my CONTROLLER, i make a file hello.php
so it becomes (codeigniter\system\application\controllers\hello.php),
the code are:
Code:
<?php
class Hello extends Controller {
    
   function Hello ()  //Constructor
   {
     parent::Controller();
  [b]$this->load->helper('asset');[/b]        
   }
    
   function index()
   {        
   [b]css_asset('css/style.css');[/b]
        
     $data['message'] = 'This is for the html body sent from controller';
     $this->load->view('hello_view',$data);
   }
}
6)In my VIEWS, i make a file hello_view.php
so it becomes (codeigniter\system\application\views\hello_view.php),
the code are:
[code]
<html>
<head><title>Title</title>
</head>

<body>
<h1> This is Body Title with External CSS placed in assets</h1>
<p>&lt;?php echo "Hello, this is made with php ",$message ?&gt; </p>
&lt;/body&gt;
&lt;/html&gt;
/code]

There is no CSS RESULT OF ABOVE CODE when i try in http://localhost/codeigniter/

7)I build a folder asset in the root, together with other files like .htaccess so the structure like:
Folder Structure
assets/
—css/style.css
—image/
—js/

Questions:

1)What must i setting to make my external CSS runs ?
2)what file that the code must i change to? please show me how..

Thank you guys for the help, maybe my question is so basic...
i hope many newbies can be helped through this question too.

El Forum

[eluser]Unknown[/eluser]
Why no one help me :down:

El Forum

[eluser]Matthieu Fauveau[/eluser]
Hi vinexar,

You are in luck 'cause I'm in an helping mood (while I wait for someone to pick me up, but that's another story).

The asset helper is only returning the HTML string. When you call : css_asset('css/style.css'), it will return the HTML link tag. You need to get that in your $data array to display it into your view.

In a nutshell :

Controller
Code:
function index()
   {        
     $data['css_asset'] = css_asset('css/style.css');
        
     $data['message'] = 'This is for the html body sent from controller';
     $this->load->view('hello_view',$data);
   }

View
Code:
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;Title&lt;/title&gt;
&lt;?php echo (isset($css_asset)) ? $css_asset : '' ?&gt;
&lt;/head&gt;

&lt;body&gt;
  <h1>This is Body Title with External CSS placed in assets</h1>  
  &lt;?php echo “Hello, this is made with php “,$message ?&gt;  
&lt;/body&gt;
&lt;/html&gt;