2016-10-19 57 views
0

子我如何檢查是否一個字符串包含特定的子? 例如我有一個名爲{category.files}的對象數組,並輸出每個對象名稱。檢查字符串包含在VHS 2.4

<f:for each="{category.files}" as="file"> 
    <p>{file.name}</p> 
</f:for> 

現在我試着檢查名稱是否包含特定的子字符串,如果是真的輸出它?在我的情況下,我搜索的子字符串

我發現VHS方法contains但我不知道如何使用它,因爲沒有包含任何實際的例子。

它是如何在我的情況下工作嗎?

<f:for each="{category.files}" as="file"> 
            ???   ???     ?? 
    <v:if.string.contains then="[mixed]" else="[mixed]" haystack="{file}" needle="fire"> 
     <!--output it if found --> 

     <!-- else, do nothing --> 
    </v:if.string.contains> 
</f:for> 

因此,換句話說,我試圖尋找草垛

回答

2
heystack = string to compare 
needle = string to find 

如果{file.name}能有針,你應該做這樣的事情:

<f:for each="{category.files}" as="file"> 
    <v:condition.string.contains haystack="{file.name}" needle="fire"> 
     <f:then> 
     Needle found 
     </f:then> 
     <f:else> 
     Needle not found 
     </f:else> 
    </v:condition.string.contains> 
    </f:for> 

編輯:由於在評論中的版本中提到的要求視圖助手v:if.string.contains更名爲v:condition.string.contains這是用來。

+0

謝謝!不幸的是,我現在無法測試,因爲我得到了'無法分析類:FluidTYPO3 \ Vhs \ ViewHelpers \ If \ String \ ContainsViewHelper可能沒有加載或沒有自動加載器?'。我試圖在部分擴展名中使用vhs代碼。也許我必須首先包含VHS? – Black

+1

嘗試添加'''{命名空間V = FluidTYPO3 \ VHS \ ViewHelpers}'''你的模板 –

+0

嗯開始時,我嘗試過,但還是同樣的錯誤消息。我是否必須在'ext_localconf.php'或其他地方包含VHS,以便擴展可以使用它? – Black