2017-09-15 122 views
0

我使用spree-multi-domain有一個大禮包存儲和自述對如何切換佈局文件更改使用大禮包,多域

這些佈局應設在你的網站的主題擴展的應用的說明部分模板/ views/spree/layouts/store#code/directory。所以,如果你有一個代碼爲「alpha」的商店,你應該將它的默認佈局存儲在app/views/spree/layouts/alpha/spree_application.html.erb

這工作正常,但現在我不是確定我可以如何更改其他模板,如home/index.html.erb。據我瞭解,這些文件呈現在佈局模板的yield部分,但我不確定如何使用此模板爲不同的域設置不同的模板。

回答

0

我修改從伊莫雷諾卡瓦列羅答案因此它可以在軌道上5.1

只儲存你的觀點一樣

應用程序/視圖/大禮包/ first_store /家庭/ index.html.erb

應用程序/視圖/大禮包/ second_store /家庭/ index.html.erb

並添加此到config /初始化/ 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 && [email protected]_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 && [email protected]_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>/

1

只是唯一代碼創建一個初始化multi_domain.rb與此和具有兩個或多個商店

商店1 =施普雷:: Store.find_by(代碼: 「first_store」)

商店2 =施普雷:: Store.find_by(代碼: 「second_store」)

和你分開的觀點是這樣的:

應用程序/視圖/大禮包/ first_store /家庭/ index.html.erb

應用程序/視圖/大禮包/ second_store /家庭/ 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 && [email protected]_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 && [email protected]_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個域的真實商店中運行。

+0

去這類作品,但'alias_method_chain'在軌5.1現在已經過時。 – Qwertie