我正在尝试为我的hugo网站编写一个简短代码,以获取页面的title参数。
我有这样的目录结构:
content
├── workshops
│ ├── foo
│ │ └── _index.md
│ ├── bar.md
这很完美:
{{ with .Site.GetPage "home" "workshops/foo"}}
{{ .Params.Title }}
{{ end }}
而且这个总是空白(即使减价中有标题)。
{{ with .Site.GetPage "home" "workshops/bar"}}
{{ .Params.Title }}
{{ end }}
我的问题是:如何获得独立页面的标题?
我尝试了很多不同的组合方式,但是我来的不对。我已经尝试阅读这些文档,而在这一点上,它们令人费解。
答案 0 :(得分:0)
我有解决办法!我编写了一个Python3.7脚本来创建目录,并移动和重命名markdown文件,然后在整个目录中运行它。这解决了我的问题,但是有点麻烦...
import logging
import os
from pathlib import Path
def fixup(path):
location = Path(path)
assert location.is_dir(), location
for child in location.iterdir():
if child.is_dir():
fixup(child)
else:
fix_file(child)
def fix_file(file_path):
name = file_path.name
if not name.endswith(".md"):
# we only care about markdown files.
return
check_metadata(file_path)
if name.startswith("_index."):
# looks good
return
# make a directory with the same name as the file (without the extension)
suffix = ''.join(file_path.suffixes)
prefix = name[: -len(suffix)]
new_dir = file_path.parent / prefix
new_dir.mkdir()
new_path = new_dir / f"_index{suffix}"
file_path.rename(new_path)
def check_metadata(file_path):
""" given the path to a markdown file, make sure that the frontmatter includes
the required metadata
"""
# TODO
# required = ['title']
# allowed = ['pre', 'weight', 'ready']
if __name__ == '__main__':
fixup('content')
答案 1 :(得分:0)
两个区别:
{{ with site.GetPage "workshops/bar" }}
{{ .Title }}
{{ end }}