How can I get tomorrow's date in fluid with f:format.date?
I need to do an if condition in fluid to check if the event's date is earlier than tomorrow.
Thank you.
You can use strtotime strings in f:format.date, so you can use {f:format.date(date: 'tomorrow')} to get tomorrow's date and compare that to your date. Something like:
<f:if condition="{event.date} < {f:format.date(date: 'tomorrow')}">
<f:then>
Earlier than tomorrow
</f:then>
<f:else>
Not earlier than tomorrow
</f:else>
</f:if>
Related
I need to calculate the difference in days between a given date and the current date.
I've created a mask element, where I can provide a date. In my template I want to output something like "published 2 days ago"
I've tried this:
<f:if condition="{data_item.tx_mask_date}">
<p class="ww-teaserbox__date">
<f:variable name="now" value="{f:format.date(date: 'now', format: 'Y-m-d')}" />
<f:variable name="publish" value="{f:format.date(date: data_item.tx_mask_date, format: 'Y-m-d')}" />
{f:format.date(date: now, format: 'Y-m-d') - f:format.date(date: publish, format: 'Y-m-d') }
</p>
</f:if>
But that just outputs {f:format.date(date: now, format: 'Y-m-d') - f:format.date(date: publish, format: 'Y-m-d') }.
I hope anyone can help me here, thanks in advance.
Even though fluid can do some basic calculations this is not really possible.
I suggest using a custom ViewHelper with 2 arguments and do the calculcation like this https://stackoverflow.com/a/26225650/2389552
I have an array ('db-titles') whose items are strings composed in the form 'TextA*TextB'.
I want to create the following HTML structure from these array items: <span>TextA</span><span>TextB</span>
For this I use the following fluid script:
{namespace v=FluidTYPO3\Vhs\ViewHelpers}
<f:if condition="{db-titles -> f:count()} > 1">
<f:then>
<!-- This works as expected, the html code will be rendered correctly -->
<v:iterator.for from="0" to="{db-titles -> f:count()}" iteration="i">
<span><v:format.replace content="{db-titles.{i.index}}" substring="*" replacement="</span><span>"/></span>
</v:iterator.for>
</f:then>
<f:else>
<!-- This strangely does not work, although I - instead of running through all values of the array - just want to output the first value of it.... -->
<span><v:format.replace content="{v:iterator.first(haystack: db-titles)}" substring="*" replacement="</span><span>"/></span>
</f:else>
</f:if>
As I commented in the source code, everything works as it should under '<f:then>', but under '<f:else>', where I only want to use the first value of the array, the replaced part ('*' to </span><span>) is strangely rendered as text instead of HTML: <span>TextA</span><span>TextB</span>
How can this be?
Let me mention that I use the app "PHPStorm" for programming - is it possible that the program renders the source code wrong?
Thanks in advance for any help!
Without having found an explanation for the strange behaviour, I have found an solution: wrapping <v:format.replace ... /> with <f:format.raw> ... </> prevents that the tags converted to strings.
<f:if condition="{db-titles -> f:count()} > 1">
<f:then>
<v:iterator.for from="0" to="{db-titles -> f:count()}" iteration="i">
<span><f:format.raw><v:format.replace content="{db-titles.{i.index}}" substring="*" replacement="</span><span>"/></f:format.raw></span>
</v:iterator.for>
</f:then>
<f:else>
<span><f:format.raw><v:format.replace content="{v:iterator.first(haystack: db-titles)}" substring="*" replacement="</span><span>"/></(f:format.raw></span>
</f:else>
</f:if>
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.
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?
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>