/   /   /  Magento 2 Frontend : what about directives view media and store ?


For more extensions and themes visit our store

Magento 2 Frontend : what about directives view media and store ?

In Magento 2 the directives is almost like Magento 1 in global idea but with new structure , it read from other location and use kind of processing.    
The big different I will extract the paragraph of bug from documentation of code :

  * @bug: the "_absolute" key is not supported by underlying services
         * probably this happened because of multitude of refactorings in past
         * The original intent of _absolute parameter was to simply append specified path to a base URL
         * bypassing any kind of processing.
         * For example, normally you would use {{view url="css/styles.css"}} directive which would automatically resolve
         * into something like http://example.com/pub/static/area/theme/en_US/css/styles.css
         * But with _absolute, the expected behavior is this: {{view url="favicon.ico" _absolute=true}} should resolve
         * into something like http://example.com/favicon.ico
         * To fix the issue, it is better not to maintain the _absolute parameter anymore in undrelying services,
* but instead just create a different type of directive, for example {{baseUrl path="favicon.ico"}}

Ok here I will introduce some  directive and use case :

1 - {{view url=images/logo.svg}} I will use inside tag img in new cms page :
<img src="{{view url=images/logo.svg}}" alt="" /> the resoult :

  1. <img alt="" src="http://mag2.com/pub/static/frontend/Magento/blank/en_US/images/logo.svg">

See is very cool  , Magento 2 use the path after  processing not using the absolute path …
Ok now I will use the same  directive but I add new store view germany de_DE from backend and we change the language to Germany in frontend we have as result :

  1. <img alt="" src="http://mag2.com/pub/static/frontend/Magento/blank/de_DE/images/logo.svg">

Note if you have any result after add new store view you need deploy this new language with :
 php bin/magento setup:static-content:deploy de_DE

Magento 2 build the URL takes into consideration   kind of processing according to store view and the current theme if you see is blank …

What happen if I want an image from specific module inside my  theme like Magento_Catalog , simple use this {{view url=Magento_Catalog::images/logo.png}} the result is :

  1. http://mag2.com/pub/static/frontend/Magento/blank/en_US/Magento_Catalog/images/logo.png 

Note : you can use the same technique for css and js : {{view url="css/styles.css"}}
the result is  :

  1. http://mag2.com/pub/static/frontend/Magento/blank/en_US/css/styles.css

2 - {{media url=test/logo.svg}} now we read from media folder pub/media example :
<p><img src="{{media url=test/logo.svg}}" alt="" /></p>
after you create your folder test inside pub/media and add  logo.svg the result is :

  1. <img alt="" src="http://mag2.com/pub/media/test/logo.svg">

3 - {{store url="translation/ajax/index"}} give you the URL of current store the result for my case is  :

  1. http://mag2.com/index.php/translation/ajax/index/ 

This is all


Related Posts

make your store more efficient

Solving problems. With open source technology. Professional results. That’s what makes Ibnab your best choice

IBNAB is a company made of a group of professionals whose work is providing secure open source solutions. Our company strives for reaching magnificent results with each experience and provides professional open source solutions that cover every part of the business process.