【问题标题】:Using custom Stencil tag in Kitura在 Kitura 中使用自定义 Stencil 标签
【发布时间】:2016-11-15 03:39:58
【问题描述】:

按照 Stencil 文档中的说明,我注册了一个简单的自定义标签:

let ns = Namespace()

ns.registerSimpleTag("contact") { context in
  return "<a href=\"/contact\">contact us</a>"
}

但我看不到如何将命名空间传递给render,因为response.render 只需要一个字典,而不是一个实际的Context 对象。

我错过了什么?我应该在某个地方使用而不是自己创建一个预先存在的命名空间吗?

【问题讨论】:

    标签: kitura


    【解决方案1】:

    您可以分叉https://github.com/IBM-Swift/Kitura-StencilTemplateEngine 并更改KituraStencilTemplateEngine.swift

    StencilTemplateEngine.render() 中定义您的 ns 命名空间并将返回代码行更改为:

    return try template.render(Context(dictionary: context), namespace: ns)
    

    然后在你的 fork 中添加一个标签,并使用你的 fork 作为 Package.swift 中的依赖项。

    【讨论】:

    • 感谢您确认我需要对 class StencilTemplateEngine 进行更改。我的做法与您建议的方式略有不同,现在有了一个可行的解决方案。
    • @MacAvon 这里的基本原理是,一旦您自定义 Stencil 模板引擎,例如使用自定义标签,您可以将其视为不同的模板引擎。因此,您可以创建自己的 Kitura-MyCustomizedStencilTemplateEngine 存储库,具有自己的版本控制等,并将其用于您的多个 Kitura 应用程序。 Kitura-MyCustomizedStencilTemplateEngine 将实现 Kitura TemplateEngine protocol 并为您的所有 Kitura 应用添加 Stencil 所需的任何自定义。
    猜你喜欢
    • 2012-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    • 1970-01-01
    • 2017-02-10
    • 1970-01-01
    相关资源
    最近更新 更多