Description: The x-text directive in AlpineJS allows you to display reactive data from your x-data scope as the text content of an HTML element. This text will update automatically whenever the underlying data changes. Python developers can think of this as similar to using template variables (e.g., {{ my_variable }}) in frameworks like Django or Flask, but it happens entirely on the client-side, driven by JavaScript.
The primary way to use this skill is through the x-text attribute:
x-text="propertyName":
This binds the text content of the HTML element to the value of propertyName defined in your Alpine component's x-data.
For example, if x-data="{ message: 'Hello Alpine!' }", then <span x-text="message"></span> will display "Hello Alpine!". If message changes, the text in the span will automatically update.
<div x-data="{ greeting: 'Welcome' }">
<p x-text="greeting"></p> <!-- Displays: Welcome -->
</div>
x-text="'Hello, ' + name" (JavaScript expressions):
You can also provide any valid JavaScript expression as the value for x-text. This is powerful for concatenating strings, performing simple calculations, or calling functions. Remember that if you are using literal strings within the expression, they must be enclosed in quotes (single or double).
<div x-data="{ user: { firstName: 'Alex', score: 100 } }">
<p x-text="'User: ' + user.firstName"></p> <!-- Displays: User: Alex -->
<p x-text="user.firstName + ' has a score of ' + user.score"></p> <!-- Displays: Alex has a score of 100 -->
</div>
Trying to render HTML with x-text:
x-text sets the textContent property of an HTML element. This means any HTML tags within your data (e.g., message: '<strong>Bold</strong>') will be displayed as literal strings (e.g., "<strong>Bold</strong>") rather than being rendered as HTML elements. If you explicitly need to render HTML from a data property, use the x-html directive. However, be cautious with x-html as it can expose your application to Cross-Site Scripting (XSS) vulnerabilities if the HTML source is not trusted or properly sanitized.
Forgetting quotes for literal strings in expressions:
When combining reactive data with static text, remember standard JavaScript string concatenation rules. Literal strings must be enclosed in quotes. For example, x-text="'User: ' + username" is correct. If you write x-text="User: " + username (without quotes around "User: "), JavaScript will try to find a variable named User, which will likely result in an error or unexpected behavior.
Assuming x-text automatically formats data:
x-text displays data exactly as it is. It does not perform any automatic formatting for dates, numbers, currency, etc. If you need to format data, you should either:
a) Pre-format it within your x-data component, possibly using JavaScript methods.
b) Use JavaScript formatting functions directly within the x-text expression (e.g., x-text="new Date(myDate).toLocaleDateString()").
Plain text display:
Using an expression:
The value of userName in x-data is currently: ""
Current count:
The following shows how This is <strong>bold</strong> text.x-text handles HTML tags. The data is: '
x-text:
x-html (use with caution!):
Notice how x-text displays the HTML tags as plain text, while x-html renders them.