Jump to content
thirty bees forum
  • 0

How to test for hook data?


Question

Posted

How can I test in a theme template file whether or not a display hook has data?

Inside the /themes/themename/product.tpl file there is a hook to check for out of stock conditions and displays an input field, button, or nothing depending on whether not there should be a notification for mailalerts module.

I used the following test just prior to $HOOK_PRODUCT_OOS in the product.tpl file to prevent the div from being displayed if the element wasn't supposed to be displayed as this disrupted my theme by having it display regardless:

<div id="oosHook"{if $product->quantity > 0} style="display: none;"{/if}>

I have run in to a similar situation with the /themes/themename/product-list-item.tpl file where the test gets populated regardless of any hook data using the following code:

            {if $show_functional_buttons}
                <div class="functional-buttons clearfix show-if-product-grid-hover">
                    {hook h='displayProductListFunctionalButtons' product=$product}
                    {if isset($comparator_max_item) && $comparator_max_item}
                        <div class="compare">
                            <a class="add_to_compare" href="{$product.link|escape:'html':'UTF-8'}"
                               data-id-product="{$product.id_product}">
                                <i class="icon icon-plus"></i> {l s='Add to Compare'}
                            </a>
                        </div>
                    {/if}
                </div>
            {/if}

The reason this happens is because the variable/array $show_functional_buttons gets populated in the /themes/themename/product-list.tpl with this code:

{$show_functional_buttons = $page_name != 'index'}

As you can see, it only matters whether we are viewing 'index' or not - which really is not a valid test in my opinion. What I want to do is either unset this variable or change it to false if the hook named "displayProductListFunctionalButtons" actually has data - which it should not if the wishlist module or similar isn't active and I have set the "product compare" setting in [ preferences / products ] configuration page to "0".

3 answers to this question

Recommended Posts

  • 0
Posted (edited)

First is the original code that presented stylized div regardless of whether or not data is available, second is the modified code that keeps this element from being displayed at all when no data is available. I suppose I could have taken the approach to just hide the div instead of remove it entirely, but my particular coding style gives preference to not displaying an element if there is no need to do so. This approach was less work than correcting the php source files to address the issue.

Here's the original code:

            {if $show_functional_buttons}
                <div class="functional-buttons clearfix show-if-product-grid-hover">
                    {hook h='displayProductListFunctionalButtons' product=$product}
                    {if isset($comparator_max_item) && $comparator_max_item}
                        <div class="compare">
                            <a class="add_to_compare" href="{$product.link|escape:'html':'UTF-8'}"
                               data-id-product="{$product.id_product}">
                                <i class="icon icon-plus"></i> {l s='Add to Compare'}
                            </a>
                        </div>
                    {/if}
                </div>
            {/if}

Here's the code that corrects the issue so the div is not displayed if there is no data available to display - it keeps the rest of my product block code clean in the presentation:

Note: Because the "comparator" code isn't part of the hook, an if/elseif test was required so that it gets displayed regardless of whether the hook contains data when the comparator is turned on in the configuration settings.

            {* EXTEND TEST IF FUNCTIONAL_BUTTONS SHOULD BE DISPLAYED - FOR CLEANER THEME CODE/CSS *}
            {if $show_functional_buttons}
            {capture name='displayFunBtns'}{hook h='displayProductListFunctionalButtons'}{/capture}
                {if $smarty.capture.displayFunBtns}
                <div class="functional-buttons clearfix show-if-product-grid-hover">
                    {hook h='displayProductListFunctionalButtons' product=$product}
                    {if isset($comparator_max_item) && $comparator_max_item}
                        <div class="compare">
                            <a class="add_to_compare" href="{$product.link|escape:'html':'UTF-8'}"
                               data-id-product="{$product.id_product}">
                                <i class="icon icon-plus"></i> {l s='Add to Compare'}
                            </a>
                        </div>
                    {/if}
                </div>
                {elseif isset($comparator_max_item) && $comparator_max_item}
                    <div class="functional-buttons clearfix show-if-product-grid-hover">
                        <div class="compare">
                            <a class="add_to_compare" href="{$product.link|escape:'html':'UTF-8'}"
                               data-id-product="{$product.id_product}">
                                <i class="icon icon-plus"></i> {l s='Add to Compare'}
                            </a>
                        </div>
                    </div>
                {/if}
            {/if}

 

Edited by Obi

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...