Skip to content

Commit f8fa0ca

Browse files
committed
add: "Using RichText fields in templates" and "Alternative transformations"
1 parent 357f0cd commit f8fa0ca

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

docs/backend/fields.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,62 @@ context.body = RichTextValue("Some input text", 'text/plain', 'text/html')
232232
The standard widget used for a `RichText` field will correctly store this type of object for you, so it is rarely necessary to create one yourself.
233233

234234

235+
##### Using RichText fields in templates
236+
237+
If you use a `DisplayForm`, the display widget for the `RichText` field will render the transformed output markup automatically.
238+
If you write TAL manually, you may try something like the following.
239+
240+
```xml
241+
<div tal:content="structure context/body" />
242+
```
243+
244+
This, however, will render a string as follows.
245+
246+
```html
247+
RichTextValue object. (Did you mean <attribute>.raw or <attribute>.output?)
248+
```
249+
250+
The correct syntax is:
251+
252+
```xml
253+
<div tal:content="structure context/body/output" />
254+
```
255+
256+
This will render the cached, transformed output.
257+
This operation is approximately as efficient as rendering a simple `Text` field, since the transformation is only applied once, when the value is first saved.
258+
259+
260+
##### Alternative transformations
261+
262+
Sometimes, you may want to invoke alternative transformations.
263+
Under the hood, the default implementation uses the `portal_transforms` tool to calculate a transform chain from the raw value's input MIME type to the desired output MIME type.
264+
If you need to write your own transforms, take a look at [this tutorial](https://5.docs.plone.org/develop/plone/misc/portal_transforms.html).
265+
This is abstracted behind an `ITransformer` adapter to allow alternative implementations.
266+
267+
To invoke a transformation in code, you can use the following syntax.
268+
269+
```python
270+
from plone.app.textfield.interfaces import ITransformer
271+
272+
transformer = ITransformer(context)
273+
transformedValue = transformer(context.body, 'text/plain')
274+
```
275+
276+
The `__call__()` method of the `ITransformer` adapter takes a `RichTextValue` object and an output MIME type as parameters.
277+
278+
If you write a page template, there is an even more convenient syntax.
279+
280+
```xml
281+
<div tal:content="structure context/@@text-transform/body/text/plain" />
282+
```
283+
284+
The first traversal name gives the name of the field on the context (`body` in this case).
285+
The second and third give the output MIME type.
286+
If the MIME type is omitted, the default output MIME type will be used.
287+
288+
```{note}
289+
Unlike the `output` property, the value is not cached, and so will be calculated each time the page is rendered.
290+
```
235291

236292

237293

0 commit comments

Comments
 (0)