1

My custom module xml file contains the block code. I want to call that block code directly using createBlock method and set template to show in specific template. My block code is adding animated fancing lines to form.

If i mention in xml using following format then it works fine. How can i display same block using createBlock method?

<referenceContainer name="form.contact.additional.info">
 <block class="Jackson\Wyss\Block\Frontend\Wyss" name="Wyss-reca" after="-"
 template="Jackson_Wyss::wyss.phtml">
 <arguments>
 <argument name="jsLayout" xsi:type="array">
 <item name="components" xsi:type="array">
 <item name="wyssdata" xsi:type="array">
 <item name="component" xsi:type="string">Jackson_Wyss/js/reca</item>
 <item name="zone" xsi:type="string">contact</item>
 </item>
 </item>
 </argument>
 </arguments>
 </block>
 </referenceContainer>

for example:

 $block->getLayout()->createBlock('Jackson\wyss\Block\Frontend\Reca')->setData('jsLayout', '5')->setTemplate('goodtest/test.phtml')->toHtml();

In above xml code zone is form type name. I want to set different name for all the forms in createBlock method. is there anyway we can createBlock method for above xml code?

How can i make createBlock method of my above xml code?

asked Apr 1, 2019 at 10:51
3
  • You can try using as childblock. Add this block as as child where in you want to call and then call using getChildBlock Commented Apr 1, 2019 at 11:48
  • Thanks for your reply. So if i call it using the childBlock will it consider the paramaters defined under the (in your example)<item name="instant-purchase" xsi:type="array"> <item name="component" xsi:type="string">Magento_InstantPurchase/js/view/instant-purchase</item> <item name="config" xsi:type="array"> I want to make sure that in zone (in my code) applied there. Commented Apr 1, 2019 at 14:35
  • Yes it do. I have used it as mentioned in below code. YOu can try and let me know if in case of any issue Commented Apr 2, 2019 at 5:47

1 Answer 1

0

Try using calling as ChildBlock, like the bellow example:

<!-- Add Instant purchase on Recently viewed products -->
 <referenceBlock name="catalog.product.related" >
 <block name="product.info.addtocart.instantPurchase.additional.recently" class="Magento\InstantPurchase\Block\Button" template="Magento_InstantPurchase::button.phtml" before="-">
 <arguments>
 <argument name="jsLayout" xsi:type="array">
 <item name="components" xsi:type="array">
 <item name="instant-purchase" xsi:type="array">
 <item name="component" xsi:type="string">Magento_InstantPurchase/js/view/instant-purchase</item>
 <item name="config" xsi:type="array">
 <item name="template" xsi:type="string">Magento_InstantPurchase/instant-purchase</item>
 </item>
 </item>
 </item>
 </argument>
 </arguments>
 </block>
 </referenceBlock>

In parent .phtml:

<?php
 if($block->getChildBlock('product.info.addtocart.instantPurchase.additional.recently', true))
 {
 $block->getChildBlock('product.info.addtocart.instantPurchase.additional.recently')->setProduct($_product);
 echo $block->getChildHtml('product.info.addtocart.instantPurchase.additional.recently', false);
}

?>

answered Apr 1, 2019 at 11:52

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.