You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note that `Content` is *immutable* and methods will return new instances rather than updating the current instance.
319
358
320
359
360
+
### Markup variables
361
+
362
+
You may be tempted to combine markup with Python's f-strings (or other string template system).
363
+
Something along these lines:
364
+
365
+
```python
366
+
classWelcomeWidget(Widget):
367
+
defrender(self) -> RenderResult:
368
+
name ="Will"
369
+
returnf"Hello [bold]{name}[/bold]!"
370
+
```
371
+
372
+
While this is straightforward and intuitive, it can potentially break in subtle ways.
373
+
If the 'name' variable contains square brackets, these may be interpreted as markup.
374
+
For instance if the user entered their name at some point as "[magenta italic underline]Will is Cool!" then your app will display those styles where you didn't intend them to be.
375
+
376
+
We can avoid this problem by relying on the [Content.from_markup][textual.content.Content.from_markup] method to insert the variables for us.
377
+
If you supply variables as keyword arguments, these will be substituted in the markup using the same syntax as [string.Template](https://docs.python.org/3/library/string.html#template-strings).
378
+
Any square brackets in the variables will be present in the output, but won't change the styles.
You can experiment with this feature by entering a dictionary of variables in the variables text-area.
387
+
388
+
Here's what that looks like:
389
+
390
+
```{.textual path="docs/examples/guide/content/playground.py" lines=16 type="hello [bold]$name[/bold]!\t{'name': '[magenta italic underline]Will is Cool!'}"]}
391
+
```
392
+
321
393
## Rich renderables
322
394
323
-
Textual supports Rich renderables, which means you can return any object that works with Rich, such as Rich's [Text](https://rich.readthedocs.io/en/latest/text.html) object.
395
+
Textual supports Rich *renderables*, which means you can display any object that works with Rich, such as Rich's [Text](https://rich.readthedocs.io/en/latest/text.html) object.
396
+
397
+
The Content class is preferred for simple text, as it supports more of Textual's features.
398
+
But you can display any of the objects in the [Rich library](https://github.com/Textualize/rich) (or ecosystem) within a widget.
399
+
400
+
Here's an example which displays its own code using Rich's [Syntax](https://rich.readthedocs.io/en/latest/syntax.html) object.
0 commit comments