简体   繁体   中英

Custom Block in Magento catalogsearch.xml

I have created a sample module Web. Also I have created one Sample Block for this. I have one web.phtml file in app/design/frontend/default/default/template folder and one web.xml file in app/desing/fronted/default/default/layout file. Below is my web.xml file

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
    </default>
    <web_index_index>
    <reference name="root">
          <action method="setTemplate"><template>page/2columns-left.phtml</template></action>
    </reference>
        <reference name="content">
            <block type="web/web" name="web" template="web/web.phtml" />
        </reference>
    </web_index_index>
</layout> 

It works properly in my http://mydomain.com/magento/web .

Now I copied a catalogsearch.xml file from app/desing/fronted/base/default/layout and paste it in app/desing/fronted/default/default/layout and added block code in it after results.phtml but I am not able to see the block in my catalogsearch page.

<block type="web/web" name="web" template="web/web.phtml" /> 

What I am missing here? What is the proper way to add block in catalogsearch.xml file?

The file in which a layout update directive appears is (generally) irrelevant. What scopes layout XML directives are the layout update handles.

For catalogsearch results you will want to add your block to two handles if you want it in both the simple and advanced search results views; add this to your web.xml layout file:

<catalogsearch_result_index>
    <reference name="content">
        <block type="web/web" name="web" template="web/web.phtml" after="-" /> 
    </reference>
</catalogsearch_result_index>

<catalogsearch_advanced_result>
    <reference name="content">
        <block type="web/web" name="web" template="web/web.phtml" after="-" /> 
    </reference>
</catalogsearch_advanced_result>

Now, most developers will blanche at the thought of repeating code. The above instruction can be written once in a utility handle and then the utility handle can be used to apply the result elsewhere:

<web_addToContent>
    <reference name="content">
        <block type="web/web" name="web" template="web/web.phtml" after="-" /> 
    </reference>
</web_addToContent>

<catalogsearch_advanced_result>
    <update handle="web_addToContent" />
</catalogsearch_advanced_result>

<catalogsearch_advanced_result>
    <update handle="web_addToContent" />
</catalogsearch_advanced_result>

If the block is appearing at the beginning of the content (despite after="-" ), it may be necessary to make the web module's config XML load after the catalog search module.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM