<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>metaduck &#187; CSS</title>
	<atom:link href="http://www.metaduck.com/category/css/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.metaduck.com</link>
	<description></description>
	<lastBuildDate>Wed, 05 May 2010 12:04:43 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Tutorial: building and styling a Rails app with styled_objects</title>
		<link>http://www.metaduck.com/2009/10/tutorial-building-and-styling-a-rails-app-with-styled-objects/</link>
		<comments>http://www.metaduck.com/2009/10/tutorial-building-and-styling-a-rails-app-with-styled-objects/#comments</comments>
		<pubDate>Mon, 12 Oct 2009 22:53:23 +0000</pubDate>
		<dc:creator>Pedro Teixeira</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.metaduck.com/?p=88</guid>
		<description><![CDATA[How to keep your stylesheets clean and organized using Rails
This tutorial covers building a web application from scratch using Rails with the styled_objects plugin.
As an example we will be building a very static front-end website, mainly to focus on stylesheets.
Everything you get from can be applied to more complex applications.
Because this is such a simple [...]]]></description>
			<content:encoded><![CDATA[<p><strong><em>How to keep your stylesheets clean and organized using Rails</em></strong></p>
<p>This tutorial covers building a web application from scratch using Rails with the styled_objects plugin.</p>
<p><span id="more-88"></span>As an example we will be building a very static front-end website, mainly to focus on stylesheets.</p>
<p>Everything you get from can be applied to more complex applications.</p>
<p>Because this is such a simple application, we won't be using scaffolding generators.</p>
<h2>About styled_obejcts</h2>
<p><a href="http://wiki.github.com/pgte/styled_objects">styled_objects</a> <strong> </strong>is a Rails plugin for simplifying stylesheet management on your application.</p>
<p>Instead of having one or more large stylesheets on your public folder, have many. Keep your <span>CSS</span> close to their respective templates. <strong>styled_objects</strong> will compile them into one file per page.</p>
<h2>Getting started</h2>
<p>1. Bootstrap your Rails app</p>
<pre>$ rails testapp</pre>
<p>2. Install the LESS gem:</p>
<p>Edit your config/environment.rb and place, anywhere before the "end":</p>
<pre class="brush: ruby;">config.gem 'less'</pre>
<p>Run on the shell:</p>
<pre>$ sudo rake gems:install</pre>
<p>2. install the styled_objects plugin</p>
<pre>script/plugin install git://github.com/pgte/styled_objects.git</pre>
<h2>The application</h2>
<p>Here we will be building a simple example application for an e-commerce website.<br />
First we will be focusing on building the controllers and the views, and finally we will learn how to style them with styled_objects.</p>
<p>To start we will be having products and product categories. One product can be in many categories and one category can contain many products. So, it is a many-to-many relationship.</p>
<p>For the sake of simplicity, let's say a product has a name and a description.</p>
<p>Each category have a name.</p>
<p>(I won't go through the building of the models, it's beyond the scope of this tutorial).</p>
<h3>Product page</h3>
<p>After you have your migrations and models setup, we need to configure the routes for our products controller.</p>
<p>Edit your config/routes.rb and add:</p>
<pre class="brush: ruby;">map.resources :products</pre>
<p>Now build the controller under app/controllers/products_controller.rb. For now we will only have a "show" action:</p>
<pre class="brush: ruby;">
class ProductsController &lt; ApplicationController

  def show
    @product = Product.find(params[:id])
  end

end
</pre>
<p>Do the show template  under app/views/products/show.html.erb:</p>
<pre class="brush: ruby;">
&lt;h1&gt;&lt;%= h @product.name %&gt;&lt;/h1&gt;
&lt;p&gt;&lt;%= h @product.description %&gt;&lt;/p&gt;
</pre>
<h3>Category page</h3>
<p>On this page we will be presenting all products on the current category.</p>
<p>Build the product category routes ou config/routes.rb:</p>
<pre class="brush: ruby;">
config.resources :categories
</pre>
<p>Next, build the controller under app/controllers/categories_controller.rb:</p>
<pre class="brush: ruby;">
class CategoriesController &lt; ApplicationController

  def show
    @category = Product.find(params[:id], :include =&gt; :products)
  end

end
</pre>
<p>And the view under app/views/categories/show.html.erb:</p>
<pre class="brush: ruby;">
&lt;h1&gt;&lt;%= h @category.name%&gt;&lt;/h1&gt;

&lt;p&gt;products in this category:&lt;/p&gt;

&lt;%= render :partial =&gt; 'products/product', :collection =&gt; @category.products %&gt;
</pre>
<p>This template is a bit more complex than the product show one because it presents all the products inside the current category by calling the <em>products/_product.html.erb</em> partial for each product.</p>
<p>Let's do this partial (under app/views/products/_product.html.erb):</p>
<pre class="brush: ruby;">
&lt;h2&gt;&lt;%= link_to h(product.name), product_path(product) %&gt;&lt;/h2&gt;
&lt;p&gt;&lt;%= h truncate(product.description) %&gt;&lt;/p&gt;
</pre>
<p>Here, instead of presenting the full product description, we truncate it using the ActionView truncate helper, presenting only the first 30 characters of the product description.</p>
<h2>Styling up</h2>
<p>So, now that we have the category and product pages in place, we need to style them.</p>
<p>But first we need to style the whole website.</p>
<h3>Global styling</h3>
<p>Personally, I feel comfortable with using this folloing setup when starting a website, but it is not a requirement for styled_objects. (We haven't got there yet).</p>
<h4>Reset stylesheet</h4>
<p>There are numerous reset stylesheets out there. I particulary like the one bundled inside blueprint CSS.</p>
<p>So, <a href="http://www.blueprintcss.org/">grab a copy of blueprint</a> and extract the <em>blueprint/src/reset.css</em> file and copy it into <em>public/stylesheets/</em> directory.</p>
<p>Next, edit your <em>app/views/layouts/application.html.erb</em> and add the following line before the &lt;/head&gt; tag:</p>
<pre class="brush: xml;">

&lt;%= stylesheet_link_tag 'reset' %&gt;
</pre>
<h4>Typography sytlesheet</h4>
<p>This is where we add a special stylesheet only for defining your typography. This is where you will be styling your p, a, ul, li, table, etc. tags.</p>
<p>This is your job, but I like to start from the blueprint/src/typography.css bundled on blueprint.</p>
<p>Save this file into public/stylesheets and add the respective the stylesheet link tag right after the reset one:</p>
<pre class="brush: xml;">
&lt;%= stylesheet_link_tag 'reset' %&gt;
&lt;%= stylesheet_link_tag 'typography' %&gt;
</pre>
<p>This stylesheet you will be formatting according to the desired look of your website.</p>
<h4>Layout stylesheet</h4>
<p>Before building a layout stylesheet, you need to build your layout markup. This will be done inside <em>app/layouts/application.html.erb</em> :</p>
<pre class="brush: php;">
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;
&lt;html&gt;
	&lt;head&gt;
		&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot; /&gt;
		&lt;title&gt;Untitled Document&lt;/title&gt;
		&lt;%= javascript_include_tag 'jquery-1.3.2.min' %&gt;
		&lt;%= stylesheet_link_tag 'reset' %&gt;

	&lt;/head&gt;
	&lt;body&gt;
      &lt;div class=&quot;container&quot;&gt;
        &lt;div id=&quot;header&quot;&gt;&lt;h1&gt;Welcome to ACME&lt;/h1&gt;&lt;/div&gt;
        &lt;div id=&quot;main&quot;&gt;
          &lt;%= yield %&gt;
        &lt;/div&gt;
      &lt;/div&gt;
	&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>So, on inside our HTML body tag we have a div tag with a "container" class. This "container" class will be used to center and fix the width of our content.</p>
<p>This is what goes inside the layout stylesheet, under public/stylesheets/layout.css:</p>
<pre class="brush: css;">
.container {
  width: 950px;
  margin: 0 auto;
}
</pre>
<p>So, we are setting the width of content area of our website and centering it.</p>
<p>A lot more could be done here, like styling the <em>header</em> div. This also should be done on the layout stylesheet.</p>
<p>The main idea here is: <strong>use the layout stylesheet to only style markup on the layout template. Don't style more than that here</strong>.</p>
<h3>Local styling</h3>
<p>Now we want to style our templates and partials individually. That's where styled_objects comes in.</p>
<p>This plugin allows you to define the styles per template or partial wiathout adding any code.</p>
<p>First we need to call this helper on the <em>app/views/layouts/application.html.erb</em> file:</p>
<pre class="brush: ruby;">&lt;%= so_stylesheet_link_tag %&gt;</pre>
<p>Place it right before the &lt;/head&gt; tag. This helper creates the link tag to the URL for the "glued" stylesheet of each page.</p>
<p>We now begin with the product show page.</p>
<p>Create app/views/products/show.css and edit it.</p>
<p>We want to style the title and the description:</p>
<pre class="brush: css;">background-color: #e0e0e0;
h1 {color: #f576d8; border-bottom: 1px solid #822321};
p {padding: 1em 0; background-color: #fff;, border-top: 1px solid #d6d6d6}
</pre>
<p>On the first line we see something that isn't pure CSS: the background color style declared on the root of the CSS document. That's there because we are styling the <em>show.html.erb</em> template, not any sub node.</p>
<p>Now we want to style the product category page.</p>
<p>Create <em>app/views/categories/show.css</em>:</p>
<pre class="brush: css;">
border-top: 5px solid black;
background-color: #f7f7f7;
h1 {letter-spacing: -2px; color: #3e3e3e}
</pre>
<p>Here we're giving the category show template a top border (line 1), a background-color (line 2) and we're also styling the title on line 3.</p>
<p>This category show template also shows the products and for that it uses the <em>products/_product.html.erb</em> partial.</p>
<p>We also wish to style this partial. Create the file <em>app/views/products/_product.css</em>:</p>
<pre class="brush: css;">background-color: #999;
border: 1px solid #333;
float: left;
h2 {text-transform: uppercase}
</pre>
<h3>AJAX loading</h3>
<p>So, styled_objects collects the needed CSS fragments on each page and constructs one URL that references all the "glued" CSSes.</p>
<p>What if I want to have a shopping basket partial that is called by AJAX instead of normal partial rendering inside a template?</p>
<p>Simple: just add &lt;%= so_include_partial(partial_path) %&gt; (or &lt;%= so_include_template(template_path) %&gt; if it's a template) to anywhere on the calling template. The partial CSS will be included.</p>
<p>Next: <a href="http://www.metaduck.com/2009/10/tutorial-advanced-styled-objects-on-rails/">See the Advanced styled_objects on Rails Tutorial</a></p>
<h2>More</h2>
<p>Read more about object-oriented CSS here:</p>
<ul>
<li><a href="http://wiki.github.com/stubbornella/oocss">Object-oriented CSS by stubbornella</a></li>
</ul>
<p>See the presentation video:</p>
<ul>
<li><a href="http://www.stubbornella.org/content/2009/03/23/object-oriented-css-video-on-ydn/">Object-oriented CSS by stubbornella</a></li>
</ul>
<h2>Other resources</h2>
<ul>
<li><a href="http://lesscss.org/docs.html">LESS syntax</a></li>
<li><a href="http://wiki.github.com/pgte/styled_objects">styled_objects Rails plugin</a></li>
<li><a href="http://www.blueprintcss.org/">blueprint CSS framework</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.metaduck.com/2009/10/tutorial-building-and-styling-a-rails-app-with-styled-objects/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
