Closed ravener closed 5 years ago
Hey~!
There's no built-in rendering/view engine support for Polka. However, you could add it in yourself. Something like this, perhaps:
const { join } = require('path');
const { renderFile } = require('ejs')
const send = require('@polka/send-type');
const views = join(__dirname, 'views');
app = polka();
app.use((req, res, next) => {
// Attach your "render" method
res.render = (file, data) => {
// Compile the file w/ data
renderFile(join(views, file), data, (err, html) => {
// Handle Error, else return output
if (err) return send(res, 500, err.message || err);
res.setHeader('content-type', 'text/html');
send(res, 200, html);
});
};
next();
});
I haven't tested it, but that should do the trick 😄
Lemme know if that works & if you have any other questions!
Thanks, it mostly works except i had to switch the send()
/res.setHeader()
call to a res.writeHead()
and res.end()
because seems like with send-type module it messes up the content-type header so the output is just non rendered html as text, i would say in the module check if content-type has already been set
Ah you're right, forgot about that. You can do this instead (which I originally had, but don't like the look of lol)
send(res, 200, html, {
'content-type': 'text/html'
})
hello lukeed, it is not working either.error comes like join(__dirname, 'views'); is not a function.I am trying to render a view in a get request , but feels like polka has no such feature of rendering view.
Hi @RojinaBajra, did you try the above snippet? The join
function is required from the built-in path
module. It's included at the top of the snippet (https://github.com/lukeed/polka/issues/73#issuecomment-428315454)
something wrong here??let me know.I am trying to render a form in get url /form.
import polka from 'polka'; import send from '@polka/send-type'; import {join} from 'path'; import {renferFile} from 'ejs'; const views = join(__dirname, 'views'); app.get('/form', function(req, res) {
res.render = (file, data) => { renderFile(join(views, file), data, (err, html) => { if (err) return send(res, 500, err.message || err); res.setHeader('content-type', 'text/html'); send(res, 200, html, { 'content-type': 'text/html' }) }); }; });
You have a typo in your import :)
// import {renferFile} from 'ejs';
import {renderFile} from 'ejs';
dont work. What could be other reasons?
lukeed, where is views folder? i have a file called form.ejs inside views folder . Thus , is this enough? const views = join(__dirname, 'views');
@RojinaBajra The views
directory is where you put it. You just need to remember that __dirname
is the current directory of the current file being run.
Since you're bundling (I can tell via the import
statements), your directory pathing may be different than what you've written for development. For example:
|- src/
|- index.js <-- Assume `join(__dirname, 'views')` is here
|- bundle.js <-- Assume this is output file
|- views/
In this example, the join(__dirname, 'views')
will work because bundle.js
is sibling to the views
directory. However, if you had written join(__dirname, '../views')
then it would not work because even though it looks correct from src/index.js
viewpoint, it's wrong because the server is run from the bundle.js
location.
I've helped you as much I can at this point. This is not a Polka or package issue & instead is a generic Node.js/development problem, specific to your build setup.
Good luck~!
i had to write return.send() in order to render the page . How can i now send the request paramters of a form file after post request submit.
In express , i do sth like to render a page with values. app.get("/", (req, res) => { res.render("index.ejs", { value: "someValue" ); }); What would be here in polka in below code, lukeed? I have tried several ways but didnt work either. app.get('/form', function(req, res) { renderFile(views, (err, html) => { if (err) return send(res, 500,err.message || err); return send(res, 200, html,{'Content-Type':'text/html'}); });
});
@RojinaBajra If you find using ejs
is not straightforward for you with polka
, perhaps this tiny little package might be helpful https://github.com/motss/lit-ntml. Check it out for simple and intuitive templating with JavaScript.
Thanks,solved the problem.Learning polka was a good one
On Tue, 23 Apr 2019, 07:20 Rong Sen Ng, notifications@github.com wrote:
@RojinaBajra https://github.com/RojinaBajra If you find using ejs is not straightforward for you with polka, perhaps this tiny little package might be helpful https://github.com/motss/lit-ntml. Check it out for simple and intuitive templating with JavaScript.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/lukeed/polka/issues/73#issuecomment-485609121, or mute the thread https://github.com/notifications/unsubscribe-auth/ADGHYHG5NGXGCLQCT2XG553PRZRULANCNFSM4F2PVPOA .
Hello I have solved the problem already.
app.get('/form', function(req, res) { // const data = {qs:req.params}; renderFile(views, (err, html) => { if (err) return send(res, 500,err.message || err); return send(res, 200, html,{'Content-Type':'text/html'}); }); // }; });
app.post('/form-submission', function(req,res){ const successMsg = "Form Submitted Successfully"; req.cdata = { success: 1, reqData:req.body, message: 'Form Submitted Successfully' }; return send(res, 200, req.cdata,successMsg, { 'Content-Type': 'application/x-www-form-urlencoded','Content-Length': 99}); });
This works for me .
On Tue, 23 Apr 2019, 07:20 Rong Sen Ng, notifications@github.com wrote:
@RojinaBajra https://github.com/RojinaBajra If you find using ejs is not straightforward for you with polka, perhaps this tiny little package might be helpful https://github.com/motss/lit-ntml. Check it out for simple and intuitive templating with JavaScript.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/lukeed/polka/issues/73#issuecomment-485609121, or mute the thread https://github.com/notifications/unsubscribe-auth/ADGHYHG5NGXGCLQCT2XG553PRZRULANCNFSM4F2PVPOA .
Ik this thread is quite old but there's a Polka plugin for EJS: https://github.com/ravener/polka-ejs/blob/master/index.js
just in case anyone needs it
Hi, I'm a little bit confused on how would you use the ejs (Embedded JavaScript) template engine in polka, this is how i do it in express