5

How can I override the component view for a specific menu item?

For a global overwrite I can use template/html/com_xxx/.
But how can I do it only for a specific menu item?

Example :

Joomla! has a Blog view for Articles.
How can I change the markup of this view for a specific menu item, and keep the default markup for the other menu items linking to the same Blog view?

3

Method 1:

Use the CSS class to make it work the way you describe.

  1. Add the class to the menu item (at the page display tab, write it in the "Page Class" field). Example: " special-page" (note the space before the page class, without it it will append it to the component rendered output). I recommend using a class that can be used in more than one situation.
  2. At the override, you need to script the "filter".

    if(strpos($this->pageclass_sfx, ' special-page') !== false) {
        $templateA = true;
    }
    
    if($templateA){
    (put your customized override here)
    }
    

Method 2:

Use a template override for the view!

  1. In your HTML folder for the overrides, duplicate the desired view (ex: the article view), you need to rename it and have the .xml file with the parameters (copy it from the views in the component, and make sure the .xml has the same name as the overriden view!).

    example: Create an overriden view of the article, let's call it "special", you will need to copy the default.php into your HTML folder in your template (and put it into the following folder structure: com_content/article/ and rename it to special.php). Don't forget the .xml (that you need to copy and even adapt if needed) to the same folder as the override, and rename it to special.xml

    Update the view with the overriden code/style, etc.

  2. Then, go to the menu item, and change it to the new menu type (under articles, choose special), test it!
  • 1
    method 1 - too tricky, and method 2 is a right answer ;) – Fedik Apr 24 '14 at 11:21
  • 1
    @Fedik true, but in terms of updating one can be better than the other. I usually use method 1 because I can use the same file and customize parts of it, whereas method 2 is for a full override. – jackJoe Apr 24 '14 at 11:24
2

General solution :

For each menu-item, you can choose a template to apply to the linked page, so you can duplicate your template and create your view override in the duplicated template.

You can then choose the duplicated template in your menu-item parameters and the override will be effective only for that menu-item.

With K2 :

For your example, you could use K2. With it, you can create several view overrides with different names..

Overrides look like this :

template_name/html/com_k2/override_1
                         /override_2
                         /...

Then when you choose a K2 item in your menu parameters, you can choose the override to apply to it on that specific page. This avoids creating several templates.

  • 1
    You can create view overrides with different names in a standard Joomla without K2... – jackJoe Apr 24 '14 at 11:11
  • @jackJoe yes but you can't choose them from menu manager. – web-tiki Apr 24 '14 at 11:12
  • Yes you can :) choose a "different" (your override) menu type. See my method 2 in my answer. – jackJoe Apr 24 '14 at 11:13
  • @jackJoe You are right, didn't know that. I upvoted your answer. – web-tiki Apr 24 '14 at 11:18
  • thanks :) try it! different overrides to the same views are great! – jackJoe Apr 24 '14 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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