The ~VUE sigil
use PhoenixVapor imports the ~VUE sigil, which works anywhere ~H does: in a LiveView's render/1 or in a function component. Like ~H, it needs assigns in scope.
def render(assigns) do
~VUE"""
<div :class="status">
<h1>{{ title }}</h1>
<ul>
<li v-for="item in items" :key="item.id">{{ item.name }}</li>
</ul>
<p v-if="show_footer">{{ footer }}</p>
</div>
"""
endThe template compiles at compile time to static HTML and dynamic slots. At render time, each slot is evaluated against the assigns and re-evaluated only when an assign it reads has changed, so LiveView sends the same small diffs it does for HEEx.
Syntax
PhoenixVapor supports these parts of Vue's template syntax:
- Interpolation:
{{ expr }} - Attributes:
:class="expr",:href="expr", and other bound attributes - Events:
@click="save"becomesphx-click="save"; handle it withhandle_event/3 - Conditionals:
v-if,v-else-if,v-else, andv-show - Lists:
v-for="item in items"with:key - Forms:
v-model="name"binds the value and sendsname_changedon change - Raw HTML:
v-html
Expressions
Expressions read assigns by name; user.name and items[0] work on maps and lists. Most expressions are evaluated in Elixir: comparisons, arithmetic, &&, ||, !, ternaries, template literals, .length, join and includes on lists, and trim, toUpperCase, toLowerCase, includes, startsWith, and endsWith on strings. Anything else, such as .filter(...) with a callback, is evaluated in QuickBEAM on every render, and reported as a warning when compiling, with the globals Vue allows such as Math and JSON. Expressions follow JavaScript's semantics: 0 and "" are falsy, and a name that isn't an assign is undefined, as Vue treats an unknown name; an expression that fails, such as a call to something that isn't a function, raises PhoenixVapor.ExpressionError with its file, line, and column.
Values display as in Vue: null as nothing, and objects and lists as JSON. Attributes follow Vue's server renderer: null and a false boolean attribute such as disabled leave the attribute out, and class and style take objects and arrays.
Components
In a .vue file, a component imported from another .vue file renders on the server: its template is compiled with the parent's, its props become its assigns, slot content renders with the parent's assigns, and other attributes fall through to its root element, with class and style merged. Imports resolve as in the browser build, relative to the file or through Volt aliases such as @/.
<script setup>
import Card from "@/ui/Card.vue"
</script>
<template>
<Card :title="post.title" class="mt-4">
<p>{{ post.body }}</p>
</Card>
</template>Components from packages
A component from a package, such as a Reka UI primitive, gets its markup from its JavaScript. In a hybrid component, when everything it receives is known at compile time, Vue's server renderer runs it once in QuickBEAM while the template compiles, and its HTML becomes part of the template. Rendering then runs no JavaScript.
<script setup>
import { ref } from "vue"
import { TooltipProvider, TabsRoot, TabsList, TabsTrigger, TabsContent } from "reka-ui"
const tab = ref("general")
</script>
<template>
<TooltipProvider>
<TabsRoot v-model="tab">
<TabsList>
<TabsTrigger value="general">General</TabsTrigger>
</TabsList>
<TabsContent value="general"><p>{{ project.name }}</p></TabsContent>
</TabsRoot>
</TooltipProvider>
</template>TooltipProvider renders only its content, and the Tabs render with Reka's markup and ARIA attributes. Package components inside one another render together, so parts such as TabsList get their parent's context. The template's own content inside them, such as {{ project.name }}, stays dynamic. So does a v-for or v-if of the template's own: a package part inside it, such as a TooltipRoot per row, renders once in its ancestors' context, and the loop repeats that markup.
This is for hybrid mode, where the server renders the first paint and Vue takes over in the browser. Known values are static props, literals, macro results, the initial values of refs, which the browser renders first too, and expressions of those, such as :open="selected !== null" while selected starts as null.
In other modes nothing takes over in the browser, so frozen markup from a component with behavior, such as tabs whose triggers never switch, would look interactive and do nothing. There, a package component renders only when it renders just its content, as a provider such as TooltipProvider does, and any other is a compile error.
A package component that receives a value known only when rendering, such as :open="row.open" inside a v-for, or that passes props to its slot content, can't render on the server. In hybrid mode it's left out of the first render, with a compile-time warning that says why, and appears when the browser mounts the component. In other modes it's a compile error.
Macros
A helper imported with the type: "macro" import attribute, the convention Bun and unplugin-macros use, may run while the template compiles:
<script setup lang="ts">
import { button } from "./variants" with { type: "macro" }
const props = defineProps<{ variant?: "solid" | "ghost" }>()
</script>
<template>
<button :class="button({ variant: props.variant })"><slot /></button>
</template>When everything a call reads is known at compile time, such as a prop a parent passes as a constant (<Button variant="ghost">) or doesn't pass at all, the call runs once in QuickBEAM and its result is compiled into the template, so rendering runs no JavaScript. This suits variant helpers such as tailwind-variants. The browser build imports the helper as usual.
When a call reads props known only when rendering, such as <Button :variant="row.variant">, and their TypeScript types are finite sets of literals, the call runs once for each combination of values, up to 64, and rendering looks the result up. The types are resolved with TypeScript's own checker from the project's node_modules, so a type derived from a variants config works too:
<script setup lang="ts">
import { badge, type BadgeProps } from "./variants" with { type: "macro" }
// "neutral" | "success" | "warning" | "danger"
const props = defineProps<{ tone?: BadgeProps["tone"] }>()
</script>A value outside the type raises PhoenixVapor.ExpressionError when rendering. A call that depends on anything else, such as a prop typed string, is a compile error, or a warning in hybrid mode, where the browser renders it.
Components from assigns
Elsewhere, such as in ~VUE, a capitalized tag renders a component from the __components__ assign, a map from tag name to a function that takes assigns and returns rendered content. Default slot content is passed as inner_block:
assign(socket, __components__: %{"Card" => &MyAppWeb.Components.card/1})<Card :title="post.title" />.vue files
use PhoenixVapor, file: "Dashboard.vue" makes a .vue file the LiveView's template. The path is relative to the module's file, and can be any expression known at compile time, such as Path.join(@templates, "Dashboard.vue"). Without ref() in <script setup>, the file is server-only: render/1 comes from the template, and the rest of the LiveView is your Elixir.
<!-- lib/my_app_web/live/Dashboard.vue -->
<template>
<h1>{{ title }}</h1>
<p v-for="stat in stats">{{ stat.label }}: {{ stat.value }}</p>
</template>defmodule MyAppWeb.DashboardLive do
use MyAppWeb, :live_view
use PhoenixVapor, file: "Dashboard.vue"
def mount(_params, _session, socket) do
{:ok, assign(socket, title: "Dashboard", stats: Stats.all())}
end
endA <script lang="elixir"> block is compiled into the module, so a component can live in a single file; see Hybrid mode.
Script functions on the server
A function <script setup> defines is JavaScript, which the server doesn't run. When the template calls one, define the same function in <script lang="elixir">, named in snake_case with the same arity, and the server calls it instead:
<script setup lang="ts">
function roleTone(role: string) {
return role === "owner" ? "warning" : role === "admin" ? "success" : "neutral"
}
</script>
<script lang="elixir">
def role_tone("owner"), do: "warning"
def role_tone("admin"), do: "success"
def role_tone(_role), do: "neutral"
</script>
<template>
<Badge v-for="member in members" :tone="roleTone(member.role)">{{ member.role }}</Badge>
</template>The server renders roleTone(member.role) with role_tone/1, and in hybrid mode the browser runs roleTone once it mounts, so the two should agree. Without an Elixir function, the call is reported, as below, with the name to define.
Function components from .vue files
PhoenixVapor.Vue.component/2 defines a function component from a .vue file's template. A <style scoped> block is compiled too: the root element gets the scope attribute, and the CSS is available from a generated function.
defmodule MyAppWeb.Components do
use Phoenix.Component
require PhoenixVapor.Vue
PhoenixVapor.Vue.component(:card, "vue/Card.vue")
endMyAppWeb.Components.__vue_css_card__()
# ".card[data-v-7a7a37b1] { ... }"Problems at compile time
Templates compile when the module that uses them does, and problems point into the .vue file:
** (CompileError) lib/my_app_web/live/Settings.vue:12: can't parse the expression `user.`
warning: <TabsRoot> is imported from "reka-ui", which the server can't render; the browser renders it when it mounts
│
24 │ <TabsRoot v-model="tab">
│ ~~~~~~~~~~~~~~~~~~~~~~~~
│
└─ lib/my_app_web/live/Settings.vue:24: (file)What the server can't render is a compile error, except in hybrid mode, where the browser renders it once it mounts, so it's a warning: a component from a package that can't render on the server, a call to a function <script setup> defines or imports, unless <script lang="elixir"> defines it for the server, and a macro call that depends on a value known only when rendering.
Rendering at runtime
PhoenixVapor.render/2 compiles and renders a template string at runtime, for templates that aren't known at compile time. It parses the template on every call, so prefer the sigil or a .vue file when you can.
PhoenixVapor.render("<p>{{ msg }}</p>", %{msg: "Hello"})