TYPO3: Backend Layout Condition in TypoScript

余生颓废 提交于 2019-12-31 04:13:25

问题


I'd like to change the way elements are rendered depending on the page's backend layout.
Changing the fluid styled content template depending on the backend layout works as following:

[globalVar = TSFE:page|backend_layout = 1][globalVar = TSFE:page|backend_layout = 2]
lib.fluidContent.templateRootPaths.10 = EXT:ds_res/Resources/Private/Templates/ContentTemplates/
[global]

If it's 1 or 2, then use the other templates.

However, this only works if the BE layout is set directly at the page and not when it's inherited from its parent.
How to fix this?

Running TYPO3 7.6.15
Thanks.


回答1:


In TYPO3 7.5 "Backend Layout"-Conditions has been simplified with "pagelayout" in Typoscript. Example:

page.10 = FLUIDTEMPLATE
page.10 {
  file.stdWrap.cObject = CASE
  file.stdWrap.cObject {
    key.data = pagelayout
    default = TEXT
    default.value = EXT:sitepackage/Resources/Private/Templates/Home.html
    3 = TEXT
    3.value = EXT:sitepackage/Resources/Private/Templates/1-col.html
    4 = TEXT
    4.value = EXT:sitepackage/Resources/Private/Templates/2-col.html
  }
}

Instead of:

field = backend_layout
ifEmpty.data = levelfield:-2,backend_layout_next_level,slide
ifEmpty.ifEmpty = default

Maybe this also works in your conditions like this:

[globalVar = TSFE:page|pagelayout = 1]

However, you should not change the used template file with [xy]-conditions and prefer using a CASE shown in the example above. Each number is the UID of a backend_layout by the way.




回答2:


We use this solution

page = PAGE
page.10 = FLUIDTEMPLATE
page.10 {

templateName = TEXT
templateName.stdWrap {
    cObject = TEXT
    cObject {
        data = levelfield:-2,backend_layout_next_level,slide
        override.field = backend_layout
        split {
            token = pagets__
            1.current = 1
            1.wrap = |
        }
    }
    ifEmpty = Index
        }
        layoutRootPaths {
            10 = EXT:yourext/Resources/Private/Layouts
        }
        partialRootPaths {
            10 = EXT:yourext/Resources/Private/Partials
        }
        templateRootPaths {
            10 = EXT:yourext/Resources/Private/Templates
        }
}

This approach is when u do not have the Backendlayouts in the DB (made through the backend) but include them from files. As there is no uid then you go by the templatename.

Example: If you use this PageTS:

mod.web_layout.BackendLayouts {
    Blankpage {
        title = Blankpage
        name = Blankpage
        icon = EXT:yourext/Resources/Public/Icons/BackendLayouts/Blankpage.jpg
    config {
        backend_layout {
            colCount = 1
            rowCount = 2
            rows {
                1 {
                    columns {
                        1 {
                            name = Nameofthecolumn
                            colPos = 0
                            colspan = 1
                        }
                    }
                }
                2 {
                    columns {
                        1 {
                            name = Nameofthesecondcolumn
                            colPos = 1
                            colspan = 1
                        }
                    }
                }

            }
        }
    }
}

You will need a Template.html with the name Blankpage.html within your EXT-Templates.

So you can add more Templates and Backendlayouts without touching the TS again. Simply add PageTS and a html-template.




回答3:


There is a feature available which avoids having that code duplicated

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



回答4:


As you can not handle the inherited layouts in conditions you need to use typoscript. My solution is this:

page.10 = FLUIDTEMPLATE
page.10 {
    templateRootPaths.1 = {$resDir}/Private/Templates
    partialRootPaths.1 = {$resDir}/Private/Partials
    layoutRootPaths.1 = {$resDir}/Private/Layouts

    templateName = TEXT
    templateName.cObject = CASE
    templateName.cObject {
        key.data = levelfield:-1, backend_layout_next_level, slide
        key.override.field = backend_layout

        #Default Template
        default = TEXT
        default.value = subpage

        # homepage 
        pagets__homepage = TEXT
        pagets__homepage.value = homepage

        pagets__subpage = TEXT
        pagets__subpage.value = subpage
    }
    variables {
        :

        pageLayout = TEXT
        pageLayout.data = levelfield:-1, backend_layout_next_level, slide
        pageLayout.override.field = backend_layout

        :
    }
}

Avoid using file for better implementation with ..RootPaths.

As we use backendlayouts defined in files (which gets included in the pageTSconfig) the key names starting with pagets__, you also might use the numbers of backend_layout records.

I use the constant {$resDir} to define the root of resources which can be changed in an easy way. In a siteextension this could be:

resDir = EXT:site_project1/Resources

I also define a fluid variable with the currently active page layout for further differentiation in the templates.

If you want the ...RootPaths to be different for each layout you need to build cObject with a CASE obejct similar to my selection of the template name.

In general: all this could be handled in the fluid templates if you have the backend layout available in your variables: you only need to have one starting template, which calls a layout where all further calls to partials are individualized by the current layout like

<f:render partial="{pagelayout}/header" arguments="{_all}" />


来源:https://stackoverflow.com/questions/41630348/typo3-backend-layout-condition-in-typoscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!