获取 TYPO3 Fluid 模板中的字符串长度
Get length of string in TYPO3 Fluid template
如何检查字符串的长度?
如果不足 8 个字符,我想添加 class。
所以像这样:
{f:if(condition: '{page.title -> f:count()} < 8', then: ' large')}
您可以使用 vhs 包中的 v:count.bytes
进行尝试
https://fluidtypo3.org/viewhelpers/vhs/master/Count/BytesViewHelper.html
使用 Fluid 可以使用一些小技巧:
您将字符串裁剪为其最大长度并将结果与原始字符串进行比较。如果裁剪后的字符串不匹配原始字符串,则原始字符串比预期的长。
{f:if(condition: '{page.title} != {page.title -> f:format.crop(maxCharacters: 8, append:\'\')}', then: ' large')}
注意:append
必须设置为空字符串。
例子
lib.stringLength = FLUIDTEMPLATE
lib.stringLength {
variables {
shortText = TEXT
shortText.value = abc
exactText = TEXT
exactText.value = four
longText = TEXT
longText.value = Lorem ipsum
}
template = TEXT
template.value(
<h2>{shortText}</h2>
<p>condition: '{shortText} != {shortText -> f:format.crop(maxCharacters: 4, append:'')}': <br />
result: {f:if(condition: '{shortText} != {shortText -> f:format.crop(maxCharacters: 4, append:\'\')}', then: ' large')}
</p>
<hr />
<h2>{exactText}</h2>
<p>condition: '{exactText} != {exactText -> f:format.crop(maxCharacters: 4, append:'')}': <br />
result: {f:if(condition: '{exactText} != {exactText -> f:format.crop(maxCharacters: 4, append:\'\')}', then: ' large')}
</p>
<hr />
<h2>{longText}</h2>
<p>condition: '{longText} != {longText -> f:format.crop(maxCharacters: 4, append:'')}': <br />
result: {f:if(condition: '{longText} != {longText -> f:format.crop(maxCharacters: 4, append:\'\')}', then: ' large')}
</p>
<hr />
)
}
结果:
abc
condition: 'abc != abc':
result:
four
condition: 'four != four':
result:
Lorem ipsum
condition: 'Lorem ipsum != Lore':
result: large
如何检查字符串的长度? 如果不足 8 个字符,我想添加 class。
所以像这样:
{f:if(condition: '{page.title -> f:count()} < 8', then: ' large')}
您可以使用 vhs 包中的 v:count.bytes
进行尝试
https://fluidtypo3.org/viewhelpers/vhs/master/Count/BytesViewHelper.html
使用 Fluid 可以使用一些小技巧:
您将字符串裁剪为其最大长度并将结果与原始字符串进行比较。如果裁剪后的字符串不匹配原始字符串,则原始字符串比预期的长。
{f:if(condition: '{page.title} != {page.title -> f:format.crop(maxCharacters: 8, append:\'\')}', then: ' large')}
注意:append
必须设置为空字符串。
例子
lib.stringLength = FLUIDTEMPLATE
lib.stringLength {
variables {
shortText = TEXT
shortText.value = abc
exactText = TEXT
exactText.value = four
longText = TEXT
longText.value = Lorem ipsum
}
template = TEXT
template.value(
<h2>{shortText}</h2>
<p>condition: '{shortText} != {shortText -> f:format.crop(maxCharacters: 4, append:'')}': <br />
result: {f:if(condition: '{shortText} != {shortText -> f:format.crop(maxCharacters: 4, append:\'\')}', then: ' large')}
</p>
<hr />
<h2>{exactText}</h2>
<p>condition: '{exactText} != {exactText -> f:format.crop(maxCharacters: 4, append:'')}': <br />
result: {f:if(condition: '{exactText} != {exactText -> f:format.crop(maxCharacters: 4, append:\'\')}', then: ' large')}
</p>
<hr />
<h2>{longText}</h2>
<p>condition: '{longText} != {longText -> f:format.crop(maxCharacters: 4, append:'')}': <br />
result: {f:if(condition: '{longText} != {longText -> f:format.crop(maxCharacters: 4, append:\'\')}', then: ' large')}
</p>
<hr />
)
}
结果:
abc
condition: 'abc != abc':
result:
four
condition: 'four != four':
result:
Lorem ipsum
condition: 'Lorem ipsum != Lore':
result: large