I want to add a new CMS directive to render CDN media URL's just like the media directive:
{{media url="wysiwyg/image.jpg"}}
Will result in http://www.example.com/media/wysiwyg/image.jpg
Now I have some specific images that will be hosted on a separate CDN. I want to be able to render URL's to those images from CMS pages/blocks:
{{cdnmedia url="images/picture.jpg"}}
This should result in http://cdn.example.com/images/picture.jpg
where the CDN base URL would be configurable.
I already have a helper that can take "images/picture.jpg"
as input and will return a full CDN URL (based on configured secure and unsecure CDN bas URL's).
How can I add this into the CMS rendering?
Best Answer
This is how I've done it right now.
Overriding CMS template filter
The CMS Helper contains two methods called
getPageTemplateProcessor()
andgetBlockTemplateProcessor()
which will doMage::getModel(...)
with input of the configured model name from the config respectively atglobal/cms/page/tempate_filter
andglobal/cms/block/tempate_filter
.So add this to your module's
config.xml
to replace the original values ofwidget/template_filter
(set inapp/code/core/Mage/Widget/etc/config.xml
):Sidenotes:
<tempate_filter>
is missing the 'l' in 'template' which is a typo in Magento core<depends>
on<Mage_Widget />
Custom template filter model with 'cdnmedia' directive
And the model:
Rendering the actual URL
The question is how to add the directive, not the actual URL creation, but for completeness, here's the 'mymodule_cdn' helper
getUrl()
method:Usages
You can use the
{{cdnmedia}}
directive now in your CMS pages and static blocks:And because of the use of a helper building the actual URL, you can use this in phtml templates:
Reference
Used this article as reference: http://de.nr-apps.com/blog/2013/01/16/magento-store-url-template-directive/