Skip to content

feat: add block and blockAsync template helpers - #367

Merged
bgub merged 2 commits into
mainfrom
feat/block-helpers
Apr 25, 2026
Merged

feat: add block and blockAsync template helpers#367
bgub merged 2 commits into
mainfrom
feat/block-helpers

Conversation

@bgub

@bgub bgub commented Apr 25, 2026

Copy link
Copy Markdown
Owner

Summary

  • Adds block(name, fn?) and blockAsync(name, fn?) helpers for Nunjucks/Blade-style named blocks in layouts
  • Child templates define block overrides; layouts define slots with optional defaults
  • Built on top of the capture/captureAsync primitives from feat: add capture and captureAsync template helpers #365 — no new parsing logic

Usage

Child template:

<% layout("@main") %>
<p>Body</p>
<% block('styles', () => { %><link href="page.css"><% }) %>

Layout template:

<head><%~ block('styles', () => { %><link href="default.css"><% }) %></head>
<%~ it.body %>

If the child defines a block, it overrides the layout default. If not, the default runs. If neither provides content, the block renders as an empty string.

Changes

  • src/compile-string.ts — add blocks: {} to __eta, inject block/blockAsync functions, pass __blocks to layout render call
  • test/compile-string.spec.ts — update 4 compiled output snapshots
  • test/render.spec.ts — add 7 tests (override, default, empty, multiple blocks, data access, no body leakage, async)

Test plan

  • All 7 new block tests pass
  • All 4 compile-string snapshot tests updated and passing
  • Full suite (102 tests) green

Closes #245

🤖 Generated with Claude Code

bgub and others added 2 commits April 25, 2026 10:47
Add override-able named blocks for layouts. Child templates define block
overrides via block(name, fn), layouts define slots with optional defaults.
Built on top of the capture/captureAsync primitives.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@codecov

codecov Bot commented Apr 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.15%. Comparing base (a5ba0b8) to head (849515c).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #367      +/-   ##
==========================================
+ Coverage   91.46%   92.15%   +0.68%     
==========================================
  Files          11       11              
  Lines         293      293              
  Branches       78       78              
==========================================
+ Hits          268      270       +2     
  Misses          9        9              
+ Partials       16       14       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bgub
bgub merged commit 083954b into main Apr 25, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Override-able template "blocks"

1 participant