10

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.
Rob Himself
  • 353
  • 1
  • 7
  • Are you on SP1 Hotfix Rollup 1, and are you doing static publishing only? – Nickoli Roussakov Nov 08 '12 at 18:57
  • Hey Rob. If any of the answers below helped you sort your problem, can you please accept it by clicking the large check mark that shows to the left of it? – Frank van Puffelen Nov 10 '12 at 00:36
  • Frank, absolutely. I guess I got so engrossed in development I forgot to come back and accept! Nickoli (as usual) gave a great response, but for now I'll be going with Chris's suggestion. Thank you to all for your responses! – Rob Himself Nov 11 '12 at 00:30

3 Answers3

6

I'm assuming that you're doing static publishing only, i.e. not using the Tridion Content Broker.

You should be able to do this with the new TOM.NET-based Tridion Event System and subscribe to the Publishing event at the Initiated phase. This means that just before the page is starting to publish, you will catch the event and modify the page filename. However, this will make the page have the new name in the CME. So again, using another event phase after the publish transaction goes through, the TransactionCommitted phase, you can change the name of the page back.

You can also write a custom deployer extension to do this, which will rename the page. However, you will need to also have code to manage the "unpublishing" of the renamed page as well. See Jaime's blog post on how to write a Deployer extension: http://sdltridionworld.com/articles/sdltridion2011/tutorials/Deployer_Extensions_With_Eclipse_1.aspx

Nickoli Roussakov
  • 3,434
  • 16
  • 22
  • Static publishing, yes. Tinkering in the Event System doesn't dishearten me too badly, but I'd been hoping this could be accomplished a bit more easily via TBB in the PT. – Rob Himself Nov 08 '12 at 19:17
  • I appreciate your suggestion on the custom deployer extension. I'll give that a read to see if that's a desirable path for me. :D – Rob Himself Nov 08 '12 at 19:17
  • The Event System is probably an easier approach that the Deployer Extension. I'm weary of being able to do this in a TBB because you're already inside a publish session, and you may not be able to write TOM.NET objects that are within the package of your session. If you can do it, I would expect that any modification of the Page or Component object would actually take after the session completes. – Nickoli Roussakov Nov 08 '12 at 19:19
6

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.

Chris Summers
  • 10,153
  • 1
  • 21
  • 46
  • 3
    Just to clear any questions that might come up at some point, please note Tridion will indeed unpublish the variant, but it might not be directly with the unpublish of this Page. It is related to the use of your "mmc" Component, when that is not used on any Published Page anymore, all variants of it will be unpublished too. – Bart Koopman Nov 09 '12 at 08:55
  • This is most straight forward approach. The reason I didn't suggest it in my answer is because I've experienced issues with having the variants unpublished in pre-SP1 versions of Tridion despite no other items using them. – Nickoli Roussakov Nov 09 '12 at 15:23
  • 1
    Chris, I really appreciate the suggestion. As it's the closest to my current attempt I'll give it a shot immediately, and post back with my findings. Heck, I may even take this experience and turn it into a blog post for other Tridion developers with the same need. – Rob Himself Nov 09 '12 at 15:51
  • Chris, I got this working by the way. Thank you very much for the suggestion. Now I just need to figure out why the CSS is being rendered enclosed in a tcdl:ComponentPresentation tag, and I'll be good to go. Perhaps that's a different topic. – Rob Himself Nov 12 '12 at 17:09
  • 1
    Yes - make a new topic for that one - But my guess is that you are calling your 'create binary' TBB before calling the 'Clean Up' TBB to the page template – Chris Summers Nov 12 '12 at 19:39
  • ^ That did it. Not sure why I didn't catch that before. Thanks! – Rob Himself Nov 15 '12 at 22:34
2

It's difficult to provide the most appropriate answer not understanding the context/reason for not changing a filename in the CM but changing it outside ... I'd usually recommend simply managing two pages?

There are other options you could investigate...

Content Deployer Extension.

Event System as Nick suggests.

Both of the above you need to consider the impact on unpublishing too.

You could have a server side simple app watch for the file to be published and copy it (it being the Tridion published file) over the {renamed} version of the file (Chris discusses something similar here Can we customize Deployer using .NET?)) - moreso if you want to avoid Java coding for content deployers.

Community
  • 1
  • 1
  • Additionally I'm sure since 2011 SP1 the template approach is not recommended as calling PublishEngine.Publish() in our templates is not out of the box any more and has to be 'allowed' with a parameter of allowWriteOperationsInTemplates. – Dylan .. Mark Saunders Nov 08 '12 at 20:11
  • I appreciate the response. The filename, as it resides in CM, is for the base unminified version of the file. If I publish this out, and I don't wish to minify my output, I have an expectation that the filename will indicate this by not having "_min" at the end of it. If I publish it out, and I want my output minified, I not only need to make sure I'm not overwriting a non-minified version, but also giving a visual indication that the file is compressed. – Rob Himself Nov 08 '12 at 20:23
  • I /could/ go the route of managing two pages in Tridion, but for my content folks, the usability of flipping a toggle versus creating two pages that contain the exact same CPs with only a slightly different filename... the convenience and efficiency factor lies with only one of those. – Rob Himself Nov 08 '12 at 20:25