Back to HOWL

Batteries Included

Build modular, think globally

Datastar was originally a branch of HTMX that was meant to make it more modular and easier to write plugins for. What was not expected is in creating what felt like essential plugins was actually able to recreate all the needed features of a full SPA frameworks, HTMX and Alpine and do it all in smaller package. While you can still build your own plugins, the core set of plugins is enough to build a full application. Let’s explore some more features

Evaluating data-* attributes

Before we had used something like data-text="$foo" attribute. What this is actually doing under the hood is turning the string into a function that is evaluated safely into (ctx)=> ctx.store.foo.value where ctx is the Datastar object that was injected into the page. In this case its connected directly to the contents of a signal but you could do any javascript that is valid. This also means you could eval to a constant string but that would looks something like data-text="'hello world'", notice the quotes, otherwise it would be looking for variable hello world which isn’t valid and would fail to evaluate. This is the one major gotcha of declarative code, you have to be careful about the context.

More attributes

So far we’ve seen data-store, data-model and data-text. There are many more attributes that can be used to make your page more reactive. Check out the API for a full list of attributes but here are a few more that get used the most.

data-show

This is a simple way to show and hide elements. It’s a simple way to do v-if in Vue or x-show in Alpine.

<div data-show="$foo">Hello World</div>

When the foo signal is truthy the div will be shown, otherwise it will be hidden.

data-on

This is a way to add event listeners to elements. It’s a simple way to do v-on in Vue or x-on in Alpine.

<button data-on-click="console.log('hello world')">Click me</button>

When the button is clicked it will log hello world to the console.

Actions

Actions are helper functions that are made available during a data-* attribute evaluation. They are a way to do more complex operations without having to write a lot of javascript. Here are a few of the most common actions. They are prefixed with $$ to avoid any conflicts with other attributes.

$$setAll

Sometimes you want to set multiple values at once. It takes a prefix regexp and a set of signals. For example if you have a form with a bunch of inputs and you want to set them all at once you could do something like this.

data-text="$$setAll('contact_',true)`

$$toggleAll

Works the same as $$setAll but will flip the value of the signals.

There can be many more actions and you can even write your own. But the majority of the time you’ll be using the actions that allow for backend integration, which we’ll cover in the next section.

Next STREAMING BACKEND