Skip to content
Max Ivak edited this page Dec 16, 2018 · 6 revisions

Controllers

SiteBaseController

  • SiteBaseController - base controller for pages processed by CMS

  • add your custom code to SiteBaseController controller

# app/controllers/site_base_controller.rb

class SiteBaseController  < BaseController


  ### custom for application
  before_filter :my_before_page_render


  def my_before_page_render
    # so smth here

  end


end

this code will work for all pages - both text pages and pages with your controller.

Your controller

To use OptimaCMS features in controller and views add this:

class MysiteController < SiteBaseController

  include ::Optimacms::Mycontroller
  ::ActionController::Base.helper Optimacms::Engine.helpers

  def myaction
...
  end

..

end

Callbacks in controller

after_init_pagedata

  • called after page data has been initialized
  • used to set additional vars to pagedata.url_vars. For example, you can set @lang.
class ApplicationController < ActionController::Base

  def after_init_pagedata

    ...
    @pagedata.url_vars[:lang] = '' # evaluate lang here
  end
end



Example.

  • set lang from custom var :locale in URL

class ApplicationController < ActionController::Base

  def after_init_pagedata
    # for url /en-gb/devices/123/
    @locale = params[:locale] # 'en-gb'

    @lang = nil
    if @locale=~ /([a-z]+)([_-])([a-z]+)/
      @lang, @region_code = $1, $3
    end

    @pagedata.url_vars[:lang] = lang
  end
end


before_page_render

  • use special method in your controller before_page_render
  • it will be invoked AFTER CMS processed the page and BEFORE rendering page
class ApplicationController < ActionController::Base

...
  ### callbacks for CMS
  def before_page_render
    set_locale

  end


  def set_locale
    @lang = params[:lang] || 'en'
    params[:lang] ||= @lang

    # set locale
    I18n.locale = @lang.to_sym

  end


Known issues

Error. A copy of Optimacms::Mycontroller has been removed from the module tree but is still active!

  • add these lines to controller
  include ::Optimacms::Mycontroller
  ::ActionController::Base.helper Optimacms::Engine.helpers

Clone this wiki locally