Fluid condition with ContainsViewHelper - typo3

i use this condition in my fluid template:
<f:if condition="{settings.image.className} == 'lightbox'">
<f:then>
....do something
</f:then>
<f:else>
<f:if condition="{settings.image.className} !== 'lightbox'">
<f:then>
....do something else
</f:then>
</f:if>
</f:else>
It works fine but if $settings.image.className" is something like "lightbox container" instead of just "lightbox" it does not work of course.
Unfortunately i do not know how write a condtion that checks if $settings.image.className contains "lightbox" or not.
The only instructions i found are here:
ViewHelper Reference .However i do not know how to apply that.

add this to the top of the partial/content Element
{namespace v=FluidTYPO3\Vhs\ViewHelpers}
and change the logic like this
<v:condition.string.contains haystack="{settings.image.className}" needle="lightbox">
<f:then>
....do something
</f:then>
<f:else>
....do something else
</f:else>
</v:condition.string.contains>

Related

Want to change a logo with an if/then condition

I want to select a logo depending on a check box of a page property I have added via extension. The variable selectcompany is shown correctly when dumping the variables.
I am running Typo3 8 LTS.
obj.logo {
<f:if condition="{field:selectcompany}==1">
<f:then>
file = fileadmin/template/private/images/Logo1.png
</f:then>
<f:else>
file = fileadmin/template/private/images/Logo0.png
</f:else>
</f:if>
}
Although the variable is set correctly, always Logo0.png is displayed. When the variable is set to 1, I expected Logo1.png.
If this is a TypoScript snippet, you can not fill in a Fluid condition there, but have to make use of the TypoScript variant of an if condition.
https://docs.typo3.org/m/typo3/reference-typoscript/master/en-us/Functions/If.html
On TypoScript, it is possible to use a condition (TYPO3 9.5.x syntax):
[page["selectcompany"] == 1]
obj.logo.file= fileadmin/Images/logo1.png
[GLOBAL]
Else, you could solve with fluid templating; it should be just:
<f:if condition="{data.selectcompany}">
<f:then>
<f:image src="fileadmin/template/private/images/Logo1.png" alt="" />
</f:then>
<f:else>
<f:image src="fileadmin/template/private/images/Logo0.png" alt="" />
</f:else>
</f:if>
I used <f:image> but I guess you could also go with a plain <img> tag, too.

In a TYPO3 template, v:variable.set works... but f:variable doesn't

The problem
I have customized something in a file overriding this news extension file Templates/Styles/Twb/Templates/News/List.html. This was working...
<v:variable.set name="special.category" value="1" />
<f:if condition="{special.category}">
<f:then>
Do something for the special category when the variable's boolean value is 1
</f:then>
<f:else>
Otherwise do something else
</f:else>
</f:if>
But when I tried to change it to this, it no longer works...
<f:variable name="special.category" value="1" />
<f:if condition="{special.category}">
<f:then>
Do something for the special category when the variable's boolean value is 1
</f:then>
<f:else>
Otherwise do something else
</f:else>
</f:if>
Some documentation
https://docs.typo3.org/typo3cms/extensions/core/Changelog/8.6/Feature-79402-VariableViewHelperForFluid.html
https://fluidtypo3.org/viewhelpers/vhs/master/Variable/SetViewHelper.html
My platform
I am using TYPO3 9.5.3 but would also like to know the answer for 8.7.
I think its the dot in the variable name. Maybe vhs:variable is implemented different in this case. Maybe <f:variable name="special" value="{category: 1}"/> this works?

How to negate f:if condition in TYPO3 6.2?

I have a TYPO3 6.2 instance ... yes I know it's obsolete, I work parallel on a migration ;)
I have this if statement
<f:if condition="{v:var.get(name: 'access', useRawKeys: 1)}">
Is there a way to negate this statement in TYPO3 6.2.31?
Thanks in advance
Condition evaluation is very basic in TYPO3 6 and 7: there is no not.
Also there is no build in not-Viewhelper, you might write it yourself.
But you could easily use the <f:else> viewhelper, a <f:then> is not necessary:
<f:if condition="{v:var.get(name: 'access', useRawKeys: 1)}">
<f:else>
: do anything if condition is false
</f:else>
</f:if>
Do you mean, if there is no "access" parameter?
<f:if condition="{v:var.get(name: 'access', useRawKeys: 1)}">
<f:else>
###your code if there is no access in the URL##
</f:else>
</f:if>

TYPO3 - How to check if a news article has a certain category with fluid

How can I check that a news article has a certain category? And when a news article is allocated to a certain category I want to add some additional information to the article (Detail). I tried this in Template/News/Detail.html e.g.:
//<f:for each="{categories}" as="category">
<f:if condition="{category.title} == 'Category01'">
<f:then>
<h4>It worked!</h4>
</f:then>
<f:else>
<h4>Didn't work!</h4>
</f:else>
</f:if>
//</f:for>
What works is e.g. this:
<!-- check category -->
<f:if condition="{newsItem.categories}">
<f:for each="{newsItem.categories}" as="category">
<f:if condition="{category.title} == 'Category01'">
<f:then>
<h4>It worked!</h4>
</f:then>
</f:if>
</f:for>
</f:if>

Clean way to switch between typo3 fluid page and action link

Is there a more clean way to switch between a page or a action link in fluid based on a var?
Now i used the if then statement but this increases a lot of double code lines. See example:
<f:if condition="{var}">
<f:then>
<f:link.page pageUid="{PageId}">
// a lot of code lines
</f:link.page>
</f:then>
<f:else>
<f:link.action pluginName="Name" controller="Cont">
// the same a lot of code lines again
</f:link.action>
</f:else>
</f:if>
You can extract the code within the links into a partial.
For this, first create a partial template. Inside an Extbase extension, they are placed in Resources/Private/Partials by convention (you can change this by using the setPartialsRootPath() method on the template object, but usually that shouldn't be necessary).
# Resources/Private/Partials/LinkContent.html
<!-- Insert your shared code lines here -->
Then reference the partial in your template:
<f:if condition="{var}">
<f:then>
<f:link.page pageUid="{PageId}">
<f:render partial="LinkContent" />
</f:link.page>
</f:then>
<f:else>
<f:link.action pluginName="Name" controller="Cont">
<f:render partial="LinkContent" />
</f:link.action>
</f:else>
</f:if>
Note that you will have to pass variables explicitly into the partial from the parent template:
<!-- Assuming there's a variable "foo" in your template -->
<f:render partial="LinkContent" arguments="{foo: foo}" />
Alternatively, you can import the entire scope into the partial:
<f:render partial="LinkContent" arguments="{_all}" />