TYPO3 Fluid Templates Variables Bit-Operator - typo3

I wonder how to do some bit-wise math in a Fluid template. Like a bit-wise AND:
<f:if condition="{var & 2}"> ... </f:if>
But the {var & 2} expression isn't evaluated and remains as a string.
I couldn't find a viewhelper so far. I guess I have to write one?

Related

TYPO3 - how to properly define constant, store it into variable and use inside of fluid template

In the Fluid template of plugin I am working on, some things are hardcoded. For instance:
<f:else>
<li>
<v:page.link pageUid="114" />
</li>
</f:else>
Since pageUid values are not same on test and production server I would like to make this more dynamic.
I would like to store this somehow in variable and then use the variable in the fluid template.
I just dont know hot to make this and where in TYPO3.
Thanks in advance!
Because it is an setting do it like this:
Constants:
plugin.myext.settings.detailPid = 123
Setup:
plugin.myext.settings.detailPid = {$plugin.myext.settings.detailPid}
Variables are for variable content. If you have the same PID using variables with TEXT or similiar is overdressed and settings are the correct way.
Also variables are only accessable for FLUID_TEMPLATE content element, not for plugins!
Also in your extbase controller you can access these settings by simple access $this->settings['detailPid']without to render the cObjects first.
In your fluid you can access settings by {settings.detailPid}.
In typoscript template for your content object FLUIDTEMPLATE:
Typoscript setup/configuration:
10 = FLUIDTEMPLATE
10 {
variables {
pageUid = TEXT
pageUid.value = 114
}
}
or using constants
Typoscript constants:
pageUid = 114
Typoscript setup/configuration:
10 = FLUIDTEMPLATE
10 {
variables {
pageUid = TEXT
pageUid.value = {$pageUid}
}
}
Then you can fetch pageUid in your Fluid HTML
<f:else>
<li>
<v:page.link pageUid="{pageUid}" />
</li>
</f:else>
To use variables in a Fluid partial, make sure to pass these along, e.g. by providing _all:
<f:render partial="fluid_part_header" arguments="{_all}" />
If you use EXT:vhs anyways, you can do the following to:
TS-Constants:
pageUid=114
TS-Setup:
settings.pageUid = {$pageUid}
Template (Fluid)
<f:else>
<li>
<v:page.link pageUid="{v:variable.typoscript(path: 'settings.pageUid')}" />
</li>
</f:else>
This will make it available for all FLUID Templates.
Please consider using jokumer's solution.
randomresult's solution would work, but I wouldn't suggest it. You don't need vhs to pass variables.
Not talking about Paul Beck`s solution, because it wouldn't work at all. Not anymore at least. (See TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper, https://docs.typo3.org/typo3cms/ExtbaseGuide/Fluid/ViewHelper/CObject.html). It accepts only content objects.
Set your Pid in constants like PID_SOME_PAGE = 123 and set a variable in your plugins settings. Like this:
plugin.tx_yourplugin {
...
settings{
somePage = {$PID_SOME_PAGE}
}
...
}
You can bypass that constants version if you want and set your page id in settings directly. It's just a cleaner way in my opinion, especially for larger websites.
Then you can use that variable in your template, like this <f:link.page pageUid="{settings.somePage}">Go to Page</f:link.page>. More options for f:link.page here: https://docs.typo3.org/typo3cms/ExtbaseGuide/Fluid/ViewHelper/Link/Page.html

How to define array of numbers and output it?

How can I define an array of numbers and output each number in a for loop?
I tried it like this:
<f:alias map="{numbers: [1,2,3,4,5,6]}">
<f:for each="{numbers}" as="number">
<p>{number}</p>
</f:for>
</f:alias>
Result:
The argument "map" was registered with type "array", but is of type "string" in view helper "TYPO3\CMS\Fluid\ViewHelpers\AliasViewHelper"
And like this:
<f:alias map="{v:iterator.explode(content: '1,2,3,4,5,6')}">
<f:for each="{content}" as="zahl">
<p>{zahl}</p>
</f:for>
</f:alias>
Result: No output.
<f:for each="{0:1, 1:2, 2:3, 3:4, 4:5, 5:6, 6:7}" as="foo">{foo}</f:for>
The ideal solution IF and only IF:
You use VHS.
You want numbers starting from 0 or 1 going to a max; or numbers calculated using those two starting indices (TYPO3 8.0+ supports math expressions in Fluid natively, earlier versions require VHS for this).
You want to loop the numbers, not consume them as an array.
Which seems to be exactly your use case...
Then, and only then, is the following the ideal solution in terms of both performance and minimising complexity:
<v:iterator.loop count="6" iteration="iteration">
{iteration.index} starts at zero, {iteration.cycle} starts at one.
</v:iterator.loop>
Don't forget the following either:
{f:render(section: 'OtherSection', arguments: {iteration: iteration})
-> v:iterator.loop(count: 6, iteration: 'iteration')}
Which is the most efficient way of rendering a section X number of times
with only the iteration variable being different. Sections or partials are the most efficient way to represent this exact type of code and the
inline syntax is the most efficient when parsing.
try this:
<f:alias map="{numbers: {1,2,3,4,5,6}}">
<f:for each="{numbers}" as="number">
<p>{number}</p>
</f:for>
</f:alias>
I was able to solve it with this code:
<html xmlns="http://www.w3.org/1999/xhtml" lang="en"
xmlns:v="http://typo3.org/ns/FluidTYPO3/Vhs/ViewHelpers"
v:schemaLocation="https://fluidtypo3.org/schemas/vhs-master.xsd">
<f:for each="{v:iterator.explode(content: '1,2,3,4,5,6')}" as="number">
<p>{number}</p>
</f:for>
Output:
1
2
3
4
5
6
Make sure that you have installed the extension VHS: Fluid ViewHelpers (extension key = vhs).
Make sure to include the extension in the partial.
Write this at the top:
<html xmlns="http://www.w3.org/1999/xhtml" lang="en"
xmlns:v="http://typo3.org/ns/FluidTYPO3/Vhs/ViewHelpers"
v:schemaLocation="https://fluidtypo3.org/schemas/vhs-master.xsd">
I am using Typo3 v6.2.25

How to disable automated encoding of special characters in fluid partials (TYPO3)

Should be simple enough. I'm trying to add an input field to a fluid partial in the extension "yag" (yet another gallery).
Input: <f:form.textfield id="live-filter" name="test" />
Output: <input id="live-filter" type="text" name="test" />
Somehow the code get's filtered along the way, but I don't know why.
TYPO3 v. 6.2
YAG v. 3.2.1
Edit: A wild guess would be some output filtering in TYPO3 itself, but where? I didn't set anything by purpose.
You need to traverse the path upwards to check if there is any fluid tag wrapped around it, that does escaping. In general, all tags do escaping.
Also check the code around <f:render partial....
It could also be that the TypoScript code that does calls the fluid template, has a .htmlspecialchars = 1 set.
Since TYPO3 8 there is another pitfall: Custom Viewhelpers do htmlspecialchars on the output unless told otherwise. The solution is:
<?php
namespace Vendor\ArTest\ViewHelpers;
class YourViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper{
/**
* As this ViewHelper renders HTML, the output must not be escaped.
*
* #var bool
*/
protected $escapeOutput = false;
As of TYPO3 ver. 9.5 and up to ver. 10.4 you could also wrap the output in the Fluid template into <f:format.htmlentitiesDecode> Tags like this:
<f:format.htmlentitiesDecode>
<f:form.textfield id="live-filter" name="test" />
</f:format.htmlentitiesDecode>
Further information on this can be found in the TYPO3 View Helper Reference.

Dynamicly change TYPO3 fluid Layout by typeNum

I am using the extension fluidpages and want to switch the layout by the typeNum.
is it possible to change the f:layout by by an condition?
This wont work:
<f:layout name="{f:if(condition: '{typeNum} == 666', then: 'PageAjax', else: 'Page')}"/>
Suggested approach:
<f:layout name="Page/{typeNum}"/>
Required files:
Resources/Private/Layouts/Page/0.html
Resources/Private/Layouts/Page/666.html
Please note: this only works if the {typeNum} variable is guaranteed to exist - if it does not, you will face a "template file not found" error with an empty filename. To avoid this, you can use the VHS extension's v:var.convert ViewHelper to ensure a proper value:
<f:layout name="Page/{typeNum -> v:var.convert(type: 'integer')}"/>
I got the same problem recently, and found the solution after a few researchings.
The problem is, you can not use nested fluid like <f:if>, <f:cObject> or others in <f:layout>. Otherwise, you will get a fatal error in the cache file, saying call to a member function getViewHelper() on a non-object. And when you look at the cache file, you will find it's because $self is not defined.
Therefore, my solution is, searching public function getLayoutName( in \TYPO3\CMS\Fluid\Core\Compiler\TemplateCompiler, and adding \$self = \$this; before \$currentVariableContainer = \$renderingContext->getTemplateVariableContainer();, just like generateCodeForSection()
I have now added the Condition in the Layout template. I get the typeNum from typoscript.
<f:if condition="{f:cObject(typoscriptObjectPath:'plugin.nc_template.settings.pageLayout')} == 'Default'">
<p>Default Template</p>
</f:if>
<f:if condition="{f:cObject(typoscriptObjectPath:'plugin.nc_template.settings.pageLayout')} == 'Ajax'">
<p>Ajax Template</p>
</f:if>
I found an example on the fedext Page, but could not get this to run:
https://fedext.net/blog/archive.html?tx_news_pi1[news]=55&tx_news_pi1[%40widget_0][currentPage]=8&cHash=f9c3165598a28d2aa98fd30ef115bb75
CanĀ“t you use an if-statement instead?
IMHO this is easier to read - and if you need to add more arguments which depends on typeNum, it would stay readable.
<f:if condition="{typeNum} == 666">
<f:then>
<f:layout name="PageAjax">
</f:then>
<f:else>
<f:layout name="Page">
</f:else>
</f:if>

Access dynamic object property in Typo3 Fluid template

I have an object in a Typo3 Fluid template and want to access a property on it, but the name of the property is in a variable someProperty:
<f:if condition="searchObject.{someProperty}">
Found!
</f:if>
Because this does not work: Is there a built in way to access a property by variable?
You can give a chance to fedex Fluid viewhelper collection and its v:var.get viewhelper.
Or check the other viewhelpers in here. If you do not find a suitable one, you can write your own for this functionality based on some example here.
UPDATE:
Since this is an old question to TYPO3 6.2 it is the time to update it to the current standards. (See also the answer of Claus Due:
For the current LTS: TYPO3 9.5, you can use simple:
{searchObject.{someProperty}}
In Fluid standalone and TYPO3v8 and upward:
{array.{variableContainingKey}}.
The point notation actually is the correct way to access a property. What do you mean by dynamic? Can be null? Have you tried the following?
<f:if condition="<f:count>{searchObject.someProperty}</f:count>">
Found!
</f:if>
Or maybe just:
<f:if condition="{searchObject.someProperty}">
Found!
</f:if>