Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tridion 2011: Changing a page's file name as it's being published

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.
like image 639
Rob Himself Avatar asked Nov 08 '12 18:11

Rob Himself


3 Answers

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.

like image 140
Chris Summers Avatar answered Oct 05 '22 04:10

Chris Summers


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

like image 34
Nickoli Roussakov Avatar answered Oct 05 '22 03:10

Nickoli Roussakov


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.

like image 44
Dylan .. Mark Saunders Avatar answered Oct 05 '22 02:10

Dylan .. Mark Saunders