Thursday, 2 February 2012

What's My Base URL? How to use a relative path on an image or file in magento!

Simple question really. Lets say you have an image file in:

/home/you/magento/media/myimage.jpg

Now, http://www.you.com points to magento/

But we all know that absolute links like
<img src="http://www.you.com/media/myimage.jpg"> 
are naughty

so you really want to say is

<img src="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);?>media/myimage.jpg">

And I just gave it away.

So here's the lowdown for an html file in magento.

Mage::getBaseUrl is the magento root folder.

Mage::getSkinUrl and Mage::getImageUrl should also work, to the skin and mediaimages folders respectively.

(note the "should" - I havent tried them yet!)

Note this only works in html files, for the scripty type files you would use something else, like
$this->getSkinUrl()
Or thats what I'm guessing.

1 comment: