Using images in packages

Permalink 1 user found helpful
I'm sure this is covered already, but I cannot find the answer - apologies.

I'm extending a package and just want to generate some HTML to use an image that is part of the package. The image is in the folder named
packages/mypackage/blocks/mypackage/images

I just need to generate some HTML that says <img src="imagepath">

But what is the imagepath? Do I need to call getRelativePath on something, or can I just hard-code it in there? If the former, what object do I call getRelativePath on; if the latter, what is the correct path to put in there?

 
ScottC replied on at Permalink Reply
ScottC
You'd want to get the package object usually via the package handle, then pass it to the ConcreteUrlsHelper after loading it and call getPackageURL($pkg); then append your image folder and the image filename itself.
marshallarts replied on at Permalink Reply
Ok, thanks. I can see how to get the package handle, but it's not clear to me how to use it with the URL helper. At the beginning of a custom template handler I've added:

$c = Page::getCurrentPage(); // this was already there

$bks=$c->getBlocks("Main");
$pkh=$bks[0]->getPackageHandle(); // there will only ever be the one

That's where I sort of stalled again. Sorry, this is all uncharted territory for me.
marshallarts replied on at Permalink Best Answer Reply
All done, the code is as follows, in case it is of assistance to anyone else:


// Get the package relative path
$bks=$c->getBlocks("Main");
$pkid=$bks[0]->getPackageID(); // can only do this if only 1 block in 'Main'
$pk=Package::getByID($pkid);
$ppath=$pk->getRelativePath();

I can then put $ppath in my HTML as required.

This may not be the "best" way to do this, but it works.
edward replied on at Permalink Reply
In a package, from within a block's view, you can do

$package = Package::getByID($obj->getPackageID());
$package_path = BASE_URL . $package->getRelativePath() . '/';
$block_path = $package_path . "blocks/" . $obj->btHandle . '/';


to obtain the package path and block path, respectively.