datastar/site/static/md/reference/expression_context.md

1.1 KiB

Expression Context

Datastar is not exposed in the global scope. Everything you need to do should be possible via Datastar expressions in attribute plugins and datastar-execute-script SSE events.

Every Datastar expression is evaluated using a context ctx. This means that ctx is exposed in expressions and can be used to access properties and methods within the current context.

You should use ctx sparingly, and only when you can't achieve the desired behavior using signals.

ctx.el

The current element being processed.

ctx.signals

The signals root object that contains functions for accessing and modifying signals. Signals can be accessed using the signal method. The following is the equivalent of an expression containing only $foo.

<div data-signals-foo="1">
  <div data-text="ctx.signals.signal('foo').value"></div>
</div>

Here is how you can output the existing signals in JSON format, which can be useful when troubleshooting an issue.

<pre data-text="ctx.signals.JSON()"></pre>