如何在模板文件中包含元数据?

发布于 2024-07-13 16:34:36 字数 768 浏览 7 评论 0原文

我有一个通过 erb 过滤模板文件的系统。 使用约定优于配置,输出文件在镜像输入文件的文件层次结构中创建。 许多文件具有相同的名称,我可以使用目录来区分它们。

该计划一直有效,直到我需要将附加信息与每个文件关联起来。 因此,我在每个目录中使用元数据创建了一个 YAML 文件。 现在我有约定配置。 恶心。

然后我了解了 Webby,以及它在每个模板文件顶部包含 YAML 元数据部分的方式。 它们看起来像这样:

---
title: Baxter the Dog
filter: textile
---
All the best little blogs use Webby.

如果我可以实现这样的标头,我就可以放弃我的层次结构和单独的 YAML 文件。 Webby 实现非常通用,实现了一个新的 MetaFile 类,将标题与“真实文本”分开,但它似乎比我需要的更复杂。

将元数据放在 erb 注释中似乎不错——它将被 erb 自动忽略,但我不确定如何访问注释数据。

<%#
title: Baxter the Dog
%>

有没有办法访问erb评论? 或者也许有不同的方法? 我的很多模板都会执行大量 erb 操作,但如果可以使其余部分变得更容易,我可以在单独的步骤中运行 erb。

I have a system that filters template files through erb. Using convention over configuration, the output files get created in a file hierarchy that mirrors the input files. Many of the files have the same names, and I was able to use the directories to differentiate them.

That plan worked until I needed to associate additional info with each file. So I created a YAML file in each directory with the metadata. Now I have both convention and configuration. Yuck.

Then I learned Webby, and the way it includes a YAML metadata section at the top of each template file. They look like this:

---
title: Baxter the Dog
filter: textile
---
All the best little blogs use Webby.

If I could implement a header like that, I could ditch my hierarchy and the separate YAML files. The Webby implementation is very generic, implementing a new MetaFile class that separates the header from the "real text", but it seems more complicated than I need.

Putting the metadata in an erb comment seems good -- it will be automatically ignored by erb, but I'm not sure how to access the comment data.

<%#
title: Baxter the Dog
%>

Is there a way to access the erb comments? Or maybe a different approach? A lot of my templates do a bunch of erb stuff, but I could run erb in a separate step if it makes the rest easier.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深者入戏 2024-07-20 16:34:36

如果您也将内容转储为 YAML 怎么样? 据推测,元数据只是转储到 YAML 的哈希值。 您可以将内容作为字符串附加到同一文件中的第二个 YAML 文档中:-

---
title: Baxter the Dog
filter: textile
--- |
Content line 1
Content line 2
Content line 3

转储就像这样简单:-

File.open('file.txt', 'w') do |output|
  YAML.dump(metadata, output)
  YAML.dump(content, output)
end

加载就像这样简单:-

File.open('file.txt') do |input|
  stream = YAML.load_stream(input)
  metadata, content = stream.documents
end

请注意,管道字符出现在 YAML 以便保留内容字符串中的换行符

How about if you dump your content as YAML too. Presumably the metadata is simply a Hash dumped to YAML. You could just append the content as string in a second YAML document in the same file :-

---
title: Baxter the Dog
filter: textile
--- |
Content line 1
Content line 2
Content line 3

Dumping is as simple as :-

File.open('file.txt', 'w') do |output|
  YAML.dump(metadata, output)
  YAML.dump(content, output)
end

Loading is as simple as :-

File.open('file.txt') do |input|
  stream = YAML.load_stream(input)
  metadata, content = stream.documents
end

Note that the pipe character appears in the YAML so that newlines in the content string are preserved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文