3

我想添加我自己的 Markdown 文件,一个名为profile.md. 但是,我不知道应该在我的 Vapor 项目中添加文件的位置,该项目是用 Xcode 开发的。

我尝试将其添加到Sources/或仅添加到根目录,但无法按Bundle.main.url(forResource:)功能搜索,例如:

guard let url = Bundle.main.url(forResource: "profile", withExtension: ".md") else { ... }

另外,我发现我已经在Copy Bundle Resources选项卡下添加了它。

在普通应用程序(适用于 iOS 和 macOS)中,上述功能可以正常工作。

如何添加我自己的文件以在 Xcode 开发的 Vapor 项目中使用?

4

1 回答 1

4

This is a very basic example of loading the contents of a file from a custom location using Foundation rather than Vapor's template view loading mechanism, and returning it as a text response.

drop.get { req in
  let templateString = try String(contentsOfFile: drop.workDir + "Resources/profile.md")
  return templateString
}

I'm not sure exactly what you mean by "parse, and embed it into another template file" but you'd put that bit in between the lines above.

于 2016-11-06T09:09:36.837 回答