簡體   English   中英

TYPO3 / Typoscript:獲取內容元素的屬性

[英]TYPO3 / Typoscript: Get properties of a content element

我在訪問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看起來像這樣:( 在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>

您使用fluid_styled_content,不是嗎? 為什么不使用FLUIDTEMPLATE和DataProcessing? 在那里,您可以將所有內容數據存儲在一個不錯的數組中,並且可以根據需要構建HTML。

請參閱https://usetypo3.com/custom-fsc-element.html作為一個小開始。

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

我無法嘗試,但是我會這樣做:

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

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

        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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM