koajs / ejs Goto Github PK
View Code? Open in Web Editor NEWa koa view render middleware, support all feature of ejs
License: MIT License
a koa view render middleware, support all feature of ejs
License: MIT License
ejs
3.x drops support for Node 4 and 6, and it removes require.extensions
which previously caused an annoying warning in webpack bundles: mde/ejs#412.
Since this package already requires Node >= 7.6
, I don't think this upgrade should warrant a major version bump.
I use koa2 and koa-ejs to make a react ssr demo
koa code:
const markup = renderToString(
<Provider store={store()} >
<RouterContext { ...props } />
</Provider>
);
await ctx.render('index', { markup });
chrome devtool look like this:
<html>
<head>
<title>koa2 webpack2 react ssr</title>
</head>
<body>
<div id="root"><div data-reactroot="" data-reactid="1" data-react-checksum="-1292022691"><h4 data-reactid="2">This is React App Component</h4><a href="/a" data-reactid="3">A</a><a href="/b" data-reactid="4">B</a><a href="/c" data-reactid="5">C</a><div data-reactid="6">This is B</div></div></div>
<script src="./bundle.js"></script>
</body>
</html>
why?
Why did you remove the koa-ejs locals? How to render now global locals?
can't cache <% include('./head.html', {foo: 'bar'}) %>
because ejs.compile options not have params cache which developer seted:
const fn = ejs.compile(tpl, {
filename: viewPath,
_with: settings._with,
compileDebug: settings.debug && settings.compileDebug,
debug: settings.debug,
delimiter: settings.delimiter
});
Hello,
is this package/repository still maintained or is slowly getting to death?
Thank you
index.js, line 105
currently i have this on the server:
var path = require('path');
var koa = require('koa');
var render = require('koa-ejs');
var app = koa();
render(app,{
root: path.join(__dirname, "views"),
layout:false
});
app.use(function*(){
this.body = yield this.render("home");
});
app.listen(3000);
console.log("listen at localhost:3000");
.. and as "home" (views/home.html)
<html>
<head>
<title>koa ejs</title>
</head>
<body>
hello koa-ejs
</body>
</html>
when a run curl localhost:3000 -v
I get:
* Rebuilt URL to: localhost:3000/
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:3000
> Accept: */*
>
< HTTP/1.1 204 No Content
< X-Powered-By: koa
< Date: Wed, 15 Oct 2014 03:08:15 GMT
< Connection: keep-alive
<
* Connection #0 to host localhost left intact
Am I doing something wrong? I'm using this dependencies:
"dependencies": {
"koa": "^0.12.2",
"koa-ejs": "^1.1.2"
}
Hi,
Is there any sample code for ejs to be used with koa 2.x? thanks,
A.
RangeError: Maximum call stack size exceeded
at ejs.resolveInclude (/aaa/node_modules/koa-ejs/index.js:85:26)
at ejs.resolveInclude (/aaa/node_modules/koa-ejs/index.js:89:14)
at ejs.resolveInclude (/aaa/node_modules/koa-ejs/index.js:89:14)
at ejs.resolveInclude (/aaa/node_modules/koa-ejs/index.js:89:14)
at ejs.resolveInclude (/aaa/node_modules/koa-ejs/index.js:89:14)
at ejs.resolveInclude (/aaa/node_modules/koa-ejs/index.js:89:14)
at ejs.resolveInclude (/aaa/node_modules/koa-ejs/index.js:89:14)
at ejs.resolveInclude (/aaa/node_modules/koa-ejs/index.js:89:14)
at ejs.resolveInclude (/aaa/node_modules/koa-ejs/index.js:89:14)
at ejs.resolveInclude (/aaa/node_modules/koa-ejs/index.js:89:14)
然后后面的渲染就全挂了,一直报这个错
Line 84 in f70a090
Each call to the render
function will be reassigned parentResolveInclude
. The call stack gets longer and longer.
Add the log on line 86
const parentResolveInclude = ejs.resolveInclude;
ejs.resolveInclude = function(name, filename, isDir) {
console.log(name);
if (!path.extname(name)) {
name += settings.viewExt;
}
return parentResolveInclude(name, filename, isDir);
}
template/head
template/google-analysis
template/nav
template/copyright
template/head
template/head.ejs
template/google-analysis
template/google-analysis.ejs
template/nav
template/nav.ejs
template/copyright
template/copyright.ejs
template/head
template/head.ejs
template/head.ejs
template/google-analysis
template/google-analysis.ejs
template/google-analysis.ejs
template/nav
template/nav.ejs
template/nav.ejs
template/copyright
template/copyright.ejs
template/copyright.ejs
I think should move line 84 to line 32.
adding DOCTYPE html to the top of the layout template inserts HTML file in an HTML file
Hi,
thanks a lot for merging all the PR. When will be release new version of the package please?
Thanks
Jan Opravil
Right now koa-ejs uses only actual node's filesystem.
This becomes trouble when using it with webpack-dev-middleware.
My case: I have server that uses dev middleware to manage compilation for client-side javascript. It keeps all generated assets in memory. I am able to access it's memory filesystem via devMiddlewareInstance.fileSystem
which is memfs node's fs
api compliant fs.
But koa-ejs accepts only path to root.
So, solution is either to add fs
property to koa-ejs options which will accept any node's fs
api compliant type. Or second way is to provide root as callback of type (templateFilename: string) => Promise<string>
, like: root: (templateFilename) => myfs.readFilePromise(templateFilename)
.
What do you think?
line79 if (settings.cache && cache[viewPath]) return cache[viewPath](options.scope, options)
should be
return cache[viewPath].call()
Hi, I need such function like the co-ejs
the co-ejs only support koa1.x but I hope use in koa2
I can write code in ejs
<% user= await locals.user.get(1) %>
thank you
Can't get the layout feature to work. I have the following files.
File index.js
const path = require('path')
const views = require('koa-views')
const Koa = require('koa')
const app = module.exports = new Koa()
app.use(views(path.join(__dirname, '/views'), { extension: 'ejs' }))
const user = { name: { first: 'Tobi', last: 'Holowaychuk' }, species: 'ferret', age: 5 }
app.use(async function(ctx) {
await ctx.render('user', { user })
})
app.listen(3000)
File views/layout.ejs
<html>
<head>
<title>koa ejs</title>
</head>
<body>
<h3>koa ejs</h3>
<%- body %>
</body>
</html>
File views/user.ejs
<p><%= user.name.first %> is a <%= user.age %> year old <%= user.species %>.</p>
Copied from https://github.com/koajs/examples/blob/master/templates/app.js
Changing await ctx.render('user', { user })
to await ctx.render('user', { user, layout: 'layout.ejs' })
also does nothing.
The only result I get is <p>Tobi is a 5 year old ferret.</p>
(no sign of layout.ejs
).
Look here: https://github.com/tj/ejs
Custom delimiters can also be applied globally:
var ejs = require('ejs');
ejs.open = '{{';
ejs.close = '}}';
Which would make the following a valid template:
<h1>{{= title }}</h1>
I thought koa-ejs supported all features? If so, howcome this isn't supported?
Hey,
would it be possible to change default settings to:
var defaultSettings = {
cache: true,
layout: 'layout',
viewExt: 'html',
open: '<%',
close: '%>',
filters: {},
locals: {},
debug: false
};
Else this does not make much sense as we need to define viewExt
(to omit extension in middleware) and overwrite layout
as it else searches for layout.html.html
.
Would you accept a pr on this?
I have marked that ejs have already version great then 2.0. Koa-ejs in dependencies have ejs 1.0. Why? When will the update in plans?
not support <%- include('user/show', {user: user}); %> ? got error
see https://ejs.co/#docs ,
support <%- include('user/show', {user: user}); %>
I suppose I should be able to add global-type variables into locals and have access to them in each template, without passing them to render function. Can you confirm that, and if it is true fix that ASAP. Thanks)
example code:
ejs(app, {
root: path.join(__dirname, '/../view'),
layout: 'layout',
viewExt: 'html',
cache: true,
debug: false,
locals: {
test: "some global variable"
}
});
https://github.com/node-modules/koa-ejs/blob/8d30d1c2fed53710d7cb1b4d6b63a01c0b10716c/index.js#L94
What is the intended behavior here? executing a generatorFunction won't do anything until you call .next()
When mounting another app:
Example code:
var client1 = koa();
render(client1, {
root: path.join(__dirname, "view"),
viewExt: "html",
cache: false,
debug: true
});
client1.context.render = co.wrap(app.context.render);
client1.use(async (ctx, next) => {
await ctx.render(view, locals);
});
var app = koa();
app.use(mount("/demo", client1)
Error:
"ctx.render is not a function"
{"name":"ps-search","hostname":"Phani-Pearlshare.local","pid":67479,"level":50,"req":{"method":"GET","url":"/","headers":{"host":"localhost:8080","connection":"keep-alive","pragma":"no-cache","cache-control":"no-cache","upgrade-insecure-requests":"1","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) A
ppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36","accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8","accept-encoding":"gzip, deflate, sdch, br","accept-language":"en-GB,en-US;q=0.8,en;q=0.6","cookie":"DFTT_END_USER_AUTH_TOKEN=derhv5sa32t86pdhkgcrwyvbirss8wk36
wfegi; DFTT_REALTIME_SESSION_TOKEN=sT9byMt5s1I2bILfzjy2jzJC-1gKYmslRc1RFkAptVqwLELpPoC5xUthRbeHXWf6P96Sc49yy50wy0kNQnkKWw.8-1; DFTT_END_USER_ID=10628333; cookieconsent_dismissed=yes; _ga=GA1.1.1305685054.1470246540; driftt_aid=7dc5c5af-5b35-4874-8c16-a92041ab68a2; driftt_wmd=true; DFTT_END_USER_PREV_BOOTSTRAPPED=true
; connect.sid=s%3Ap4MlIDqncrnWj50o1SogZgp3.wSsF%2FSEbxRWp2PIeiXAUg7dsNUS55J5kNtPn47mwgmA"},"remoteAddress":"::1","remotePort":55063},"res":{},"err":{"message":"ctx.render is not a function","name":"TypeError","stack":"TypeError: ctx.render is not a function\n at _callee$ (/Users/Phani/new_work/ps-search/clients/de
mo/index.js:21:13)\n at tryCatch (/Users/Phani/new_work/ps-search/node_modules/regenerator-runtime/runtime.js:64:40)\n at Generator.invoke [as _invoke] (/Users/Phani/new_work/ps-search/node_modules/regenerator-runtime/runtime.js:355:22)\n at Generator.prototype.(anonymous function) [as next] (/Users/Phani/ne
w_work/ps-search/node_modules/regenerator-runtime/runtime.js:116:21)\n at step (/Users/Phani/new_work/ps-search/clients/demo/index.js:29:191)\n at /Users/Phani/new_work/ps-search/clients/demo/index.js:29:437\n at /Users/Phani/new_work/ps-search/clients/demo/index.js:29:99\n at /Users/Phani/new_work/ps-sea
rch/clients/demo/index.js:20:1\n at dispatch (/Users/Phani/new_work/ps-search/node_modules/koa-compose/index.js:44:32)\n at next (/Users/Phani/new_work/ps-search/node_modules/koa-compose/index.js:45:18)\n at send.then.done (/Users/Phani/new_work/ps-search/node_modules/koa-static/index.js:43:20)"},"duration":
30,"msg":" --> GET /demo 500 30ms","time":"2016-12-20T17:24:51.737Z","v":0}
koa-ejs/index.js line 96:
const fn = ejs.compile(tpl, {
filename: viewPath,
_with: settings._with,
compileDebug: settings.debug && settings.compileDebug,
debug: settings.debug,
delimiter: settings.delimiter,
cache: settings.cache,
async: settings.async
});
why set option 'compileDebug' as 'settings.debug && settings.compileDebug'
instead of 'settings.compileDebug'?
The latest on npm is 2.0.0
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.