I love the idea of being able to share constants between my sass/css
files.  I wanted to go one step further and not have to do the @import
call.  So, I made a modification to the engine/plugin which looks for
the file "constants.sass" next to your sass file and reads the
constants from there if the file exists (convention over
configuration!).  My change also makes the css files get regenerated
if you make a change to the constants file.  If this looks good, I
would love to see my patch committed.  Here are my svn diffs:


===================================================================
--- plugin.rb   (revision 529)
+++ plugin.rb   (working copy)

       private

       def template_filename(name)
         "#{@@options[:template_location]}/#{name}.sass"
       end

       def css_filename(name)
         "#{@@options[:css_location]}/#{name}.css"
       end

       def stylesheet_needs_update?(name)
-        !File.exists?(css_filename(name)) ||
(File.mtime(template_filename(name)) - 2) >
File.mtime(css_filename(name))
+        !File.exists?(css_filename(name)) ||
+          (File.mtime(template_filename(name)) - 2) >
File.mtime(css_filename(name)) ||
+          needs_updating_by_constants(name)
       end
+
+      def needs_updating_by_constants(name)
+        constants_file = template_filename('constants')
+        return false unless File.exist?(constants_file)
+        return File.mtime(constants_file) >
File.mtime(css_filename(name))
     end
+
   end
+  end
 end


===================================================================
--- engine.rb   (revision 529)
+++ engine.rb   (working copy)
@@ -68,8 +68,25 @@
       @template = template.split(/\n\r|\n/)
       @lines = []
       @constants = {}
+
+      load_global_constants
     end

+    def load_global_constants
+      constants_file = File.dirname(@options[:filename]) + '/
constants.sass'
+      if File.exist? constants_file
+        # swap out template contents with the content of my constants
file
+        original_template = @template
+        @template = File.read(constants_file)
+
+        # do a render to populate constants hash
+        render
+
+        # swap template contents back in
+        @template = original_template
+      end
+    end
+
     # Processes the template and returns the result as a string.
     def render
       begin


cheers,
Haakon


On May 29, 3:24 pm, Robin <[EMAIL PROTECTED]> wrote:
> Weepy, there's a workaround for the import bug.  Add this to your
> environment:
>
> Sass::Plugin.options[:always_update] = true
>
> It will forcesassto recompile everything every time.
>
> On May 28, 5:26 pm, weepy <[EMAIL PROTECTED]> wrote:
>
> > heya nathan
>
> > - just wondering if there was an eta on the @import bug ? save me
> > deleting appplication.css every time i make change tosass!
>
> > - on another note - what are your thoughts on promoting the 2nd syntax
> > - i doubt many people is using it since im not sure they'd know about
> > it !
>
> > weepy
>
> > On May 5, 8:57 pm, Nathan Weizenbaum <[EMAIL PROTECTED]> wrote:
>
> > > Yes, this is a flaw. It's in the TODO, and will be fixed before 1.7 is
> > > released.
>
> > > - Nathan
>
> > > Robin wrote:
> > > > Hey Nathan,
>
> > > > This is a really awesome feature.  However, I think I've found a bug
> > > > with
> > > > the compile behavior, where files are not always regenerated.
>
> > > > Let's say you have parent.sassand child.sass, where parent @imports
> > > > child.
>
> > > > If you make a change to child.sass, save it, then reload your page in
> > > > the browser, parent.sasswill not be recompiled because it has not
> > > > changed, even though it @imports content that has changed!
>
> > > > On Mar 25, 6:00 am, Nathan Weizenbaum <[EMAIL PROTECTED]> wrote:
>
> > > >> Hello, fellow Hamlites,
>
> > > >> While it's nice to be able to create standaloneSasstemplates, each
> > > >> website usually has at least three or four CSS files.Sassis wonderful
> > > >> for reducing the amount of repetition within a document, but what does
> > > >> it offer to minimize repeated code between several files? Is there a 
> > > >> way
> > > >> to have all the CSS files share a few core rules or even (dare I
> > > >> suggest!)constantdefinitions?
>
> > > >> Well, until now, there wasn't. But why would I have brought those 
> > > >> issues
> > > >> up, in a Sneak Peek no less, if I weren't going to show a solution?
> > > >> Well, here's that solution: "@import". You may recognize that from CSS.
> > > >> Indeed, inSassit works much the same way. It imports both the rules
> > > >> and the constants from anotherSassfile into the current file. So, for
> > > >> example, if you had aSassfile with aconstantdefinitions and a small
> > > >> rule:
>
> > > >>   // shared.sass
> > > >>   !color = #1356e3
>
> > > >>   a img
> > > >>     :border-style none
>
> > > >> You couldimportit into another file
>
> > > >>   // main.sass
> > > >>   @importshared
>
> > > >>   #navbar
> > > >>     :background-color = !color
>
> > > >> And this would compile to
>
> > > >>   a img {
> > > >>     border-style: none; }
>
> > > >>   #navbar {
> > > >>     background-color: #1256e3; }
>
> > > >> You may notice that I included just "shared", leaving off the ".sass"
> > > >> file extension. This is acceptable, as is explicitly including
> > > >> "shared.sass". If the file extension is removed and no file called
> > > >> "shared.sass" is found, theimportdirective will just compile to a
> > > >> literal CSS "@importshared.css". This makes it easy to transition from
> > > >> CSS toSasswithout having to change the imports whenever you switch
> > > >> over a template.
>
> > > >> As always, this feature is available right now in the Haml trunk, which
> > > >> can be installed using "./script/plugin 
> > > >> installhttp://svn.hamptoncatlin.com/haml/trunk"; for Rails or "svn co
> > > >> svn://hamptoncatlin.com/haml/trunk" standalone. Give it a try.
>
> > > >> Enjoy!
> > > >> - Nathan


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Haml" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/haml?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to