2

For example, I have a root template shared by all customer account pages. I want to display a link to the dashboard on all pages except the dashboard (obviously).

How could I detect the page is the dashboard (from PHTML) and hide it? Like this:

<?php if (someMagicDetectTheModuleMethod() !== 'someDashboardIdentifier'): ?>
    <a href="#">Dashboard</a>
<?php endif; ?>

4 Answers 4

4

An alternate method using only phtml is to check for the update handle in the current array of handles:

<?php if (in_array('customer_account_index', $this->getLayout()->getUpdate()->getHandles())): ?>
    <a href="#">Dashboard</a>
<?php endif; ?>
0
3

They layout handles would probably be the easiest way

var_dump(Mage::app()->getLayout()->getUpdate()->getHandles());

Also you can use the module name as given in the URL

var_dump(Mage::app()->getRequest()->getModuleName());
4
  • Can you pull the "module name" (I have no idea if that's the right wording) specifically? Effectively what you see in XML, I'd be looking for this value: customer_account_index Mar 11, 2015 at 20:54
  • Aside: I actually consider doing this type of thing in XML to be an anti-pattern. It splits your code in half and that's confusing. It's clearer to keep this as close to the content as possible (in PHTML). Mar 11, 2015 at 20:56
  • Updated my answer, let me know if it helped
    – Sander Mangel
    Mar 11, 2015 at 21:05
  • getModuleName() would return "customer" for some sub-pages of the dashboard too, so that's not enough to identify it but might be useful in other cases. Mar 11, 2015 at 21:19
1

I've done this with a mixture of xml/phtml before. So in the xml you'd set some data against the block only for the dashboard layout handle:

<customer_account_index>
    <reference name="my.block">
        <action method="setData"><name>is_dashboard</name><value>1</value></action>
    </reference>
</customer_account_index>

and then in your phtml file you check if that data is set:

<?php if ($this->getIsDashboard()): ?>
    <a href="#">Dashboard</a>
<?php endif; ?>
1
  • Always aiming to avoid XML to keep the code in one place (even if XML is more "correct"). Mar 11, 2015 at 21:14
1

You shouldn't do that in a template, you should create a block method which would have something like:

public function isDashboardPage()
{
    $page = Mage::helper('core/url')->getCurrentUrl();
    if(strpos($page, 'somedashobaridentifier') !== false) {
        return true;
    }
    return false;
}

Then you should call that method in the template:

<?php if($this->isDashboardPage()): ?>
    <a href="#">Dashboard</a>
<?php endif;?>
2
  • Thanks, but not aiming for the purest method. Splitting this between the frontend and backend code is less maintainable. Same reason I'm trying not to use XML. Mar 11, 2015 at 21:03
  • You could also do something similar in a template but it's a lot less pretty.
    – LDusan
    Mar 11, 2015 at 21:06

Your Answer

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

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