Magento – Magento when calling a static block’s content via AJAX, images entered in CMS with {{ }} are not processed into their filename

ajaxcmsimagestatic-blocktemplate

I am trying to call a static block via AJAX. I can retrieve the text fine, however images are not processed from their {{ }} template format to the actual image path and come accross as they are written in the CMS.

{{media url="wysiwyg/groups-scenes-associations-c-296_367.jpeg"}}

This is not the case when I reference the block normally in a CMS page or phtml file, only when I get the block via this AJAX method.

So I am basically making a GET request to a script I have created, which launches an instance of Magento, calls the block HTML and echos it out to return to the original calling JavaScript – like so:

Script handing AJAX request:

require_once('../app/Mage.php'); 
umask(0);
Mage::app();

$block_identifier = 'delivery_country_' . $_GET['iso_country'];

$staticBlock = Mage::getModel('cms/block')->load($block_identifier);
echo $staticBlock->getContent();

Content of CMS static block

<div class="delivery-panes">
<div class="delivery-left-pane"><img src="{{media url="wysiwyg/groups-scenes-associations-c-296_367.jpeg"}}" alt="" /></div>
<div class="delivery-right-pane">
<h2>France</h2>
<p>FR Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean dignissim tempus vestibulum. Quisque tempor velit sed enim hendrerit vehicula. Donec egestas iaculis risus in bibendum. Ut est nisi, semper ac adipiscing ut, hendrerit faucibus metus. Nam mollis velit non purus congue sagittis. Phasellus sit amet felis in leo ultricies vehicula sit amet nec libero. Sed ultricies metus et quam scelerisque auctor. Vivamus et consequat orci.</p>
</div>
</div>

Full response of script

<div class="delivery-left-pane"><img src="{{media url="wysiwyg/groups-scenes-associations-c-296_367.jpeg"}}" alt="" /></div>
<div class="delivery-right-pane">
<h2>France</h2>
<p>FR Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean dignissim tempus vestibulum. Quisque tempor velit sed enim hendrerit vehicula. Donec egestas iaculis risus in bibendum. Ut est nisi, semper ac adipiscing ut, hendrerit faucibus metus. Nam mollis velit non purus congue sagittis. Phasellus sit amet felis in leo ultricies vehicula sit amet nec libero. Sed ultricies metus et quam scelerisque auctor. Vivamus et consequat orci.</p>
</div>
</div>

Why in my AJAX approach would the images be returned as the unprocessed {{ }} string above, rather than being processed into their proper path and filename? i.e. turning {{ }} into the image file name.

Best Answer

This is your problem: echo $staticBlock->getContent();. This will return the column content as it is saved in the data base. What you need is this:

require_once('../app/Mage.php'); 
umask(0);
Mage::app();

$block_identifier = 'delivery_country_' . Mage::app()->getRequest()->getParam('iso_country');
$staticBlock = Mage::app()->getLayout()->createBlock('cms/block')->setBlockId($block_identifier);
echo $staticBlock->toHtml();
Related Topic