brochure icon indicating copy to clipboard operation
brochure copied to clipboard

Allow template location to be elsewhere.

Open eric1234 opened this issue 13 years ago • 1 comments

Useful if we are embedding Brochure in another application has has it's own idea of directory structure. For example in a Rails application the following:

 mount Brochure.app(Rails.root,
     :templates_root => 'app/views/pages',
     :helpers => [ActionView::Helpers]
   ) => 'pages'

Placed in your routes.rb will allow a 'terms-and-use.html.erb' template to be created in app/views/pages which is then acessible by the path /pages/terms-and-use without having to mess around with controllers any.

eric1234 avatar Apr 11 '11 19:04 eric1234

NOTE: for anybody else wishing to embed Brochure into a Rails app. The above code works but doesn't really get all the helpers (like route helpers, application helpers, etc). Also some helpers cause an error since they are not being run in the context of a controller (like some of the asset helpers or the csrf_tag helper). So while the above works if you want a more fully functioning version the following might be useful:

  1. First use my fork (https://github.com/eric1234/brochure). In addition to the ability to set a template path it also allows you to set a common layout (i.e. your Rails layout file).

  2. Put the following code in an initializer:

    module PageServer
    
       module RailsHelper
         def config
           ActionController::Base.helpers.config.assets_dir = Rails.public_path
           ActionController::Base.helpers.config
         end
         def protect_against_forgery?; false end
         def flash; {} end
         def controller; end
       end
    
       def self.app
         helpers = [RailsHelper, Rails.application.routes.url_helpers] +
           ApplicationController.helpers.singleton_class.included_modules
         Brochure.app Rails.root, :templates_root => ['app/views/pages'],
           :layout => 'application', :helpers => helpers do |app|
           app.template_trail.paths.push 'app/views/layouts'
         end
       end
    
     end
    

The difference between this code and my previous simpler code is:

  • We use ApplicationController.helpers.singleton_class.included_modules instead of ActionView::Helpers as this includes more helpers (including the ApplicationHelper).
  • We use Rails.application.routes.url_helpers to pull in the route helpers.
  • We define a helper (that I call PageServer::RailsHelper). This helper provides some missing bits that the other helpers rely on like helper configuration, flash params, etc with some dummy responses.
  • We tell it to use the Rails application layout.
  • We tell it to consider the "layouts" directory a template path also.

The above code is a little ugly still but we are moving towards my eventual goal of being able to easily integrate Rails and Brochure.

3 . The final step is to add the following to your routes:

    mount PageServer.app => 'pages'

eric1234 avatar Apr 26 '11 19:04 eric1234