Coder Social home page Coder Social logo

qichunren / themes_on_rails Goto Github PK

View Code? Open in Web Editor NEW

This project forked from chamnap/themes_on_rails

1.0 1.0 0.0 177 KB

Adds multi themes support to your Rails 3/4/5 application

Home Page: https://rubygems.org/gems/themes_on_rails

License: MIT License

Ruby 68.45% CSS 5.67% JavaScript 4.59% HTML 18.13% Liquid 1.20% Haml 1.97%

themes_on_rails's Introduction

ThemesOnRails Build Status Dependency Status Code Climate Coverage Status Gem Version

Installation

The simplest way to install is to use Bundler.

Add this gem to your Gemfile:

gem 'themes_on_rails'

If you want to use themes_on_rails with liquid template, add one more gem to your Gemfile:

gem 'liquid-rails'

Then, use Bundler to install the gem and its dependencies:

$ bundle install

Usage

A theme is composed of three things:

  1. Assets: images, javascripts, stylesheets
  2. Views: templates and layouts (erb, haml, or other template engines)
  3. Locales: locales files if any

Generator

To generate theme inside your app:

$ rails g themes_on_rails:theme theme_name
app/
  themes/
    [theme_name]/
      assets/
        images/
          [theme_name]/
        stylesheets/
          [theme_name]/
            all.css
        javascripts/
          [theme_name]/
            all.js
      views/
        layouts/
          [theme_name].html.erb
      locales/

After you invoke the above command, make sure you restart your rails process.

It's best advisable to namespace your assets directory so that it won't conflict with assets in other themes.

image_tag              'theme_a/logo.png' # => app/themes/theme_a/assets/images/theme_a/logo.png
javascript_include_tag 'theme_a/all'      # => app/themes/theme_a/assets/javascripts/theme_a/all.js
stylesheet_link_tag    'theme_a/all'      # => app/themes/theme_a/assets/stylesheets/theme_a/all.css

There is an example app at https://github.com/chamnap/themes_on_rails_example.

Controller

You can set theme in your controllers by using the theme declaration. For example:

class HomeController < ApplicationController
  theme 'basic'

  def index
    ...
  end
end

With this declaration, all of the views rendered by the home controller will use app/themes/basic/views/home/index.html.erb as its templates and use app/themes/basic/views/layouts/basic.html.erb.

You can use a symbol to defer the choice of theme until a request is processed:

class HomeController < ApplicationController
  theme :theme_resolver

  def index
    ...
  end

  private

    def theme_resolver
      params[:theme].presence || 'professional'
    end
end

Now, if there is a params[:theme], it will use that theme. Otherwise, it will use professional theme.

You can even use an inline method, such as a Proc, to determine the theme. For example, if you pass a Proc object, the block you give the Proc will be given the controller instance, so the theme can be determined based on the current request:

class HomeController < ApplicationController
  theme Proc.new { |controller| controller.params[:theme].presence || 'professional' }
end

Theme specified at the controller level support the :only and :except options. These options take either a method name, or an array of method names, corresponding to method names within the controller:

class HomeController < ApplicationController
  theme 'basic', except: [:rss]
end

With this declaration, the basic theme would be used for everything but the rss index methods.

Authors

themes_on_rails's People

Contributors

chamnap avatar victorperez avatar johnwmcarneiro avatar lowjoel avatar qichunren avatar stevenharman avatar bastengao avatar

Stargazers

 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.