Good Day!
Basically I wanted to ask, whether it's possible to render just a part of a hexo template with my existing ExpressJS server and EJS.
Say I have a route /blog
app.get("/blog", (req, res) => {
res.render("blog", {
"route": req.path,
});
});
and an EJS template blog.ejs.
Now I want to include a part of hexo in this blog.ejs file like so:
<%- include("global/header"); -%>
<div class="main-content">
<% /* I want to include the blog part of hexo without header/footer here */ %>
</div>
<%- include("global/footer"); -%>
Is that possible?
Hexo Rendering API is what you need.
Before calling res.render, use the Hexo rendering API to render the blog and then pass the result to locals parameter.
To exclude header or footer, customizing templates in your Hexo theme can achieve this.
Check out this page for more info about Hexo templates.
I think there is another way worth trying to render the Hexo blog under route /blog.
By Hexo router API, you can make use of the goodness of NodeJS Stream API like the following example code.
Since hexo.route.get() returns a Readable, you can pipe it to the express res object, which is a Writable.
pipe method will end the response for you by default.
app.get("/blog", (req, res) => {
hexo.route.get(req.path).pipe(res);
});
After delegating the rendering job to Hexo, include your blog.ejs as a part of customized templates in your Hexo theme.
It just another information for you. Not testing it myself, but it seems feasible and pretty enough!
Thank you very much! :)
Most helpful comment
Hexo Rendering API is what you need.
Before calling
res.render, use the Hexo rendering API to render the blog and then pass the result tolocalsparameter.To exclude header or footer, customizing templates in your Hexo theme can achieve this.
Check out this page for more info about Hexo templates.
I think there is another way worth trying to render the Hexo blog under route
/blog.By Hexo router API, you can make use of the goodness of NodeJS Stream API like the following example code.
Since
hexo.route.get()returns a Readable, you can pipe it to the expressresobject, which is a Writable.pipemethod will end the response for you by default.After delegating the rendering job to Hexo, include your
blog.ejsas a part of customized templates in your Hexo theme.It just another information for you. Not testing it myself, but it seems feasible and pretty enough!