Description: The x-text directive allows you to show reactive data from your x-data scope as the text content of an HTML element. This text content updates automatically whenever the underlying data changes. For Python developers, this is similar to using template variables in frameworks like Django (e.g., {{ my_variable }}) or Flask (e.g., {{ my_variable }}), but it happens entirely on the client-side within the browser, powered by JavaScript reactivity.
The primary way to use this skill is via the x-text directive. It can accept either a property name or a JavaScript expression:
x-text="propertyName"This is the most straightforward usage. AlpineJS looks for a property named propertyName within its current x-data component scope. The textContent of the HTML element where x-text is applied will be set to the value of this property. If the property's value changes, the displayed text updates automatically.
Example:
<div x-data="{ message: 'Hello from Alpine!' }">
<span x-text="message"></span> <!-- Displays: Hello from Alpine! -->
</div>
If you later update message (e.g., via a button <button @click="message = 'Updated!'>Update</button>), the text inside the <span> will change to "Updated!".
x-text="'Hello, ' + name" (JavaScript expressions)x-text can evaluate any valid JavaScript expression. The result of this expression is then set as the element's text content. This enables:
x-text="'User: ' + username"x-text="count * price"x-text="formatDate(eventDate)"x-text="user.profile.firstName"x-text="`Welcome, ${user.name}! You have ${notifications.length} new messages.`"Example:
<div x-data="{ user: { firstName: 'Alex', points: 150 }, product: 'Widget' }">
<p x-text="'Greetings, ' + user.firstName + '!' "></p>
<!-- Displays: Greetings, Alex! -->
<p x-text="`You have ${user.points} points for purchasing a ${product}.`"></p>
<!-- Displays: You have 150 points for purchasing a Widget. -->
</div>
x-text.x-text sets the textContent property of an HTML element. This means any HTML tags within the data (e.g., <b>Bold</b>) will be displayed as literal strings (you'll see "<b>Bold</b>" on the page) rather than being rendered as HTML (Bold). This is a security measure to prevent Cross-Site Scripting (XSS) by default.
In Python web frameworks, you might use a |safe filter (Django) or similar to render HTML. In AlpineJS, if you explicitly need to render HTML from a trusted source, use the x-html directive (e.g., <div x-html="trustedHtmlContent"></div>). Be cautious with x-html if the content originates from users.
<div x-data="{ myHtml: '<strong>Important!</strong>' }">
<p>Using x-text: <span x-text="myHtml"></span></p>
<!-- Output: Using x-text: <strong>Important!</strong> -->
<p>Using x-html: <span x-html="myHtml"></span></p>
<!-- Output: Using x-html: Important! (rendered as bold) -->
</div>
When combining data with static text in a JavaScript expression within x-text, remember that literal strings must be enclosed in quotes (single ' ' or double " "). If you forget them, JavaScript will try to interpret the static text as a variable name, leading to errors.
For example, x-text="User: + username" is incorrect because User: will be treated as an undeclared variable. The correct way is x-text="'User: ' + username" or using template literals: x-text="`User: ${username}`".
<div x-data="{ username: 'PyFan' }">
<!-- Correct -->
<p x-text="'Username: ' + username"></p> <!-- Output: Username: PyFan -->
<!-- Incorrect (would cause a JavaScript error) -->
<!-- <p x-text="Username: + username"></p> -->
</div>
x-text automatically formats data.x-text displays data "as is". It does not automatically format numbers (e.g., currency, decimal places), dates, or other data types. Python developers accustomed to template filters (like Django's {{ value|date:"Y-m-d" }} or {{ amount|floatformat:2 }}) need to handle formatting using JavaScript.
You can format data either by preparing it within your x-data component (e.g., using methods or computed properties) or by applying JavaScript formatting functions directly within the x-text expression.
<div x-data="{
price: 49.955,
eventDate: new Date(),
formattedPrice() { return '$' + this.price.toFixed(2); }
}">
<p>Raw Price: <span x-text="price"></span></p>
<!-- Output: Raw Price: 49.955 -->
<p>JS formatting in x-text: <span x-text="price.toFixed(2)"></span></p>
<!-- Output: JS formatting in x-text: 49.96 -->
<p>Using a method: <span x-text="formattedPrice()"></span></p>
<!-- Output: Using a method: $49.96 -->
<p>Date (default): <span x-text="eventDate"></span></p>
<!-- Output: (long, default date string) -->
<p>Date (formatted): <span x-text="eventDate.toLocaleDateString()"></span></p>
<!-- Output: (e.g., 12/25/2023 in US locale) -->
</div>
Characters remaining: /
x-text Demonstrations:Application Name (direct property):
Greeting (concatenation & property):
Dynamic Welcome (template literal):
Using a method for formatted time:
HTML displayed as text:
(Note: The <em> tags are shown as text, not rendered as italics.)