LiveView-style ergonomics: :if/:for, @assigns sugar, Socket helpers#52
Merged
Conversation
Imports authoring ergonomics from LiveView into the ~MOB sigil and
Mob.Socket, so screens read the way Phoenix developers expect.
Sigil (lib/mob/sigil.ex):
- `@foo` inside any `{...}` expression rewrites to `assigns.foo`
(attribute values, {expr} children, and control expressions alike).
- `:if={expr}` wraps an element so it renders only when truthy; a
falsy `:if` yields nil, which is dropped from the parent's children.
- `:for={x <- list}` repeats an element per item and splices into the
parent. Combined with `:if`, the `:if` becomes a comprehension filter
(LiveView semantics).
- wrap_child/1 now normalizes nil (dropped) alongside list/map, so
control-wrapped children flatten cleanly.
Socket (lib/mob/socket.ex):
- update/3 (apply a fun to an existing assign, KeyError if absent)
- assign_new/3 (lazily set an assign only when absent)
Both mirror Phoenix.LiveView.
Tests cover the generated AST and runtime behavior for self-closing and
container elements, the :if/:for filter combo, @ expansion (including
nested @user.name), and compile-time errors for bad control attrs.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The code carried good moduledocs but the agent-facing guides (read_guide / get_oriented point here, not hexdocs) didn't mention the new authoring idioms, making them undiscoverable through the normal learning path. - guides/components.md: new "## Control flow" section documenting @assigns shorthand, :if, :for, and the :for+:if comprehension-filter combo, plus the compile-time error cases. - guides/liveview.md: disambiguation note — that guide is WebView LiveView mode; point readers after native sigil "feel" to Components → Control flow. - AGENTS.md: one-line authoring convention pointing at the new section. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Imports a few authoring ergonomics from LiveView so Mob screens read the way Phoenix developers expect. This is the "feel" layer only — no DOM-specific bits (phx-*, JS commands, morphdom, streams are out of scope).
What's in
~MOBsigil (lib/mob/sigil.ex)@fooshorthand — inside any{...}expression,@foorewrites toassigns.foo(attribute values,{expr}children, and control expressions alike), matching Phoenix HEEx. Nested forms like@user.namework too.:if={expr}— renders the element only when the expression is truthy. A falsy:ifyieldsnil, which is dropped from the parent's children.:for={x <- list}— repeats the element per item and splices into the parent.:for+:ifcombined —:ifbecomes a comprehension filter (LiveView semantics): an element is produced only for items where the condition holds.Implementation notes:
:is now accepted in attribute names at the parser level;split_control_attrs/1pulls:if/:forout before props are built and raises on any other:-prefixed attr.:if/:forrequire a{expr}value (a string value raises a clearCompileError).wrap_child/1now normalizesnil(dropped) alongside list/map, so control-wrapped children flatten cleanly through the existingList.flatten.wrap_control/3is on attr presence, not the parsed expr — otherwise:if={false}would short-circuit and skip wrapping.Mob.Socket(lib/mob/socket.ex)update/3— apply a function to an existing assign (KeyErrorif absent)assign_new/3— lazily set an assign only when absentBoth mirror
Phoenix.LiveView.Tests
Per the repo rule that sigil compile-time transforms are tested at the AST level, not just runtime: new coverage exercises self-closing and container elements, the
:if/:forfilter combo,@expansion (including nested@user.name), empty-list:for, and compile-time errors for unknown/ string-valued control attrs.mix format,mix credo --strict,mix erlfmt --check src/: cleanNot included
No version bump — release timing is the maintainer's call.
🤖 Generated with Claude Code