【问题标题】:Denjucks render with Deno, undefined is not a functionDenjucks 用 Deno 渲染,undefined 不是函数
【发布时间】:2020-08-05 16:25:31
【问题描述】:

我正在使用 Deno v1.2.1

我一辈子都不能渲染模板,我可以很容易地写回复,但渲染是另一回事,我得到undefined is not a function

// Requiring modules 
import { Application, Router, send } from "https://deno.land/x/oak/mod.ts";
import {viewEngine, engineFactory, adapterFactory} from "https://deno.land/x/view_engine/mod.ts";
// Initiate app
const app = new Application();
const router = new Router();
// Setting up boilerplate for view-engine
const renderEngine = await engineFactory.getDenjuckEngine();
const oakAdapter = await adapterFactory.getOakAdapter();

// Adding middleware to require our router
app.use(
  viewEngine(oakAdapter, renderEngine, {
    viewRoot: `${Deno.cwd()}`
  })
);
// Creating Routes
router.get("/",(ctx:any, next)=>{
    ctx.render('index.html') // This gives undefined is not a function
    //ctx.response.body = "hello"; // This works, 
});

app.use(router.routes());
app.use(router.allowedMethods());

// Making app to listen to port
console.log('App is listening to port: 55555');
await app.listen({port:55555});

当我运行代码时,我得到以下信息,我不确定它是否相关

Warning Failed to get compiled source code of "https://deno.land/std@0.59.0/path/mod.ts".

我正在运行这样的代码:

deno run --allow-net=0.0.0.0:55555 --allow-read  main.ts

我的文件夹结构是:

_base.html
index.html <- inherits from _base.html
main.ts

有人指点吗?

2020 年 8 月 5 日 虽然以下内容曾经可以工作,但 Deno 更新导致它停止工作,非继承模板和从基本文件继承的模板会产生“未定义不是函数”

/_base.html /index.html

<!--_base.html-->
<html>
{% block content %}{% endblock content %}
</html>

<!--index.html-->
{% extends "_base.html" %}

临时解决方案 2020 年 7 月 25 日

在 cmets 的帮助下,我有了这段代码,它可以工作,但是如果没有将它们硬编码到路由中,静态文件夹将无法工作:

// Requiring modules 
import { Application, Router, send } from "https://deno.land/x/oak/mod.ts";
import {viewEngine, engineFactory, adapterFactory} from "https://deno.land/x/view_engine/mod.ts";
// Initiate app
const app = new Application();
const router = new Router();
// Setting up boilerplate for view-engine
const renderEngine = await engineFactory.getDenjuckEngine();
const oakAdapter = await adapterFactory.getOakAdapter();

app.use(viewEngine(oakAdapter, renderEngine));

router
    .get("/", async (context:any, next) => {
       context.render('index.html')

  }).get("/upload", async (context:any, next) => {
    context.cookies.set(
      "lastVisit",
      new Date().toISOString(),
      { httpOnly: true, sameSite: "strict" },
    );

    context.render('upload.html')

  }).get("/images/:file", async (context) => {
     const file = context.params.file;
     await send(context, context.request.url.pathname, {
          root: `${Deno.cwd()}`,
          index: file,
        });
  }).get("/css/:file", async (context) => {
     const file = context.params.file;
     await send(context, context.request.url.pathname, {
          root: `${Deno.cwd()}`,
          index: file,
        });
  }).get("/js/:file", async (context) => {
     const file = context.params.file;
     await send(context, context.request.url.pathname, {
          root: `${Deno.cwd()}`,
          index: file,
        });
  });

app.use(router.routes());
app.use(router.allowedMethods());

app.addEventListener('error', e => {
    console.log(e.error);
});

app.addEventListener('listen', ({hostname, port, secure}) => {
    console.log(`Listening on ${secure ? 'https://' : 'http://'}${hostname || 'localhost'}:${port}`)
});

// Making app to listen to port
await app.listen({port:55555});

【问题讨论】:

    标签: typescript deno


    【解决方案1】:

    示例 使用 Oak 在 ./index.html 处呈现 Denjucks 模板 假设你有一个这样的文件夹:

    /index.html
    /main.ts
    

    现在在 index.html 中

    <!--index.html-->
    <body>
      <h1>{{data.name}}</h1>
    </body>
    

    现在在带有特定版本导入的 main.ts 中,

    // main.ts
    import { Application } from "https://deno.land/x/oak@v6.0.0/mod.ts";
    import {
      viewEngine,
      engineFactory,
      adapterFactory,
    } from "https://deno.land/x/view_engine@v1.3.0/mod.ts";
    
    const denjuckEngine = engineFactory.getDenjuckEngine();
    const oakAdapter = adapterFactory.getOakAdapter();
    
    const app = new Application();
    
    app.use(viewEngine(oakAdapter, denjuckEngine));
    
    app.use(async (ctx, next) => {
      ctx.render("index.html", { data: { name: "John" } });
    });
    
    await app.listen({ port: 8000 });
    

    然后运行

    deno run --allow-net --allow-read ./main.ts

    打开任何浏览器,输入 http://localhost:8000 你应该会看到结果。

    【讨论】:

    • 单个文件有效,但如果我在 index.html 中使用extends,我会得到(unknown path) TypeError: path.resolve is not a function
    • 我认为更新可能也影响了您的代码,因为您的示例也不再运行
    猜你喜欢
    • 2021-07-13
    • 1970-01-01
    • 2015-10-27
    • 2019-02-20
    • 2020-10-12
    • 2015-10-29
    • 2015-06-05
    • 2019-10-16
    • 2015-06-12
    相关资源
    最近更新 更多