【问题标题】:TYPO3 / Typoscript: Get properties of a content elementTYPO3 / Typoscript:获取内容元素的属性
【发布时间】:2018-07-25 06:55:33
【问题描述】:

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

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

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

我得到了带有图像标题和描述的部分,但我无法获得运行背景图像的图像文件名!

我当前的 Typoscript 如下所示: (在托马斯回答后编辑,但仍未运行)

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>

【问题讨论】:

  • 我正在使用fluid_styled_content。现在,我更喜欢插入该图像 URL 的快速解决方案。

标签: typo3 typoscript typo3-8.x


【解决方案1】:

您使用流体样式内容,不是吗?为什么不使用 FLUIDTEMPLATE 和 DataProcessing?在那里,您可以在一个不错的数组中获得所有内容数据,并且您可以按照自己的意愿构建 HTML。

请参阅https://usetypo3.com/custom-fsc-element.html 以获得一个小的开始。

这里是 DataProcessing 的文档:https://docs.typo3.org/typo3cms/TyposcriptReference/8.7/ContentObjects/Fluidtemplate/#dataprocessing

【讨论】:

  • 是的,我正在使用fluid_styled_content。谢谢,您的回答似乎是一个非常好的方法,但我认为我需要一些工作和时间来学习这些东西。我肯定会努力解决它,但在那之前,你还有更快的方法来真正快速地解决我的问题吗?目前,这可能是一个非常肮脏的解决方案,只需要快。
【解决方案2】:

我不能尝试,但我会这样做:

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

【讨论】:

  • 我无法运行您的代码....如果我用您的代码替换旧代码,我会得到相同的结果(空 URL)。我用你的代码和一些额外的代码更新了我的问题 - 也许错误在其他地方?
【解决方案3】:

你把所有的部分放在一起,你只是把它们混在一起了。

        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',您需要提供一个 UId(属于 sys_file)并且没有名称


    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

【讨论】:

  • 我试图将您的答案包含在我的代码中,但我认为我的做法不正确。我更新了我的问题并包含了我当前的代码(请参阅尝试 2 以了解我尝试包含您的答案)。你能告诉我我做错了什么吗?
  • 答案增强
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-08-11
  • 1970-01-01
  • 1970-01-01
  • 2011-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多