Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handlebars配置layout刷新路径直接读取文件内容问题 #44

Open
luchanan opened this issue Jun 2, 2017 · 4 comments
Open

Comments

@luchanan
Copy link

luchanan commented Jun 2, 2017

从这里下载了koa-generator, 想在index.hbs继承layout.hbs,做了下面的修改
app.js

app.use(views(__dirname + '/views', {
  extension: 'hbs',
  map: { hbs: 'handlebars' },
  options: {
      partials: {
          layout: './layout' 
      }
  }
}))

就改了这个,第一次没有报错,第二次貌似把layout.hbs的内容读出来作为路径了

  Error: ENOENT: no such file or directory, open 'F:\person_project\github\koa2_learn\views\<!DOCTYPE html>
  <html>
    <head>
      <title>{{title}}<\title>
      <link rel='stylesheet' href='\stylesheets\style.css' \>
    <\head>
    <body>
      {{{body}}}
    <\body>
  <\html>

初学不是太懂,指教一下,thanks

@i5ting
Copy link
Owner

i5ting commented Jun 4, 2017

@luchanan 开一个repo写个demo,然后给我

@luchanan
Copy link
Author

luchanan commented Jun 4, 2017

这个问题demo: https://github.com/luchanan/koa2_hbs.git
其实我看了express-generator,他们用的hbs,已经对handlebars进行了高级封装吧,有继承layout的功能。然后我看了你这里只有handlebars这个东西,我在想是不是基础版,layout需要自己实现(如上面我改的)。后来我用了koa-hbs(支持koa2)来弄就可以了,解决了刷新报错的问题。因为我看koa-views参数比较少,koa-hbs的参数好像不能用在koa-views上,所以我进行了下面的修改

// app.js
const hbs = require('koa-hbs')
...
// app.use(views(__dirname + '/views', {
//   extension: 'hbs',
//   map: { hbs: 'handlebars' },
//     defaultLayout: 'layout'
// }))

app.use(hbs.middleware({
    viewPath: __dirname + '/views',
    layoutsPath: __dirname + '/views',
}));
// index.hbs
{{!< layout }}
<h1>{{title}}</h1>
<p>Welcome to {{title}}</p>

请求高见

@jane-
Copy link

jane- commented Jul 27, 2017

我安装啦 npm install -g koa-generator 之后 运行 koa koa2 && cd koa2 然后报错 koa: command not found ,我的是mac系统 node v8.2.1版本

@i5ting
Copy link
Owner

i5ting commented Jul 27, 2017

@jane- 很奇怪啊,有更多信息么?

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

No branches or pull requests

3 participants