This post is over 6 months old. Some details, especially technical, may have changed.

Baler v0.2.1 Released and 2 New Extensions

Baler

I have just released a new version of Baler. Version 0.2.1 release adds a new feature for adding custom attributes to bales. The Attr function, available as an instance method to all bales, accepts an attribute name and a string value that will be applied to the rendered bale tag. The best way to demonstrate this is with an example.

CSS Media Attribute

When specifying link tags for CSS it is possible to scope that stylesheet to a specific meida type. For example you could have a stylesheet that modifies your page style for printing e.g.

<link rel="stylesheet" href="core.css" type="text/css" media="screen" />
<link rel="stylesheet" href="print.css" type="text/css" media="print" />

The new Attr method allows you to specify this new attribute like so,

Baler.Build("core.css").Attr("media", "screen").AsCss();
Baler.Build("print.css").Attr("media", "print").AsCss();

"No Value" Attributes

Some attributes, such as the async and defer on script tags do not require a value. Attr is capable of dealing with these type of attributes.

Baler.Build("script.js").Attr("async").AsJs()

Will produce

<script type="text/javascript" src="..." async></script>

I am sure there are other uses for it (setting id's or data-* attributes) as it's a fairly flexible little enhancement.

CSS Media Extensions

PM> Install-Package CodeSlice.Web.Baler.Extensions.CssMedia

The first extension to utilise this new Attr is the CssMediaExtensions addon. CssMediaExtensions provide methods for setting the media type that a stylesheet should render for such as screen or print

WithMedia()

The WithMedia Extension allows you to set the media attribute of the rendered CSS tag.

bale.Build(...).WithMedia("screen").AsCss();

AsCss()

AsCss overrides the existing AsCss method allowing you to pass a media attribute value directly into the render call. For example the above example could simply be written as,

bale.Build(...).AsCss("screen");

Named Bales Extension

PM> Install-Package CodeSlice.Web.Baler.Extensions.NamedBales

This one has been released for a while but I didn't get around to writing about it. TheNamed Bales extension provides a mechanism to declare a Bale definition up front and reuse the same bale throughout the application without having to redefine the contents over and over. For example we can define a bale with 3 scripts and name it

Baler.Build(
  "script1.js",
  "script2.js",
  "script3.js"
).NameAs("base")

This can returns the Bale itself so can be used inline or somewhere like Global.asax. Next we can render this bundle using AsJs later in the app like so,

NamedBales.Get("base").AsJs();

Currently this doesn?t short circuit Balers internal cache so a second cache check will be made using the internal hashing mechanism. This is either a good thing or a bad thing. But for now it?s a good thing! This does have the side effect of ensuring that any unnamed bales with the same signature as a named bale will still get cached bale.

NameAs()

NameAs adds some sugar to the IBale interface allowing us to apply a friendly name to a bale. There is currently no check to see if the bale name is already taken. Existing definitions will be overwritten.

NamedBales.Get()

Allows us to retrieve a bale based on a friendly name defined by the developer. Will throw and exception if the bale name doesn?t exist in the cache ? otherwise the bale itself.

Coming Up

Still plenty to be done. Docs are fairly complete but there are a mountain of unit tests to be written still and some extra features to be added (without bloating the code base of course). I have a few features I'd like to work in but I'll keep that for another day.

Published in .NET on June 08, 2011