[Nitro] Dynamic CSS

Chris Farmiloe chris.farmiloe at farmiloe.com
Fri Sep 16 12:22:46 EDT 2005


Hiya:

Intresting timing... I just started using the Template processor
to run my css through....

<code>
response.content_type = 'text/css'
Template.process_template(get_file_contents(css_filename), '@out',  
binding)
</code>

codes a bit out of context but you get the idea :)

The precompiling is great but the main reason I had for adding <?r ?>  
fun to
css was to avoid having to use rubbish 'hacks' to fix for various  
browsers and
return cleaner CSS... the browsers will cache the css anyway so load  
wont be a
problem.

I (quickly) made some mini extensions to the
request object to help me with my css organisation... think ill make  
them more
reliable soon and include some optional version checking too
so request.from_gecko? would be a general true and request.from_gecko? 
('>4')
return better than v4 versions .... simple but might be handy for  
someone

<code>
module Nitro

module Request

   # some useful mini methods for browser testing

   def from_gecko?
     headers['USER-AGENT'] =~ /Gecko/
   end
   alias_method :from_mozilla?, :from_gecko?

   def from_ie?
     headers['USER-AGENT'] =~ /MSIE/
   end
   alias_method :from_msie?, :from_ie?

   def from_opera?
     headers['USER-AGENT'] =~ /Opera/
   end

   def from_khtml?
     headers['USER-AGENT'] =~ /KTHML/
   end
   alias_method :from_safari?, :from_khtml?

   def from_w3c?
     from_gecko? or from_khtml? or from_opera?
   end

end

end
</code>



enjoy



On 16 Sep 2005, at 14:57, George Moschovitis wrote:


> Hello all,
>
> I just added a small but useful feature to Nitro: Dynamic css or CSS
> Templates. What this means is that you can use Nitro's advanced
> templating mechanism to build your templates:
>
> for example:
>
> <?r
>   mygreen = '#0f0'
>   bgcol = '#ff'
> ?>
>
> .myclass {
>   background: #{mygreen}
> }
>
> .another {
>   color: #{mygreen}
> }
>
> or even:
>
> <?r %w{ myclass another help }.each do |klass| ?>
> .#{klass} {
>   color: #{mygreen}
> }
> <?r end ?>
>
> of course you can use <% .. %> instead of <?r .. ?>
>
> This feature is really useful to avoid messy css files and/or easily
> changing colors in css files.
>
> At the moment the implementation is rather simple. You have to define
> which templates to 'precompile' in run.rb:
>
> ie
>
> Compiler.precompile 'css/style.css'
>
> will transform the file "#{template_root}/css/style.csst" into
> "#{public_root}/css/style.css"
>
> this way the generated css is served by Lighttpd/Apache. The template
> is compiled only if the source file is modified.
>
> Can anyone think of a better interface or better name than precompile?
>
>
> Thanks in advance,
> George.
>
> -- 
> http://www.gmosx.com
> http://www.navel.gr
> http://www.nitrohq.com
>
> _______________________________________________
> Nitro-general mailing list
> Nitro-general at rubyforge.org
> http://rubyforge.org/mailman/listinfo/nitro-general
>
>





More information about the Nitro-general mailing list