Asset helper?

#1
[eluser]Fred Riley[/eluser]
[ Ok, third try to post - hit Submit Post last 2 times, got a blank form afterwards, no post appearing. This time I'll try a preview first to see if that works. Hmm. ]

What happened to the asset helper? I've spent a frustrating hour simply trying to link a CSS file into a view and in the end had to use the seriously clunky:

<link rel="stylesheet" type="text/css" href="<?php echo base_url() . 'system/application/assets/css/resourcedb.css'; ?>" />

Looking online, there's an article in the CI Wiki about an 'asset helper' at http://codeigniter.com/wiki/Asset_Helper/ but there's no such helper in the latest CI I downloaded and not a mention in the otherwise excellent User Guide. Has this helper been deprecated, and if so in favour of what?

I'm a beginner at CI (though well experienced in PHP) and am still testing and learning, so don't want to sod around with url rewrites in .htaccess files as I'm testing on 3 platforms (Mac, Windoze, Unix), plus it's a pain at this stage. I've been pleased with CI up to now but not being able to simply link to a stylesheet has put a fly in the ointment.

Fred

PS: It took over 15 minutes to register on this forum - is that a fussy captcha, or what? ;-\

#2
[eluser]danmontgomery[/eluser]
Assets should really be kept out of the application folder. Generally, the assets folder will be on the same level as index.php.

That asset helper is a 3rd party contribution, not an official helper included in the codeigniter download. The download to that helper is included on the wiki page you linked to.

#3
[eluser]Fred Riley[/eluser]
[quote author="noctrum" date="1282343384"]Assets should really be kept out of the application folder. Generally, the assets folder will be on the same level as index.php.[/quote]

Ok, thanks, I'd not known that. I've put it at the same level as index.php and it's picked up with:

Code:
<link rel="stylesheet"  type="text/css" href="<?php echo base_url() . 'assets/css/resourcedb.css'; ?>"  />

I still think that's a bit clunky but it'll do.

Quote:That asset helper is a 3rd party contribution, not an official helper included in the codeigniter download. The download to that helper is included on the wiki page you linked to.

Ah, right, that explains why I couldn't find it anywhere - I didn't realise that there were 'unofficial' contributions, though I suppose I should have guessed.

Cheers

Fred

#4
[eluser]danmontgomery[/eluser]
The other option is to use a <base> html tag at the top of <head>:

Code:
<base href="http://www.mydomain.com/"/>

Anything linked to relatively then uses that <base> tag, so:

Code:
<link rel="stylesheet" href="assets/css/style.css"/>
links to http://www.mydomain.com/assets/css/style.css

I generally just use:

Code:
<base href="<?php echo base_url();?>"/>

#5
[eluser]pickupman[/eluser]
You don't even have to use the <base> tag. You just need a leading / before the resource. Prepending the tld as part of the asset URI will create a new DNS request that the browser will need to resolve to know where to find the file. This will load a minor amount of load time.
Code:
<link rel="stylesheet" type="text/css" href="/assets/css/resourcedb.css"  />

Adding the leading slash will always force the link to be relative to the document root of the site. Without the leading slash the path is relative to the last directory in the uri. With CI's uri structure, your css link will work for the home page, but will fail to resolve any page when there is a controller name shown. The browser see the controller name as a directory.

If your project has matured enough, you may find the Carabiner library in my signature helpful. It allows you to combine and minify css and js files.

#6
[eluser]slowgary[/eluser]
[quote author="pickupman" date="1282376650"]Prepending the tld as part of the asset URI will create a new DNS request that the browser will need to resolve to know where to find the file.[/quote]

Are you sure? I agree that you should not include the entire URL as it can also cause issues when you'd like to access that page via SSL since you've already hardcoded the URI protocol in those links. I would think that the browser would cache DNS, however.

#7
[eluser]pickupman[/eluser]
Here's a good sidebar to read. [url="http://developer.yahoo.com/performance/rules.html"]Best Practices for Speeding Up Your Website[/url] Browser will cache DNS request for different time lengths. However the DNS requests may prevent parallel downloading of files.

The beauty of current browsers are the addons. Fire up firebug in FF, or developer tools in Chrome, and check the response times both ways.

#8
[eluser]InsiteFX[/eluser]
From the W3C definition:

The BASE element allows authors to specify a document’s base URI explicitly.
When present, the BASE element must appear in the HEAD section of an HTML document,
before any element that refers to an external source. The path information specified
by the BASE element only affects URIs in the document where the element appears.

Basically this is saying that the Base HREF is the URI of the document you are currently on,
not the entire site.

The Base HREF is how you resolve relative URI’s to the current page (document) you are on.

If you leave out the Base HREF modern browsers will take the page you are on and make it the base,
by default.

InsiteFX


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.