Templating
==========
Alpine offers a handful of useful directives for manipulating the DOM on a web page.
Let's cover a few of the basic templating directives here, but be sure to look through the available directives in the sidebar for an exhaustive list.
[Text content](#text-content)
-----------------------------
Alpine makes it easy to control the text content of an element with the `x-text` directive.
**Start Here**
Now, Alpine will set the text content of the `
` with the value of `title` ("Start Here"). When `title` changes, so will the contents of `
`.
Like all directives in Alpine, you can use any JavaScript expression you like. For example:
3
The `` will now contain the sum of "1" and "2".
[→ Read more about `x-text`](/directives/text)
[Toggling elements](#toggling-elements)
---------------------------------------
Toggling elements is a common need in web pages and applications. Dropdowns, modals, dialogues, "show-more"s, etc... are all good examples.
Alpine offers the `x-show` and `x-if` directives for toggling elements on a page.
### [`x-show`](#x-show)
Here's a simple toggle component using `x-show`.
Content...
Content...
Expand
Content...
Now the entire `
` containing the contents will be shown and hidden based on the value of `open`.
Under the hood, Alpine adds the CSS property `display: none;` to the element when it should be hidden.
[→ Read more about `x-show`](/directives/show)
This works well for most cases, but sometimes you may want to completely add and remove the element from the DOM entirely. This is what `x-if` is for.
### [`x-if`](#x-if)
Here is the same toggle from before, but this time using `x-if` instead of `x-show`.
Content...
Content...
Expand
Notice that `x-if` must be declared on a `` tag. This is so that Alpine can leverage the existing browser behavior of the `` element and use it as the source of the target `
` to be added and removed from the page.
When `open` is true, Alpine will append the `
` to the `` tag, and remove it when `open` is false.
[→ Read more about `x-if`](/directives/if)
[Toggling with transitions](#toggling-with-transitions)
-------------------------------------------------------
Alpine makes it simple to smoothly transition between "shown" and "hidden" states using the `x-transition` directive.
> `x-transition` only works with `x-show`, not with `x-if`.
Here is, again, the simple toggle example, but this time with transitions applied:
Content...
Content...
Expands
Content...
Let's zoom in on the portion of the template dealing with transitions:
`x-transition` by itself will apply sensible default transitions (fade and scale) to the toggle.
There are two ways to customize these transitions:
* Transition helpers
* Transition CSS classes.
Let's take a look at each of these approaches:
### [Transition helpers](#transition-helpers)
Let's say you wanted to make the duration of the transition longer, you can manually specify that using the `.duration` modifier like so:
Expands
Content...
Now the transition will last 500 milliseconds.
If you want to specify different values for in and out transitions, you can use `x-transition:enter` and `x-transition:leave`:
Expands
Content...
Additionally, you can add either `.opacity` or `.scale` to only transition that property. For example:
Expands
Content...
[→ Read more about transition helpers](/directives/transition#the-transition-helper)
### [Transition classes](#transition-classes)
If you need more fine-grained control over the transitions in your application, you can apply specific CSS classes at specific phases of the transition using the following syntax (this example uses [Tailwind CSS](https://tailwindcss.com/)):
...
...
Expands
Content...
[→ Read more about transition classes](/directives/transition#applying-css-classes)
[Binding attributes](#binding-attributes)
-----------------------------------------
You can add HTML attributes like `class`, `style`, `disabled`, etc... to elements in Alpine using the `x-bind` directive.
Here is an example of a dynamically bound `class` attribute:
Toggle Red
As a shortcut, you can leave out the `x-bind` and use the shorthand `:` syntax directly: