尝试使用数据设置分页,其中{{ title }} in <head><title>{{ title }}</title></head>是当前页面的标题,如在projects.json中定义的
假设可以这样做:
# main.njk
<head>
<title>{{ title }}</title>
</head># page.njk
---
layout: main.njk
pagination:
data: projects
size: 1
alias: project
permalink: "work/{{ project.title | slug }}/"
title: {{ project.title }}可能误解了一些基本面,但{{ title }}将其呈现为[object, object]。Permalink很好..。
发布于 2020-07-06 21:33:30
项目标题实际上可以用{{ project.title }}在主模板main.njk中访问,就像在projects.json中为该项目定义的任何其他项目数据一样。
对于任何其他页面(未定义为projects.json中的对象),可以使用条件语句:
<title>{{ project.title if project.title else title}}</title>
因此:
# main.njk
<head>
<title>{{ project.title if project.title else title}}</title>
</head># page.njk
---
layout: main.njk
pagination:
data: projects
size: 1
alias: project
permalink: "work/{{ project.title | slug }}/"
---# other_page.njk
---
layout: main.njk
title: Other Page
---# projects.json
[
{
"title": "Project 1"
},
{
"title": "Project 2"
}
]产出:
# work/project-1/
<head>
<title>Project 1</title>
</head># work/project-2/
<head>
<title>Project 2</title>
</head># other-page/
<head>
<title>Other Page</title>
</head>发布于 2021-02-23 17:22:40
现在可以使用eleventyComputed了
# main.njk
<head>
<title>{{ title }}</title>
</head># page.njk
---
layout: main.njk
pagination:
data: projects
size: 1
alias: project
permalink: "work/{{ project.title | slug }}/"
eleventyComputed:
title: "{{ project.title }}"发布于 2020-04-11 08:09:39
在njk文件中,通常不能在前端使用数据变量或模板语法。
permalink变量是一个例外。
见official Eleventy documentation about permalink
为了解决您的问题,您可以:
page.njk
title --一个javascript .11ty.js模板文件,可以替换page.njk或main.njk,也可以作为main.njk.的布局。
.11ty.js文件通常可以在前端使用数据变量。
例如,在前端有一个变量的.11ty.js文件:
let thing = "whatever";
class Sample {
data() {// data() is the .11ty.js equivalent of frontmatter
return {
myCustomFrontmatterVariable: thing,
};
}
render(data){
return data.content;
}
}
module.exports = Sample;https://stackoverflow.com/questions/61106975
复制相似问题