Skip to content

Commit 4cb3650

Browse files
typo3-docs-backport-bot[bot]s2blinawolfsarahmccarthy123
authored
[DOCS] Add f:cObject example for TypoScript ContentObject (#201) (#206)
* [DOCS] Add f:cObject example for TypoScript ContentObject This has been moved from the TYPO3 API docs. * [DOCS] Various typos * Apply suggestion from @linawolf * Apply suggestion from @linawolf * Apply suggestion from @linawolf * Apply suggestion from @linawolf * Apply suggestion from @linawolf * Apply suggestion from @linawolf * [DOCS] Add headlines for TypoScript cObj example * [TASK] Language checks * [TASK] Add permalinks * Apply suggestion from @linawolf --------- (cherry picked from commit 11e0d06) Co-authored-by: Simon Praetorius <simon@praetorius.me> Co-authored-by: Lina Wolf <48202465+linawolf@users.noreply.github.com> Co-authored-by: Sarah McCarthy <sarahmccarthy123@yahoo.com>
1 parent 0691f5f commit 4cb3650

1 file changed

Lines changed: 135 additions & 0 deletions

File tree

Documentation/Global/CObject.rst

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,138 @@ When passing an object with `{data}`, the properties of the object are
116116
accessible with :typoscript:`.field` in TypoScript. If only a single value is
117117
passed or the `currentValueKey` is specified, :typoscript:`.current = 1`
118118
can be used in the TypoScript.
119+
120+
.. _typo3-fluid-cobject-contentobject:
121+
122+
Use data from a TypoScript ContentObject
123+
----------------------------------------
124+
125+
As an example of a more advanced use case, the cObject viewhelper can pass
126+
content data from a Fluid template to a content object defined in TypoScript. The
127+
following example demonstrates this with a user counter. The user counter is in
128+
a blog post (the blog post has a count of how many times it has been viewed.)
129+
130+
Add the viewhelper to your Fluid template. This can be done in 3 different
131+
ways. The basic tag syntax:
132+
133+
.. code-block:: html
134+
:caption: EXT:my_extension/Resources/Private/Templates/SomeTemplate.html
135+
136+
<f:cObject typoscriptObjectPath="lib.myCounter">{post.viewCount}</f:cObject>
137+
138+
Or a self-closing tag. Data is passed in the :html:`data` attribute.
139+
140+
.. code-block:: html
141+
:caption: EXT:my_extension/Resources/Private/Templates/SomeTemplate.html
142+
143+
<f:cObject typoscriptObjectPath="lib.myCounter" data="{post.viewCount}" />
144+
145+
Or inline notation, which is easy to read and understand (from left to right):
146+
147+
.. code-block:: html
148+
:caption: EXT:my_extension/Resources/Private/Templates/SomeTemplate.html
149+
150+
{post.viewCount -> f:cObject(typoscriptObjectPath: 'lib.myCounter')}
151+
152+
Then, add TypoScript (to your TypoScript template) to process this data. In our
153+
example below, the :typoscript:`stdWrap` attribute :typoscript:`current`
154+
works like a switch: if set to 1, it contains the value that was passed to the
155+
TypoScript object from the Fluid template:
156+
157+
.. code-block:: typoscript
158+
:caption: EXT:my_extension/Configuration/TypoScript/setup.typoscript
159+
160+
lib.myCounter = TEXT
161+
lib.myCounter {
162+
current = 1
163+
wrap = <strong>|</strong>
164+
}
165+
166+
This TypoScript snippet outputs the current number of visits in bold.
167+
168+
We can easily modify this TypoScript to output the user counter as an image instead
169+
of text:
170+
171+
.. code-block:: typoscript
172+
:caption: EXT:my_extension/Configuration/TypoScript/setup.typoscript
173+
174+
lib.myCounter = IMAGE
175+
lib.myCounter {
176+
file = GIFBUILDER
177+
file {
178+
10 = TEXT
179+
10.text.current = 1
180+
}
181+
}
182+
183+
.. _typo3-fluid-cobject-contentobject-typoscript:
184+
185+
Passing objects to TypoScript
186+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
187+
188+
Up till now, we have only been passing a single value to the TypoScript object.
189+
However, it is also possible to pass multiple values, useful for selecting which value to
190+
use or concatenating values, or you can pass objects (here the `post` object):
191+
192+
.. code-block:: html
193+
:caption: EXT:my_extension/Resources/Private/Templates/SomeTemplate.html
194+
195+
{post -> f:cObject(typoscriptObjectPath: 'lib.myCounter')}
196+
197+
But how do we access the object's properties in our TypoScript? By setting the
198+
:typoscript:`field` property of :typoscript:`stdWrap` (in a :typoscript:`COA`):
199+
200+
.. code-block:: typoscript
201+
:caption: EXT:my_extension/Configuration/TypoScript/setup.typoscript
202+
203+
lib.myCounter = COA
204+
lib.myCounter {
205+
10 = TEXT
206+
10.field = title
207+
20 = TEXT
208+
20.field = viewCount
209+
wrap = (<strong>|</strong>)
210+
}
211+
212+
This TypoScript snippet outputs the title of the blog and the number of page visits in parentheses.
213+
214+
.. _typo3-fluid-cobject-contentobject-typoscript-current:
215+
216+
Using `current` to pass values
217+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
218+
219+
It is also possible to use the :typoscript:`current` switch here.
220+
If you set the property :html:`currentValueKey`
221+
in the cObject ViewHelper, the value will be available in
222+
the TypoScript template as :typoscript:`current`. That is especially useful
223+
when you want to emphasize that the value is very
224+
*important* for the TypoScript template. For example, the
225+
*number of visits* is significant in our view
226+
counter:
227+
228+
.. code-block:: html
229+
:caption: EXT:my_extension/Resources/Private/Templates/SomeTemplate.html
230+
231+
{post -> f:cObject(typoscriptObjectPath: 'lib.myCounter', currentValueKey: 'viewCount')}
232+
233+
Then, in the TypoScript template, use :typoscript:`field` aswell as
234+
:typoscript:`current` to output the value of the view count. The following
235+
TypoScript snippet outputs the same information as above:
236+
237+
.. code-block:: typoscript
238+
:caption: EXT:my_extension/Configuration/TypoScript/setup.typoscript
239+
240+
lib.myCounter = COA
241+
lib.myCounter {
242+
10 = TEXT
243+
10.field = title
244+
20 = TEXT
245+
20.current = 1
246+
wrap = (<strong>|</strong>)
247+
}
248+
249+
Using :typoscript:`current` makes the TypoScript easier to read and the logic
250+
easier to understand.
251+
252+
In summary, the cObject ViewHelper is a powerful option to embed TypoScript
253+
expressions in Fluid templates.

0 commit comments

Comments
 (0)