Skip to content
This repository has been archived by the owner on May 1, 2023. It is now read-only.

caiena/template_assets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Template Assets - organizing your assets!

Template Assets provides a way to separate template specific assets (i.e. your User <form> specific logic) from app-wide assets (like your graph library) in a simple Rails-way naming convention.

Your users#show action should render a show.html.* template, right? Template Assets tries to ease organizing and including this template-specific assets by searching for (if exists) the corresponding show.js.* and show.css.* assets.

# Your html template file location
app/
├── views/
|   ├── users
|   |   ├── show.html.haml

===

# Your "template assets" location
app/
├── assets/
|   ├── javascripts/
|   |   ├── views/
|   |   |   ├── users/
|   |   |   |   ├── show.js.coffee
|   ├── stylesheets
|   |   ├── views
|   |   |   ├── users
|   |   |   |   ├── show.css.scss

Install

Add the following to your Gemfile:

gem 'template_assets', github: 'caiena/template_assets'

and run bundle install from your shell.

After installing the gem, setup it on your Rails app with:

rails g template_assets:install

It should create an initializer at config/initializers/template_assets.rb.

And, finally, add to your layouts (check the ADD THIS! comments):

# ex: app/views/layouts.application.html.haml
%html
  %head
    %meta{:content => "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}/
    %title Lise
    = csrf_meta_tags
    = stylesheet_link_tag    'application', media: 'all'

    -# ADD THIS!
    -# Enabling template javascripts
    = template_stylesheet_link_tag_if_exists

  %body
    -# your markup here...

    = javascript_include_tag 'application'
    -# ADD THIS!
    -# Enabling template javascripts
    = template_javascript_include_tag_if_exists

And vòila!