Skip to content

Instantly share code, notes, and snippets.

@yogeshdubey2006
Last active February 28, 2019 06:29
Show Gist options
  • Save yogeshdubey2006/f3ac00d3dcfbbe2ab1a11e2d39cb6672 to your computer and use it in GitHub Desktop.
Save yogeshdubey2006/f3ac00d3dcfbbe2ab1a11e2d39cb6672 to your computer and use it in GitHub Desktop.
Magento 2 useful tricks
-- Call static block in phtml file
<?php $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('static_block_identifier')->toHtml(); ?>
-- In xml layout file
<block class="Magento\Framework\View\Element\Template" name="test_file" template="Magento_Theme::html/test.phtml"/>
-- In cms blocks and cms pages
{{block class="Magento\Framework\View\Element\Template" name="test_file" template="Magento_Theme::html/test.phtml"}}
-- Call from Phtml File:
<?php echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_identifier')->toHtml();?>
-- Call from cms page or block:
{{block class="Magento\Cms\Block\Block" block_id="block_identifier"}}
-- Call from Xml File:
<referenceContainer name="content">
<block class="Magento\Cms\Block\Block" name="block_identifier">
<arguments>
<argument name="block_id" xsi:type="string">block_identifier</argument>
</arguments>
</block>
</referenceContainer>
-- Static Block:
<block class="Magento\Cms\Block\Block" name="HomeDiscover">
<arguments>
<argument name="block_id" xsi:type="string">home_discover_more</argument>
</arguments>
</block>
*********************************************************
*********************************************************
-- Image path
- when you write this code in static block
<img src="{{view url="images/slide-bg.jpg"}}" alt="test" />
on front end it looks like
http://localhost/magento2new/pub/static/frontend/Magento/luma/en_US/images/slide-bg.jpg
Where Magento/luma is package name and theme name, you can replace with package and theme name
So you have to take care for slide-bg.jpg is exist in pub/static/frontend/Magento/luma/en_US/images directory.
- when you write this code in phtml file
<img src='<?php echo $this->getViewFileUrl('images/footer-logo.png'); ?>' alt="Demo">
Syntax seems ok , but in frontend It looks like
http://localhost/magento2new/pub/static/frontend/Magento/luma/en_US/images/footer-logo.png
So you have to make sure that footer-logo.png exist in pub/static/frontend/Magento/luma/en_US/images directory.
- For custom module create a directory structure like Company/Module/view/frontend/web/images/image-icon.png
and in phtml use following code
<img src='<?php echo $this->getViewFileUrl('Company_Module::images/image-icon.png'); ?>' alt="image-icon" width="30" height="25" />
- OR -
<img src='<?php echo $this->getViewFileUrl('Magento_Newsletter::images/envelope.png'); ?>' alt="image-icon" width="30" height="25" />
*********************************************************
*********************************************************
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment