简体   繁体   English

ejs以后如何渲染模板

[英]ejs how to render template later

I wanted to create a one-page only based website. 我想创建一个仅一页的网站。 the problem is, how to I render a template later? 问题是,我以后如何渲染模板?

also, how to render a specific post? 另外,如何呈现特定帖子?

eg: 例如:

<!-- situation 1-->
<% include ../_header %>
<div width="40%">
  <% include ../menu %>  <!-- lots of post, once clicked will become situation 2 -->
</div>
<% include ../_footer %>

<!-- situation 2-->
<% include ../_header %>
<div width="40%">
  <% include ../menu %>
</div>
<div width="60%">
  <% include ../posts %>
</div>
<% include ../_footer %>

If you are using nodejs, and express, you will have two routes: 如果您使用的是nodejs和express,则将有两条路线:

router.get '/posts' (req, res) -> res.render('posts_template')

router.get '/posts/:post_id' (req, res) -> res.render('post_template')

As you can see, you have two different routes and templates for displaying posts and a single post. 如您所见,您有两个不同的路线和模板来显示帖子和一个帖子。

You will use layouts , have a post_template that defines only a post: 您将使用layouts ,具有一个post_template ,它仅定义一个帖子:

File: post_template.ejs

<% layout('layout') %>

<h1><%= title %></h1>
<p><%= body %></p>

And you will have a layout template for this post template: 您将拥有此帖子模板的布局模板:

File: layout_template.ejs

<h1>Posts list </h1>
<%= body %>

Now the post_template will be nested inside the layout_template , where the <%= body %> is. 现在, post_template嵌套layout_template ,其中<%= body %>所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM