<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>clog: A hacked theme_support that (mostly) works</title>
    <link>http://blog.chrispcritter.com/articles/2007/03/26/a-hacked-theme_support-that-mostly-works</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>A hacked theme_support that (mostly) works</title>
      <description>&lt;p&gt;A theme should allow the look and feel of an application to change.  A theme has a one-to-one relationship with the application, meaning that an application should only be running one theme at a time.  A theme per user would be a &amp;#8216;skin&amp;#8217; and an application that runs multiple sites with a different &amp;#8220;theme&amp;#8221; for each one is a whole new can of worms.  I&amp;#8217;ve had a need to do the latter recently, but I&amp;#8217;ll go over my (site_support) solution to that in the next post.  Meanwhile, here&amp;#8217;s my solution for adding themes to your application.&lt;/p&gt;

&lt;p&gt;When to use it: A single application connects to a single database and has multiple theme options, but only one is running at a time.&lt;/p&gt;

&lt;p&gt;Start by installing my hacked version of theme_support&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;script/plugin install http://www.chrispcritter.com/theme_support_mod/ &lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;ActionMailer currently doesn&amp;#8217;t work with it, I&amp;#8217;ll have to tackle a fix for it soon.  I&amp;#8217;ll post my findings.&lt;/p&gt;

&lt;p&gt;At this point you should at least be able to get your poject running in mongrel.&lt;/p&gt;

&lt;p&gt;To create the them, you&amp;#8217;ll need to create a &amp;#8220;themes&amp;#8221; folder in your project and start your first theme.  The theme must have the following structure:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;  $app_root
    themes/
      [theme_name]
        cache/
        images/
        stylesheets/
        javascripts/
        views/           
          layouts/      &lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You may notice that this is a little different than the structure described in the theme_support documentation.  I could not get layouts to work outside of the view path (so I just moved it into views).  I really think this is better anyway - since it matches the standard view structure.  The layout I describe also has a &amp;#8220;cache&amp;#8221; folder.  The theme_support plugin supposedly supports cached files in a public/themes structure but I had some difficulty getting this to work and really wanted to keep all theme related content in it&amp;#8217;s own folder.  I ended up setting page caching to go into the themes/[theme_name] folder with some mod_rewrite magic.  It may be better to add a &amp;#8220;public&amp;#8221; folder to each theme for this at some point.&lt;/p&gt;

&lt;p&gt;Add a line similar to the following to config/environment.rb (where &lt;DEFAULT&gt; is your default theme):&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;THEME = ENV[&amp;quot;THEME&amp;quot;] ? ENV[&amp;quot;THEME&amp;quot;] : '&amp;lt;DEFAULT&amp;gt;'&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And add the below line to pull out your site specific configuration options.&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;CONFIG = YAML.load(File.read(&amp;quot;#{RAILS_ROOT}/config/#{THEME}.yml&amp;quot;))&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you&amp;#8217;re using apache, your rewrite rules will look like the following:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;RewriteRule ^([^.]+)$ themes/&amp;lt;THEME&amp;gt;/$1.html [QSA]
RewriteRule ^images/(.*)$ themes/THEME&amp;gt;/images/$1 [QSA]&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You will need to modify the caching paths in environment.rb so they point to the right place.  Add the following lines:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;ActionController::Base.fragment_cache_store = :file_store, &amp;quot;#{RAILS_ROOT}/themes/#{THEME}/cache&amp;quot;
ActionController::Base.page_cache_directory = &amp;quot;#{RAILS_ROOT}/themes/#{THEME}&amp;quot;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Add a link in public that points to your themes directory &amp;#8220;public/themes -&gt; ../themes/&amp;#8221;
That will preserve the routes.  &lt;/p&gt;

&lt;p&gt;It&amp;#8217;s not the most elegant solution but it works.  It bypasses some routes functionality and the sym-link in particular is ugly.  I&amp;#8217;ll post more mods to theme_support as/if I make them along with simpler install instructions, but this was really just a stepping stone to my site_support setup - so it may be awhile before I get back to it.&lt;/p&gt;</description>
      <pubDate>Mon, 26 Mar 2007 14:29:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:cf7b6002-fcf9-4002-bf44-1d161d8f1538</guid>
      <author>chrisp</author>
      <link>http://blog.chrispcritter.com/articles/2007/03/26/a-hacked-theme_support-that-mostly-works</link>
      <category>Development</category>
      <category>Ruby and RubyOnRails</category>
      <category>RubyOnRails</category>
      <category>Ruby</category>
      <trackback:ping>http://blog.chrispcritter.com/articles/trackback/28</trackback:ping>
    </item>
  </channel>
</rss>
