1

Can I add the search box to a template without xml?

I need to create a dropdown with the search box when I click on a magnifying glass icon.

8
  • do you want to call search box in another phtml ?
    – Pawan
    Dec 14, 2018 at 16:44
  • Yes. I just created a custom search bar. I want to make it a dropdown from nav. Dec 14, 2018 at 16:53
  • if you want search box you can call it by: <?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magento_Search::form.mini.phtml")->toHtml();?>
    – Pawan
    Dec 14, 2018 at 17:02
  • let me know if have any issue.
    – Pawan
    Dec 14, 2018 at 17:02
  • When I enter that into the phtml file the DOM states that its loading twice. It get a console error showing duplicate IDs for #search and #search_mini_form. Any idea what is causing that? Dec 14, 2018 at 17:21

1 Answer 1

2

You can call search box by following code in phtml:

<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magento_Search::form.mini.phtml")->toHtml();?>

Hope above will help!

Update I

If you want this search bar with default one you need to create copy of form.mini.phtml say form.mini.custom.phtml

from:

Mage2Root/vendor/magento/module-search/view/frontend/templates/form.mini.phtml

to

Mage2Root/app/design/frontend/{Package}/{theme}/Magento_Search/templates/form.mini.custom.phtml

change ids from search_mini_form to search_mini_form_custom and input id from search to search.custom

finally call this by:

<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magento_Search::form.mini.custom.phtml")->toHtml();?>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.