TYPO3 Fluid Templates variables - typo3

I'm trying to set a variable in a Fluid template.
I try to divide something by 2 :
<f:variable name="hmc">{menu->f:count()} / 2</f:variable>
It does not work because I get a string instead of a numeric result.

I think you can't do it in a one-liner. You have to count first and then do the math.
<f:variable name="count">{menu -> f:count()}</f:variable>
<f:variable name="hmc">{count / 2}</f:variable>

Related

Empty cell Typo3 Fluid

Trying to set up a query for the "address list-Plugin" in Typo3.
So if my cell "Title" is written to there would be some information like "Prof.Dr." but if there's no Title on the Entry there should be "-"
Got no code at all at this point because its totally new to me but i assume it would like this a bit:
f:if condition{empty.cell} - or something. Never worked with it till yesterday and can't find any documentation that's helpful about my problem
An introduction to FLUID could be found here
A special hint for beginners:
If you want to know which FLUID variables are defined you can use of the f:debug Viewhelper:
<f:debug title="info at ...">{_all}</f:debug>
The exact syntax for your usage would be:
<f:if condition="{address.title}">
<f:then>{address.title}</f:then>
<f:else>-</f:else>
</f:if>

assign custom function call to variable

I am quite new to Typo3 and Fluid, but I guess what I am after is quite basic.
As an Analogy: Twig can extended, so it is possible to create a custom function which can be used like that:
{% set id = uid() %}
<input id="{{ id }}" … /><label for="{{ id }}">…</label>
Is such a thing possible with Fluid? Do I need to install other extensions, or can that be done with basic fluid extension?
In Fluid you have two ways to achieve this:
Prepare the desired data in the controller and assign it to the view as variable.
Create a custom viewhelper which contains the logic to build the desired data.
You can also combine these two and store the result of a viewhelper call in a variable using the f:variable viewhelper (more usage examples):
<f:variable name="myvariable">{acme:custom.viewhelper()}</f:variable>
In this specific case you can also install the VHS extension, the swiss army knife of Fluid, and use its UniqIdViewHelper Notice that you normally don't need this for forms since you bind them to (domain) objects you want to create/edit and let Extbase/Fluid handle the rest.

TYPO3: category tree of tx-news, render sub-tree only if main is selected

I override Templates/Styles/TWB/Templates/Category/List.html to have the category menu behave exactly as the sidebar menu in the introduction package (submenus show only if main is selected).
If I add the static template "News Styles Twitter Bootstrap (news)" I'm almost there, it is not difficult to open the sub-categories only if the main category is selected (I need a massive amount of categories/sub-categories), but it should also be open when a sub is selected:
...
<f:if condition="{0:category.item.uid,1:category.item.uid} == {0:overwriteDemand.categories,1:category.children.{overwriteDemand.categories}.parent}">
<f:if condition="{category.children}">
<f:render section="categoryTree" arguments="{categories: category.children,overwriteDemand:overwriteDemand,class:''}" />
</f:if>
</f:if>
...
I do not know how to use {overwriteDemand.categories} as key to match the value ... can anybody point to the proper syntax
update: I tried to apply a custom ViewHelper as this post suggests, but using TYPO3 V7.6.16 got stuck with the error should be compatible with TYPO3\CMS\Fluid\Core\ViewHelper\AbstractConditionViewHelper:‌​:render(), so could not try working with the variable overwriteDemand.categories in another way ...
Dynamic access to variables in fluid is just possible since TYPO3 8. Check out the Whats New Slides here: https://typo3.org/download/release-notes/whats-new/ and search for "Dynamic variable name parts" in the PDF.
In TYPO3 7, the "vhs"-Extension provides a ViewHelper to do the same job but nesting a ViewHelper within an complex f:if-condition is even more difficult. If possible, try out TYPO3 8. If it's not possible you may write your own ViewHelper to solve this logical problem.

Best way to declare content element uids to use them in fluid templates

I want to display some contentlement on every page with fluid templates. My solution was to create a typoscript element like:
lib.my.myelement= CONTENT
lib.my.myelement {
table = tt_content
select {
pidInList = 15
where = uid=99
}
}
and then use it in the fluid template with:
<f:cObject typoscriptObjectPath="lib.my.myelement" />
Another way is to create them directly in fluid with v:content.render or v:content.get. In this example I get all content elements on one page and save them in an array:
<v:variable.set name="contentElements" value="{v:content.get(column:'0', limit:'3', pageUid:'9', render:'FALSE')}" />
I can not use the first solution, cause on this page there will be much different languages ( I could use it, but every time a new language is added I would have work to declare all new uids of the new elements in typoscript).
The second solution would work better, cause when you translate a page the element uids change, but the pageUid does not change. But here I have the problem that I don't just need all elements, I also have to identify each element.
Here is how I use the second solution:
<v:variable.set name="contentElements" value="{v:content.get(column:'0', limit:'3', pageUid:'9', render:'FALSE')}" />
<f:for each="{contentElements}" as="contentElement" iteration="footerIteration">
<div class="column{v:math.sum(a: footerIteration.index, b: 1)}">
<v:content.render contentUids="{0:contentElement.uid}" />
</div>
</f:for>
As you can see, I use every element the same way.
lets create a simple case:
I have a page with the pageUid = 10 (this page will have translations). On every of this translations will be 3 elements. Lets say 1 element got the "id" (with id I mean something how I can identify this element) title1 and the other two elements have the id list1 and list2.
And my question now is How could I access for example list1?
I hope its understandable, if something is not clear please leave a comment.
Edit: I could use the same order on every translation for the elements and then check the iteration in the for-loop (see the second solution) to identify them. But I wonder if there a better, cleaner way.

TYPO3 fluid array key increment by 1

In fluid template I am displaying items list
<f:for each="{myItems}" as="myItem" key="key">
{key}. myItem.name
</f:for>
Here I need to display serial number for each item.
for example,
1. myitem one
2. myitem two
etc.
But in my case I used {key} for serial number. But its starting from 0.
0. myitem one
1. myitem two
So how to increment key by 1 for displaying only ?
Thanks.
You can use iteration="" property and then cycle. It starts from 1 instead of 0.
<f:for each="{myItems}" as="myItem" iteration="itemIterator">
{itemIterator.cycle}. myItem.name
</f:for>
tip: f:for iterator contains other useful properties, like isFirst, isOdd etc.
Check the wiki for more datails
In Fluid standalone and TYPO3v8 and above:
<f:for each="{myItems}" as="myItem" key="key">
{key + 1}. myItem.name
</f:for>
The key (no pun intended) is the MathExpressionNode and any implementation of Fluid which intentionally disables this implementation will not support this expression - in those cases, iterator.cycle is your friend.