Good day!
I allow my content editors to store CSS as very basic components (usually containing a single, multi-line field called "code" that they paste into), and these are then added as Component Presentations into a Page with a .css file extension. When creating the page, users are able to set a few configuration values: minify output (bool), file name prefix, and file name suffix. The intent of those last two is that if the user has selected to minify the CSS on its way out the door, the file name can be different sitting on the presentation server.
I've got everything working except the modification of the file name. I don't want to change the file name in the CM; only as it resides out on the presentation server. I assume this can be done in a TBB placed into the CSS Page Template. I took a crack at it, but want to be sure that there's not something I'm missing. The following example is just the shorthand with some configurable values hard-coded for brevity.
// Create a reference to the Page object in the package.
Page page = this.GetPage();
// Retrieve a reference to the page's file name.
string currentFileName = Utilities.GetFilename(page.FileName);
// Set the published file name on its way out the door.
page.FileName = currentFileName + "_min";
// ???
// Profit.
Reading your answers to @Dylan's response, you might consider creating a Binary Variant at publish time which contains the output of your minimized code.
In it's simplest form, you would create a text file with the output of your page, and then call .AddBinary()
specifying the contents of your file, a file name, a variant name (I suggest the Page URI for this), URI of the current StructureGroup and the URI of a Component to bind this too (probably the Component on the Page).
You can see some binary variant examples on Mihai's blog here
Binary binary = m_Engine.PublishingContext.RenderedItem.AddBinary(
resizedStream, newFilename, variantId, mmc,
binaryContent.MultimediaType.MimeType);
This will publish a file containing the output of the page in addition to the actual page. When you unpublish the page, you will unpublish the extra file as well.