Write Your Own Compass Mixin

As a developer working with CSS, one of the things that I find a bit troubling is the amount of style definitions that I have to repeat over and over to achieve the design I desire. One of the basic tenets of software development is to utilize the DRY principle, otherwise known as Don’t Repeat Yourself. Fortunately, when you implement Compass and SASS in your project to generate your CSS, you have a way to avoid copy-paste programming.

Compass adds funtionality to SASS that allows us to create mixins that you can call from anywhere in your .scss files, passing in parameters that can be defaulted if not provided. A sample mixin would be similar to the ellipsis mixin below, along with its usage.

@mixin ellipsis() {
	overflow: hidden;
  	white-space: nowrap;
  	text-overflow: ellipsis;
}
.ellipsis {
  	@include ellipsis;
}

While this is nice, and works well, you can add a few things to this ellipsis mixin to make sure that it works with a few additional features added in. Lets say we want all text that we are going to have automatically apply an ellipsis to should be blue and underlined. We have a couple of ways of doing it. The first way doesn’t take advantage of the power of a mixin.

@mixin ellipsis() {
	overflow: hidden;
  	white-space: nowrap;
  	text-overflow: ellipsis;
}
.ellipsis {
  	@include ellipsis;
    text-decoration: underline;
    color: blue;
}

This simply adds the desired styles to the .ellipsis class, making sure that it does not apply anywhere else. However, if we decide to make this functionality come with the mixin, then we can easily add those parameters to the call of the mixin like so.

@mixin ellipsis($color, $textDecoration) {
	overflow: hidden;
  	white-space: nowrap;
  	text-overflow: ellipsis;
    color: $color;
    text-decoration: $textDecoration;
}
.ellipsis {
  	@include ellipsis(blue, underline);
}

This will accomplish what we want, but it does require us to pass in the parameters for the color and whether we want the text underlined or strikethrough, but it makes it more difficult to simply add an ellipsis to text. Instead, we can do the following.

@mixin ellipsis($color: black, $textDecoration: none) {
	overflow: hidden;
  	white-space: nowrap;
  	text-overflow: ellipsis;
    color: $color;
    text-decoration: $textDecoration;
}
.ellipsis {
  	@include ellipsis(blue, underline);
}
.ellipsis.noSpecial {
	@include ellipsis;
}

Now, we can optionally pass in the color of the text we want and how we want the text decorated, and if we don’t want any special decorations at all, we can simply call @include ellipsis without passing in any parameters.

Related Posts

Apr 18, 2014
2 minutes

Magento Adminhtml and Relative Links to Static Resources

Relative links in URLs allow you to only specify the path to an resource that is in the same or subfolder of the current folder. Lets say the current page you are on is http://example.com/test/ and you want to reference an image at http://example.com/test/image.jpg. You could put the full http://example.com/test/image.jpg in the src attribute of the img tag, or you could use just image.jpg instead. This works well when you are not sure what the directory path is the parent directory of your code. However, it can cause some issues when your code is moved to another location, but some resources are not moved, such as image.jpg.

Mar 24, 2015
2 minutes

Laravel Removed The QuickStart For Version 5

To start out, I want to be clear that what follows should not be interpreted to be a criticism of the software framework that those that work on Laravel publish, nor an indictment of open-source software as a whole. Rather, it is a look at how some projects, open or closed source make it harder than it should be for new users/developers to utilize their terrific products.

It seems experts conveniently “forget” the tips and tricks and tribulations it takes to learn a new technology, covering it all with, “It’s just so easy”.

Mar 27, 2015
2 minutes

2 Ways To Find Current Directory in PHP Without Regular Expressions

There comes a time when you need to find the current directory in PHP, test to see if it is the directory that you expect it is, and take an action based on the test results. Obviously, the easiest way to get the current working directory in PHP is getcwd(). However, parsing the output of this function can provide some interesting challenges.

While it is trivial to do this sort of search with a Regular Expression, I tend to look for a solution that is easier to understand its functionality without reaching for the reference books. As a result, I would typically use one of the two methods below. One thing to note is that in each scenarion, any directory name in the path will match, not just the current directory. This can be easily updated to only match the single current directory the script is executed from.