-->

typoscript: do not show submenu if certain backend

2019-09-16 10:44发布

问题:

I made a special panning sub menu with icons which can be inserted choosing a specific backend_layout named pagets__panmenu , if that layout is selected the main menu should not display a sub menu and link to the page instead of opening a sub ...

I work with typo3 V7.6.11 and fluid styled content

The part reading the value works correctly:

NO = 1
NO {
  before.cObject = LOAD_REGISTER
  before.cObject{
    panmenu.cObject = TEXT
    panmenu.cObject.data.dataWrap = DB:pages:{field:uid}:backend_layout
  }
  ATagBeforeWrap = 1
  wrapItemAndSub = <li>|</li>
  stdWrap.htmlSpecialChars = 1
}

This is my best effort to match, but its not working:

IFSUB <.NO
IFSUB {
    wrapItemAndSub = <li class="dropdown">|</li>
    wrapItemAndSub.override = <li>|</li>        
    wrapItemAndSub.override.if {
        value.data = register:panmenu
        equals = pagets__panmenu
    }

    ATagParams = class="dropdown-toggle" data-toggle="dropdown" 
    ATagBeforeWrap = 1
}

I know that also the 2 < .1 has to be suppressed, I'm trying to get the if to work to keep the style and link clean for starters ...

回答1:

First of all: have you set TMENU.IFSUB = 1? Else, everything you do in IFSUB won't have an effect.

Now some general thoughts:

a) Normally, backend layouts are used to switch an entire page template:

page.10 = FLUIDTEMPLATE
page.10 {
  file.stdWrap.cObject = CASE
  file.stdWrap.cObject {
    key.data = pagelayout

    default = TEXT
    default.value = {$myTemplatePath}/Standard.html

    1 = TEXT
    1.value = {$myTemplatePath}/Home.html

    2 = TEXT
    2.value = {$myTemplatePath}/Landing.html

  }
  layoutRootPath = {$myLayoutPath}
  partialRootPath = {$myPartialPath}
}

So this initiates the frontend rendering for the entire page, where backend_layout with uid 1 will use the Home.html template, backend_layout with uid 2 will use the Landing.html template, and all others (=default) will use the Standard.html template.

If you build your site by this method, I would recommend doing

# that's the original version of your menu
lib.panmenu = HMENU
lib.panmenu {
 // ...
}

# make a copy of the original 
lib.panmenu_variant < lib.panmenu
# modify it as required 
lib.panmenu_variant.1.NO {   
  // ... 
}

Now, in your page Templates (which you call separately via the CASE from above), you can either use

<f:cObject typoscriptObjectPath="lib.panmenu" />

or

<f:cObject typoscriptObjectPath="lib.panmenu_variant" />

b) But if you don't want to follow that approach, you should also be able to use the backend_layout CASE on any cObject. The CASE variant has proven much more robust for me.

Here' how I'd try to get the CASE working (untested!)

// suppose temp.navigation_main is your full menu
temp.navigation_main_variant < temp.navigation_main 
temp.navigation_main_variant {
    // modify the menu as you please
    10.2 >
    10.1.IFSUB.wrapItemAndSub = <li>|</li>
}

// use lib.nav in your page
lib.nav = CASE
lib.nav {
    key.data = pagelayout
    // normally, lib.nav is the full navigation
    default < lib.navigation_main
    // except if be layout 1 is selected
    1 < lib.navigation_main_variant
  }
}

c) I've tried TypoScript constructions with LOAD_REGISTER and ifs years ago and they always made me go crazy. I wouldn't invest too much energy into them, as they rather seem to be legacy parameters than the future way of development for TYPO3 logic.