使用 spree-multi-domain 更改部分模板

Changing partial templates using spree-multi-domain

我有一个使用 spree-multi-domain 的 spree 商店,自述文件中有关于如何切换布局文件的说明

these layouts should be located in your site's theme extension in the app/views/spree/layouts/store#code/ directory. So, if you have a store with a code of "alpha" you should store its default layout in app/views/spree/layouts/alpha/spree_application.html.erb

这很好用,但现在我不确定如何更改其他模板,例如 home/index.html.erb。据我所知,这些文件被渲染到布局模板的 yield 部分,但我不确定如何使用它为不同的域设置不同的模板。

只需创建一个初始值设定项 multi_domain.rb 并拥有两个或更多具有唯一代码的商店

store1 = Spree::Store.find_by(code: "first_store")

store2 = Spree::Store.find_by(code: "second_store")

你的观点是这样分开的:

app/views/spree/first_store/home/index.html.erb

app/views/spree/second_store/home/index.html.erb

初始值设定项:

# config/multi_domain.rb
ActionView::PartialRenderer.class_eval do
  def find_template_with_multi_store(path, locals)
    prefixes = path.include?(?/) ? [] : @lookup_context.prefixes

    store_prefixes = prefixes
    store_path     = path

    if @view.respond_to?(:current_store) && @view.current_store && !@view.controller.is_a?(Spree::Admin::BaseController)
      store_prefixes = (store_prefixes.map { |i| i.gsub('spree/', "spree/#{@view.current_store.code}/") } + store_prefixes).uniq unless store_prefixes.nil?
      store_path     = store_path.gsub('spree/', "spree/#{@view.current_store.code}/") unless store_path.nil?
    end

    begin
      @lookup_context.find_template(store_path, store_prefixes, true, locals, @details)
    rescue ::ActionView::MissingTemplate
      @lookup_context.find_template(path, prefixes, true, locals, @details)
    end
  end

  alias_method_chain :find_template, :multi_store
end

ActionView::TemplateRenderer.class_eval do
  def find_template_with_multi_store(name, prefixes = [], partial = false, keys = [], options = {})
    if prefixes.nil?
      store_prefixes = nil
    elsif @view.respond_to?(:current_store) && @view.current_store && !@view.controller.is_a?(Spree::Admin::BaseController)
      spree = /^spree\//

      store_prefixes = []

      prefixes.each do |i|
        store_prefixes << i.gsub(spree, "spree/#{@view.current_store.code}/") if i.match(spree)
      end

      store_prefixes = (store_prefixes + prefixes).uniq
    else
      store_prefixes = prefixes
    end

    begin
      @lookup_context.find_template(name, store_prefixes, partial, keys, options)
    rescue ::ActionView::MissingTemplate
      @lookup_context.find_template(name, prefixes, partial, keys, options)
    end
  end
  alias_method_chain :find_template, :multi_store
end

此实施适用于具有 10 个域的真实商店。

我修改了 gonzalo moreno caballero 的答案,因此它适用于 rails 5.1

只需像

一样存储您的观点

app/views/spree/first_store/home/index.html.erb

app/views/spree/second_store/home/index.html.erb

并将其添加到 config/initializers/multi_domain.rb

module PartialRendererMultiStore
  def find_template(path, locals)
    prefixes = path.include?(?/) ? [] : @lookup_context.prefixes

    store_prefixes = prefixes
    store_path     = path

    if @view.respond_to?(:current_store) && @view.current_store && !@view.controller.is_a?(Spree::Admin::BaseController)
      store_prefixes = (store_prefixes.map { |i| i.gsub('spree/', "spree/#{@view.current_store.code}/") } + store_prefixes).uniq unless store_prefixes.nil?
      store_path     = store_path.gsub('spree/', "spree/#{@view.current_store.code}/") unless store_path.nil?
    end

    begin
      @lookup_context.find_template(store_path, store_prefixes, true, locals, @details)
    rescue ::ActionView::MissingTemplate
      @lookup_context.find_template(path, prefixes, true, locals, @details)
    end
  end
end


class ActionView::PartialRenderer
  prepend PartialRendererMultiStore
end

module TemplateRendererMultiStore
  def find_template(name, prefixes = [], partial = false, keys = [], options = {})
    if prefixes.nil?
      store_prefixes = nil
    elsif @view.respond_to?(:current_store) && @view.current_store && !@view.controller.is_a?(Spree::Admin::BaseController)
      spree = /^spree\//

      store_prefixes = []

      prefixes.each do |i|
        store_prefixes << i.gsub(spree, "spree/#{@view.current_store.code}/") if i.match(spree)
      end

      store_prefixes = (store_prefixes + prefixes).uniq
    else
      store_prefixes = prefixes
    end

    begin
      @lookup_context.find_template(name, store_prefixes, partial, keys, options)
    rescue ::ActionView::MissingTemplate
      @lookup_context.find_template(name, prefixes, partial, keys, options)
    end
  end
end

class ActionView::TemplateRenderer
  prepend TemplateRendererMultiStore
end

spree_application还需要进去views/spree/layouts/<store_code>/