Search code examples
ruby-on-railsrubyerbspree

Changing partial templates using spree-multi-domain


I have a spree store using spree-multi-domain and the readme has instructions on how to switch the layout file

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

This works fine but now I am not sure how I can change the other templates like home/index.html.erb. As I understand these files are rendered into the yield part of the layout template but I am unsure how I can set different templates for the different domains using this.


Solution

  • I modified the answer from gonzalo moreno caballero so it works on rails 5.1

    Just store your views like

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

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

    And add this to 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 still need to go in views/spree/layouts/<store_code>/