TYPO3 / Typoscript:获取内容元素的属性

时间:2018-07-25 06:55:33

标签: typo3 typoscript typo3-8.x

我在访问TYPO3中的内容对象中的内容时遇到问题:

在TYPO3 8.7.11中运行,我有一个带有内容元素“图像”的页面,并且在其中加载了一个图像。我想得到这样的输出:

<section class="foo" style="background-image:url('fileadmin/foo.png')">
   <h2>[image title]</h2>
   <p>[image description]</p>
</section>

我得到的图像标题和说明起作用了,但是我无法获得正在运行的背景图像的图像文件名!

我当前的打字稿看起来像这样: (在Thomas回答后编辑,但仍未运行)

lib.teaser = CONTENT
lib.teaser {
    table = tt_content
    select.where = colPos = {$teaser_column}
    languageField = sys_language_uid


    renderObj = COA
    renderObj {

        # Attempt 1 (regarding the answer of Thomas Löffler)
        # this doesn't work and result in an empty URL
        #
        # 10 {
        #     references {
        #         uid.data = uid
        #         table = tt_content
        #         fieldName = media
        #     }
        #
        #     begin = 0
        #     maxItems = 1
        #
        #     renderObj = IMG_RESOURCE
        #     renderObj {
        #         file.import.data = file:current:publicUrl
        #     }
        #     stdWrap.wrap = <section class="teaser" style="background-image:url('|');">
        # }

        # Attempt 2 (regarding the answer of Bernd Wilke πφ)
        # this doesn't work and result in an empty URL

        10 = FILES
        10 {
            references {
                uid.data = uid
                table = tt_content
                fieldName = media
            }

            # a) did you mean that I want to replace
            # my renderObj and use a TEXT object instead?

            renderObj = TEXT
            renderObj {
                data = file:current:publicUrl
            }

            # b) ... or did you mean that I want to provide
            # an IMG_RESOURCE and inside of that I want to
            # provide a renderObj = TEXT?
            #
            # renderObj = IMG_RESOURCE
            # renderObj {
            #     listNum = 0
            #     override.field = media
            #     renderObj = TEXT
            #     renderObj.data = file:current:publicUrl
            # }

            stdWrap.wrap = <section class="teaser" style="background-image:url('|');">
        }

        20 = TEXT
        20.field = header
        20.wrap = <h2 class="hide-text">|</h2>

        30 = TEXT
        30.field = bodytext
        30.wrap = <p>|</p>

        90 = TEXT
        90.value = </section>
    }
}

# ...
# edit: added after Thomas answer below

page {
    10 = FLUIDTEMPLATE
    10 {
        format = html
        file = {$root}/Templates/{$template}/Layouts/{$main_layout}.html
        layoutRootPath = {$root}/Templates/{$template}/Layouts
        partialRootPath = {$root}/Templates/{$template}/Partials

        variables {
            teaser < lib.teaser
            # ... some other variables ...
        }

        # load templates for sections, otherwise Typo3 won't find your sections
        file.stdWrap.cObject = CASE
        file.stdWrap.cObject {
            key.data = levelfield:-1, backend_layout_next_level, slide
            key.override.field = backend_layout

            default = TEXT
            default.value = {$root}/Templates/{$template}/Templates/Main.html
        }
    }
}

当前产生此HTML代码:

<section class="teaser" style="background-image:url('');">
   <h2 class="hide-text">Get our latest products:</h2>
   <p></p>
</section>

3 个答案:

答案 0 :(得分:2)

您使用fluid_styled_content,不是吗?为什么不使用FLUIDTEMPLATE和DataProcessing?在那里,您可以将所有内容数据存储在一个不错的数组中,并且可以随意构建HTML。

请参阅https://usetypo3.com/custom-fsc-element.html作为一个小开始。

以下是数据处理的文档:https://docs.typo3.org/typo3cms/TyposcriptReference/8.7/ContentObjects/Fluidtemplate/#dataprocessing

答案 1 :(得分:0)

我无法尝试,但是我会这样做:

10 = FILES
10 {
    references {
        uid.data = uid
        table = tt_content
        fieldName = media
    }

    begin = 0
    maxItems = 1

    renderObj = IMG_RESOURCE
    renderObj {
        file.import.data = file:current:publicUrl
    }
    stdWrap.wrap = <section class="teaser" style="background-image:url('|');">
}

示例:https://docs.typo3.org/typo3cms/TyposcriptReference/8.7/ContentObjects/Files/Index.html#usage-with-references

答案 2 :(得分:0)

您将所有部分放在一起,只是将它们混合在一起。

        renderObj = IMG_RESOURCE
        renderObj {
            file.import.data = file:current:publicUrl
            listNum = 0
            override.field = media
            file.treatIdAsReference = 1
        }

IMG_RESOURCE是正确的对象,但是您没有正确构建它:

file.import.data = file:current:publicUrl:在期望文件名的位置,您提供了图像的完整路径。 等待!? “图像的完整路径” ?这不是你想要达到的目标吗?

renderObj = TEXT
renderObj.data = file:current:publicUrl

如果您使用“ treatIdAsReference = 1”,则需要提供一个(sys_file的)UId,并且没有名称


    10 = FILES
    10 {
        references {
            // this line should not be neccessary as the current context identifies the record
            #uid.data = uid
            table = tt_content
            fieldName = media
        }
        // we only can handle one image:
        maxItems = 1

        // if we want no processing of the image:
        renderObj = TEXT
        renderObj {
            data = file:current:publicUrl
        }

        // maybe a processing  is necessary 
        // (always deliver smallest image possible)
        #renderObj = IMG_RESOURCE
        #renderObj {
        #    file {
        #        import.data = file:current:uid
        #        treatIdAsReference = 1
        #        width = 500c
        #        height = 200c
        #    }
        #}
        stdWrap.wrap = <section class="teaser" style="background-image:url('|');">
    }

如果这不起作用,那么您通常会从tt_content元素中提取文件。
也许您的字段名不是media
检查数据库:

SELECT fieldname 
  FROM sys_file_reference 
  WHERE tablenames = 'tt_content' 
    AND uid_foreign = <tt_content_record-uid>

我刚刚检查了我的一个安装,发现:字段名称为assets