Check if pid of current page is X - typo3

How can I check if a page has a specific ID, and if true output a text?
I thought about something like this (pseudocode):
<f:if condition="{current.page.uid}=='78'">
<p>I am Page 78</p>
</f:if>

If you want to use this in an FLUIDTEMPLATE (page.10 = FLUIDTEMPLATE as example) you can access the page data with {data.uid}.
<f:if condition="{data.uid} == 78">
<p>I am Page 78</p>
</f:if>
In an extbase Extension you can make it like #dimitri-l says.

You can fetch current page ID via typoscript object
typoscript:
lib.currentPageId = TEXT
lib.currentPageId.data = TSFE:id
FLUID:
<f:if condition="{f:cObject(typoscriptObjectPath:'lib.currentPageId')}==78">
<p>I am Page 78</p>
</f:if>

You can make a variable into your "page TS setup" like here :
variables {
pageUid = TEXT
pageUid.field = uid
...
}
So you can make your fluid condition as here :
<f:if condition="{pageUid}=={settings.homepid}">
<p>I am Page 78</p>
</f:if>
for exemple...

You would need to pass the page id to the fluid template. If you are using an Extbase controller you can pass $GLOBALS['TSFE']->id to your view and then use an if condition as you did.
$this->view->assign('pageId', $GLOBALS['TSFE']->id);
I am not sure if it is already possible to do string comparison in Typo3 6.2, if not, you have to compare it that way:
<f:if condition="{0:pageId} == {0:'78'}>
...
</f:if>
Otherwise this is a clean solution for current versions
<f:if condition="{pageId} == '78'>
...
</f:if>

With the VHS viewhelper you can do it with fluid only, no need for a Typoscript helper:
{namespace v=FluidTYPO3\Vhs\ViewHelpers}
<f:if condition="{v:page.info(field: 'uid')} == '21'">
<f:then>
Shows only if page ID equals 21.
</f:then>
</f:if>

Sometimes you need to give a link a class when the link target is the current page (here: uid=1). The inline version helps:
<f:link.page pageUid="1"
class="{f:if(condition: '{data.uid} == 1', then: ' current')}">
Link
</f:link.page>

Related

How to render an Image coming from TYPO3\CMS\Frontend\DataProcessing\FilesProcessor in Fluid Typo3 9?

I am trying to get an Image from a custom content element into my fluid template. To achieve this I am using FilesProcessor.
custom element
Here is the corresponding code snippet from my setup.typoscript:
tt_content {
heiner_newcontentelement =< lib.contentElement
heiner_newcontentelement {
templateName = Default
dataProcessing.10 = TYPO3\CMS\Frontend\DataProcessing\FilesProcessor
dataProcessing.10 {
as = images
references.fieldName = image
references.table = tt_content
sorting = title
sorting.direction = descending
}
}
}
Showing what's available via f:debug-Viewhelper provides the following:
available variables in fluid template
So far, so good, what I have tried was to simply output the image in my fluid template with the following code:
<html data-namespace-typo3-fluid="true" xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"></html>
<f:layout name="Default" />
<f:section name="content">
<ul class="Das ist gut">
<f:for each="{images}" as="image">
<f:debug>{image}</f:debug>
<f:image image="{image}" height="250c" width="200c"/>
</f:for>
</ul>
</f:section>
</html>
although the variable "image" in the for-each loop is not empty, the image-viewhelper wont render anything. I am so desperate :( Somebody knows whats going wrong here? A lot of thx :)
According to the documentation, File Reference information are not always available in the fluidtemplate (see also https://docs.typo3.org/m/typo3/reference-coreapi/9.5/en-us/ApiOverview/Fal/UsingFal/Frontend.html#fluidtemplate).
What happens if you try this:
<ul class="Das ist gut">
<f:for each="{images}" as="image">
<f:debug>{image}</f:debug>
<f:image image="{image.originalFile}" height="250c" width="200c"/>
</f:for>
</ul>

list items per property (here date) in the list view

i have a simple extbase extension with a model "item".
Each item has a title, a description and a date.
How can i list my items according to the date?
e.G.
24.06.17:
item 2
item 6
item 7
25.06.14:
item 1
...
i search the logic to realize this (in the view or in the controller)
In your code you can sort your record by date.
In fluid you can use the vhs viewhelpers set:
https://fluidtypo3.org/viewhelpers/vhs/master/Variable/SetViewHelper.html
Something likes (I don't test it):
<f:for each="items" as="item">
<f:if condition="{item.date}=={current}">
<f:then>
{item.title}
</f:then>
<f:else>
<br/>{item.date}: {item.title}
</f:else>
</f:if>
<v:variable.set name="current" value="{item.date}" />
</f:for>
You can use vhs viewhelpers like below.
<f:for each="{dataset -> vhs:iterator.sort(sortBy: 'date')}" as="item">
.........
.........
</f:for>

How to assign variable in fluid?

I want viewhelper that can be helpful to assign variable in fluid, I dont want variable to be passed from controller.
Install extension called vhs from TYPO3 repository
Define namespace like following at the top of your fluid template
{namespace v=FluidTYPO3\Vhs\ViewHelpers}
Then use set viewhelper
<v:variable.set name="test" value="12345" />
Value of test : {test}
{test} will return value 12345
For registering global variable
<v:variable.register.set name="test" value="12345"/>]
Get value of global variable
Value of global variable : <v:variable.register.get name="test">
Since TYPO3 8.7, fluid introduces viewhelper for the variable (No need
of VHS)
<f:variable name="myvariable">My variable's content</f:variable>
<f:variable name="myvariable" value="My variable's content"/>
With inline style usage
{f:variable(name: 'myvariable', value: 'My variable\'s content')}
{myoriginalvariable -> f:variable.set(name: 'mynewvariable')}
Since TYPO3 8.6, this is possible without extension "vhs":
<f:variable name="myvariable" value="My variable's content"/>
See https://docs.typo3.org/typo3cms/extensions/core/Changelog/8.6/Feature-79402-VariableViewHelperForFluid.html
Since first fluid version it is possible to define variables for a special area: there is the VH f:alias which enables you to define new variables in the range of this VH. And that is the difference to the variable.set VH from ext:vhs.
<f:alias map="{firstName: 'John', lastName: 'Doe'}">
<p>Hello, my name is {firstName} {lastName}</p>
</f:alias>
<f:for each="{users}" as="user" iteration="iterator">
<f:if condition="{iterator.isFirst}">
<v:variable.set name="firstName">{user.firstName}</v:variable.set>
<v:variable.set name="lastName">{user.lastName}</v:variable.set>
</f:if>
:
do other output
:
</f:for>
<p>the first user was {firstName} {lastName}.</p>
The problem with setting variables inside the fluid is the possibility to do programming logic inside fluid:
<v:variable.set name="counter" value="0">
<f:for each="records" as="record">
<f:if condition="{record.flag}">
<v:variable.set name="counter">
<f:cObject typoscriptObjectPath="lib.calc">
{counter}+1
</f:cObject>
</v:variable.set>
</f:if>
</f:for>
<p>there are {counter} records with a flag set.</p>
with this typoscript
lib.calc = TEXT
lib.calc.current = 1
lib.calc.prioriCalc = 1

How to use typoscript object in fluid template as if condition

I have a typoscript TEXT object:
lib.myid = TEXT
lib.myid.value = 1413
And want to use it in a fluid template (for tx_news):
<f:for each="{newsItem.categories}" as="category">
<f:if condition="{category.uid} == {lib.myid}">
Category ID is the same as myid
</f:if>
</f:for>
How can I do this?
You can use the ViewHelper f:cObject. It would look like this:
<f:for each="{newsItem.categories}" as="category">
<f:if condition="{category.uid} == {f:cObject(typoscriptObjectPath: 'lib.myid')}">
Category ID is the same as myid
</f:if>
</f:for>
Alternatively, you can make the value a setting of the news plugin, by setting
plugin.tx_news {
settings {
valuefromlibrary < lib.myid
}
}
Afterwards, you can access the value using {settings.valuefromlibrary} in your template. But beware that the settings are not automatically passed on to partials, so in a partial the value might not be defined. This restriction is circumvented in the default template of EXT:news by passing the settings-variable to partials.

How do I check for column content in a typo3 fluid template?

I have a typo3 fluid template in which I want to check if content exists before rendering some elements.
Is there an efficient way to do this?
eg.
<f:if condition="{contentInColPos0}">
<div class="section-content">
<f:render section="Main" />
</div>
</f:if>
Is there a built-in variable or a simple way to check content exists in a column position?
This is a common task in CMS templating (don't render this part unless there's something to show), but I can't seem to find how to do it simply.
There is no easy way to do that. But you can use some TypoScript and then pass the count to Fluid and use it in a condition:
lib.countContent = CONTENT
lib.countContent {
table = tt_content
select {
selectFields = count(uid) AS count
pidInList = this
andWhere = (deleted = 0 AND hidden = 0)
}
renderObj = COA
renderObj {
10 = TEXT
10 {
data = field:count
}
}
This object will output the number of content rows on the given page and can be accessed in Fluid:
<f:if condition="{f:cObject(typoscriptObjectPath:'lib.countContent')} > 0">
Then show some stuff
</f:if>
If you're going to use the content anyway and don't have global wrap in your content object, you can also use it directly because the Fluid IfViewHelper checks for empty strings. So e.g. this might be working even better:
lib.content < styles.content.get
(This object is empty if there is no content)
<f:if condition="{f:cObject(typoscriptObjectPath:'lib.content')}">
<f:then>
<f:format.html>{lib.content}</f:format.html>
</f:then>
<f:else>
No content found
</f:else>
</f:if>
Easyest way to check this with VHS and without TypoScript:
<f:if condition="{v:content.get(column:6) -> v:iterator.first()}">
<div class="myAmazingClass">
</div>
</f:if>
You can slide the content throug subpages if you want:
<f:if condition="{v:content.get(column:6, slide:'-1') -> v:iterator.first()}">
<div class="myAmazingClass">
</div>
</f:if>
Consider VHS with ViewHelpers https://fluidtypo3.org/viewhelpers/vhs/master/Content/GetViewHelper.html or https://fluidtypo3.org/viewhelpers/vhs/master/Content/RenderViewHelper.html combined with using the as argument and an f:if condition to check the assigned variable for being empty.
You can solve this easily:
In your TypoScript file:
lib.contentInColPos0 < styles.content.get
lib.contentInColPos0 t.select.where = colPos = 0
In your Template file:
<f:if condition="{f:cObject(typoscriptObjectPath:'lib.contentInColPos0')}">
<div class="section-content">
<f:render section="Main" />
</div>
</f:if>