Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls
 

Easiest way to add a CSS class based on page data

 
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)
 
Thread Tools Search this Thread
  #1  
Old 12-11-2015, 04:16 AM
 
xgarb xgarb is offline
 

eXpert
  
Join Date: Jul 2004
Location: UK
Posts: 263
 

Default Easiest way to add a CSS class based on page data

I have a mega menu on the site and I want to show the top level menu item for the current page in a different colour.

There are many levels of categories. I have some code that identifies the top level category for the current page.

I call it with something like this:
Code:
<widget class="XLite\Module\Client\ClientMegaMenu\View\CheckIfTopLevelCategoryForCurrentPage" thisCatId="8" />

CheckIfTopLevelCategoryForCurrentPage.php checks if the current page top level cat ID is the same as the cat ID (thisCatId) in the menu.

In my template I want to add a CSS class based on the result from the PHP file above so I need to output something like class="active-menu-item". Can I get the widget class to just output something directly and not have to use another template file to output into?

I intend to use it like this...
Code:
<li class="<widget class="XLite\Module\Client\ClientMegaMenu\View\CheckIfTopLevelCategoryForCurrentPage" thisCatId="8" /> ">
__________________
Core version: 5.5.xx
Reply With Quote
  #2  
Old 12-15-2015, 02:54 AM
  qualiteam's Avatar 
qualiteam qualiteam is offline
 

X-Guru
  
Join Date: Dec 2010
Posts: 6,373
 

Default Re: Easiest way to add a CSS class based on page data

Quote:
Code:
<li class="<widget class="XLite\Module\Client\ClientMegaMenu\View\CheckIfTopLevelCategoryForCurrentPage" thisCatId="8" /> ">

This code looks really weird. I believe there should be an easier way to do what you need.

What widget does render the mega menu? Why don't you add the check as a method to that widget class and use it as follows:
Code:
<li class="{doMyCheck(8)}">
__________________
Alex Solovev,
Qualiteam

---

User manual Video tutorials X-Cart FAQ

You are welcome to press "Thanks" button
if you find this post useful

Click here to learn how to apply patches

X-Cart Extensions
Reply With Quote

The following user thanks qualiteam for this useful post:
xgarb (12-15-2015)
  #3  
Old 12-15-2015, 05:09 AM
 
xgarb xgarb is offline
 

eXpert
  
Join Date: Jul 2004
Location: UK
Posts: 263
 

Default Re: Easiest way to add a CSS class based on page data

The mega menu is called from another template and contains calls to other widgets. It's so I can control the top categories shown in static HTML. Looks like this...

Code:
... <li class="has-children"> <a href="#" style="line-height:20px; text-transform: uppercase;">Displays &amp;<br> Data Logging</a> <widget class="XLite\Module\Client\ClientMegaMenu\View\TopMenuFromId" displayMode="tree" rootId="10" /> </li> <li class="has-children"> <a href="#" style="text-transform: uppercase;">Process Sensors</a> <widget class="XLite\Module\Client\ClientMegaMenu\View\TopMenuFromId" displayMode="tree" rootId="11" /> </li> ...

but your post pushed my brain back in the right direction and I've got it working by creating a new class that decorates the \XLite\View\AView class.

The class has a function...
Code:
protected function showHighlightIfRootIs($rootcat){ $currentPageRootCat = $this->xgarbFindRoot(); if ( $currentPageRootCat == $rootcat){ return ' megamenu-root-cat'; //css class for root cat of current page } }

called like this..
{showHighlightIfRootIs(x)}

so my menu looks like this now...
Code:
... <li class="has-children{showHighlightIfRootIs(10)}"> <a href="#" style="line-height:20px; text-transform: uppercase;">Displays &amp;<br> Data Logging</a> <widget class="XLite\Module\Client\ClientMegaMenu\View\TopMenuFromId" displayMode="tree" rootId="10" /> </li> <li class="has-children{showHighlightIfRootIs(11)}"> <a href="#" style="text-transform: uppercase;">Process Sensors</a> <widget class="XLite\Module\Client\ClientMegaMenu\View\TopMenuFromId" displayMode="tree" rootId="11" /> </li> ...

Works nicely!
__________________
Core version: 5.5.xx
Reply With Quote
  #4  
Old 12-15-2015, 05:33 AM
  qualiteam's Avatar 
qualiteam qualiteam is offline
 

X-Guru
  
Join Date: Dec 2010
Posts: 6,373
 

Default Re: Easiest way to add a CSS class based on page data

Quote:
... creating a new class that decorates the \XLite\View\AView class ...

I think there is a class that is more specific for the menu than AView. By decorating AView class you add the method to every widget on the page.

Will it work if you add the method to TopMenuFromId class?
If not, what is the full "method not found" error backtrace from the log file?
__________________
Alex Solovev,
Qualiteam

---

User manual Video tutorials X-Cart FAQ

You are welcome to press "Thanks" button
if you find this post useful

Click here to learn how to apply patches

X-Cart Extensions
Reply With Quote
  #5  
Old 12-15-2015, 06:22 AM
 
xgarb xgarb is offline
 

eXpert
  
Join Date: Jul 2004
Location: UK
Posts: 263
 

Default Re: Easiest way to add a CSS class based on page data

Didn't work...

error:

Dec 15 15:16:52 XLite [error] Trying to call undefined class method; class - "XLite\Controller\Customer\Category", function - "showHighlightIfRootIs"
Runtime id: e9de798b3da5393e282783e430de780b;
Server API: cgi-fcgi; IP: 213.143.60.87;
Request method: GET;
URI: /x5_the_return/flow-meters;
Backtrace:
file <compiled classes repository>/classes/XLite/Base.php : 83
method XLite\Base::__call('showHighlightIfRootIs', array(1))
method XLite\Controller\Customer\Category::showHighlightI fRootIs(
file <compiled classes repository>/classes/XLite/View/AViewAbstract.php : 168
file <compiled classes repository>/skins/default/en/modules/Client/ClientMegaMenu/body.tpl.php : 5
file <compiled classes repository>/skins/default/en/modules/Client/ClientMegaMenu/body.tpl.php : 5
file <compiled classes repository>/classes/XLite/View/AViewAbstract.php : 297
file <compiled classes repository>/classes/XLite/View/Content.php : 73
file <compiled classes repository>/skins/ClientTheme/customer/en/main.tpl.php : 5
file <compiled classes repository>/classes/XLite/View/AViewAbstract.php : 297
file <compiled classes repository>/classes/XLite/View/Content.php : 73
file <compiled classes repository>/classes/XLite/View/AViewAbstract.php : 319
file <compiled classes repository>/classes/XLite/View/ControllerAbstract.php : 236
file <compiled classes repository>/classes/XLite/View/ControllerAbstract.php : 265
file <compiled classes repository>/classes/XLite/View/ControllerAbstract.php : 81
file <compiled classes repository>/classes/XLite/Controller/AControllerAbstract.php : 584
file <compiled classes repository>/classes/XLite/Module/XC/ThemeTweaker/Controller/AController.php : 44
file <compiled classes repository>/classes/XLiteAbstract.php : 517
file <X-Cart root>/cart.php : 37
__________________
Core version: 5.5.xx
Reply With Quote
  #6  
Old 12-15-2015, 10:08 PM
  qualiteam's Avatar 
qualiteam qualiteam is offline
 

X-Guru
  
Join Date: Dec 2010
Posts: 6,373
 

Default Re: Easiest way to add a CSS class based on page data

OK, so there is no specific widget class that renders the template and it calls back to the AView class and then to the controller.

What is in the skins/default/en/modules/Client/ClientMegaMenu/body.tpl.php file?

How do you display it on the page? Do you use @ListChild tag to add it to a "view list"? If so, I think you should create a new widget class extended from AView, move the @ListChild tag from the template to this class, point the getDefaultTemplate() method to your ClientMegaMenu/body.tpl.php template. This way you can have a new class that renders the template and where you can add your custom methods.
__________________
Alex Solovev,
Qualiteam

---

User manual Video tutorials X-Cart FAQ

You are welcome to press "Thanks" button
if you find this post useful

Click here to learn how to apply patches

X-Cart Extensions
Reply With Quote
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 02:22 AM.

   

 
X-Cart forums © 2001-2020