0

布局

我想使用静态站点生成器hexo创建一个交替布局——概览页面上每隔一个帖子的文本应该在右侧。

我正在使用的部分需要将自定义变量传递textOrientation = "left"给部分函数。

<%site.tags.findOne({name: 'featured'}).posts.sort('date', -1).limit(5).each(function(post, index) {%>
    <%- partial('_partial/project-excerpt', {item: post}) %>
<% })%>

然后模板project_excerp.ejs需要根据传递的变量生成相应的类。

我的模板(project_excerp.ejs):

<a class="???" href="/project/<%= item.slug %>"><%= item.title %></a>

所以问题是:

  1. 如何post用我自己的变量扩展变量textOrientation
  2. 如何if then else在我的project_excerp.ejs模板中使用一个?
4

1 回答 1

0

好的,自己找到了解决办法。

index.js:

<%site.tags.findOne({name: 'featured'}).posts.sort('date', -1).limit(5).each(function(post, index) {%>
      <% if(index % 2 === 0) { %>
            <% post.textOrientation = "left"; %>
      <% } else { %>
            <% post.textOrientation = "right"; %>
      <% } %>
  <%- partial('_partial/project-excerpt', {item: post}) %>

项目摘录.ejs:

<% var projectInfoClass = "ta-right"; %>
<% if(item.textOrientation === "right") {%>
  <% projectInfoClass = "ta-left"; %>
<% } %>

<a class="<%= projectInfoClass %>" href="/project/<%= item.slug %>"><%= item.title %></a>

阅读EJS -docs 也很有帮助。

于 2016-09-05T17:34:36.447 回答