You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Deno中修改Eta页面使用布局文件时遇AssertionError求助

解决Eta模板布局提取后的AssertionError问题

我需要将index.eta、authors.eta、books.eta中的通用内容(含菜单)提取到views/layouts/layout.eta,再让这三个页面使用该布局。但提交代码后触发了AssertionError,相关文件及错误信息如下:

相关文件代码

app.js

import { Application, Router } from "https://deno.land/x/oak@v11.1.0/mod.ts";
import { renderMiddleware } from "./middlewares/renderMiddleware.js";

const app = new Application();
const router = new Router();

app.use(renderMiddleware);

router.get("/", ({ render }) => render("index.eta"));
router.get("/books", ({ render }) => render("books.eta"));
router.get("/authors", ({ render }) => render("authors.eta"));

app.use(router.routes());

if (!Deno.env.get("TEST_ENVIRONMENT")) {
  app.listen({ port: 7777 });
}

export default app;

middlewares/renderMiddleware.js

import { configure, renderFile } from "https://deno.land/x/eta@v2.0.0/mod.ts";

const renderMiddleware = async (context, next) => {
  configure({
    views: `${Deno.cwd()}/views/`,
  });

  context.render = async (file, data) => {
    context.response.headers.set("Content-Type", "text/html; charset=utf-8");
    context.response.body = await renderFile(file, data);
  };

  await next();
};

export { renderMiddleware };

原views/authors.eta

<% layout('./layouts/layout.eta') %>
<h1><a href="/">Books and authors!</a></h1>
<ul>
  <li><a href="/books">Check out the books!</a></li>
  <li><a href="/authors">Check out the authors!</a></li>
</ul>
<h2>Authors</h2>
<ul>
  <li>Mike Hebot</li>
  <li>Kent Chanbot</li>
  <li>Kaquhy K. Platt</li>
</ul>

原views/books.eta

<% layout('./layouts/layout.eta') %>
<h1><a href="/">Books and authors!</a></h1>
<ul>
  <li><a href="/books">Check out the books!</a></li>
  <li><a href="/authors">Check out the authors!</a></li>
</ul>
<h2>Books</h2>
<ul>
  <li>The Secret of the Silent Porter</li>
  <li>Liberation of Nebula</li>
  <li>2246: Retribution</li>
  <li>2132: Outbreak</li>
  <li>The Ruins of Zion</li>
</ul>

原views/index.eta

<% layout('./layouts/layout.eta') %>
<h1><a href="/">Books and authors!</a></h1>
<ul>
  <li><a href="/books">Check out the books!</a></li>
  <li><a href="/authors">Check out the authors!</a></li>
</ul>

原views/layouts/layout.eta

<!doctype html>
<html lang="en">
  <head>
    <title>Title</title>
    <meta charset="utf-8">
  </head>
  <body>
    <%~ it.body %>
  </body>
</html>

错误信息

error: AssertionError
    throw new AssertionError(msg);          

问题原因

  1. 布局路径错误:Eta中layout()的路径是相对当前模板的。原页面在views/目录,布局在views/layouts/,使用./layouts/layout.eta会被解析为views/layouts/layouts/layout.eta,导致找不到模板文件触发断言错误。
  2. 通用内容未提取:所有页面重复保留了标题和菜单,没有将这些通用内容移到布局文件中,不符合任务要求。

修复步骤

1. 修改layout.eta,加入通用内容

把所有页面共有的标题、菜单移到布局中,仅保留it.body作为页面独有内容的占位:

<!doctype html>
<html lang="en">
  <head>
    <title>Books and Authors</title>
    <meta charset="utf-8">
  </head>
  <body>
    <h1><a href="/">Books and authors!</a></h1>
    <ul>
      <li><a href="/books">Check out the books!</a></li>
      <li><a href="/authors">Check out the authors!</a></li>
    </ul>
    <%~ it.body %>
  </body>
</html>

2. 修改页面模板,移除通用内容并修正布局路径

  • index.eta(无独有内容,仅保留布局声明):
<% layout('layouts/layout.eta') %>
  • authors.eta(仅保留作者列表):
<% layout('layouts/layout.eta') %>
<h2>Authors</h2>
<ul>
  <li>Mike Hebot</li>
  <li>Kent Chanbot</li>
  <li>Kaquhy K. Platt</li>
</ul>
  • books.eta(仅保留书籍列表):
<% layout('layouts/layout.eta') %>
<h2>Books</h2>
<ul>
  <li>The Secret of the Silent Porter</li>
  <li>Liberation of Nebula</li>
  <li>2246: Retribution</li>
  <li>2132: Outbreak</li>
  <li>The Ruins of Zion</li>
</ul>

验证修复

运行应用后,三个页面会统一显示标题和菜单,同时各自展示独有内容,AssertionError会因为布局路径正确、模板可被找到而消失。

内容的提问来源于stack exchange,提问作者bobbyjackson555

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 10:17:55