【问题标题】:How to set separate (or multiple) template paths for pages and partials in Mustache with Sinatra?如何使用 Sinatra 在 Mustache 中为页面和部分设置单独(或多个)模板路径?
【发布时间】:2013-12-03 18:21:36
【问题描述】:

我设置了模板路径:

class Mustache
  self.template_path = 'templates/pages'
end

这很好用。

但是,我想将部分存储在一个单独的文件夹中:templates/partials

小胡子模板如下所示:

Hello {{ name }}
Your info: {{> info }

info.mustache 是一个部分。

信息部分没有类,所以我不能设置self.template_path

那么,如何设置一个partial的模板路径,或者有多个模板路径呢?

self.template_path = 'templates'
self.template_path = 'templates/'
self.template_path = 'templates/*'
self.template_path = 'templates/**'
self.template_path = [ 'templates/pages', 'templates/partials' ]

没用。

【问题讨论】:

    标签: ruby sinatra mustache


    【解决方案1】:

    你不能。如果您查看此文件,您会发现只能设置模板路径。部分最终在渲染过程中使用相同的路径。

    https://github.com/defunkt/mustache/blob/master/lib/mustache/settings.rb

    但是,如果您将部分命名为 {{>partials/info}},它将在模板路径 (templates/partials/info.mustache) 中查找 info.mustache。

    您也可以像这样覆盖 mustache.rb 中的部分方法:

    class Fubar < Mustache
      def partial(name)
        partial_name = "partials/#{name}"
        super(partial_name)
      end
    end
    

    最终,partial 与任何其他模板没有什么不同。您想要的路径仅用于组织目的。

    另一位用户提出了一个很好的观点,即现在保留了小胡子。有一个时间点,最初的贡献者已经停止支持它。我很高兴有人接手了这个项目。

    【讨论】:

    • >>"Mustache for Ruby 不再维护" .....听起来对我来说非常活跃的存储库。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多