Publication URL TBB
In most cases, you will not be publishing the binary files (images/css/js) in the same location of where you publish pages. As a good practice, these external files will be stored in separate folders. In such cases, you will need to specify path (relative) in the page DWT template.
Generally external css files will be declared like
<link href="/styles/main.css" rel="stylesheet" type="text/css"/>
This would work only if you have styles folder immediately after server path.
http://<server>/styles/main.css
In Tridion, default publication URL is “/” but if you have specified any path in the Publication, you will need to append that path also. It should be accessed like.
http://<server>/<pub url>/styles/main.css
The below C# TBB will get the publication URL of the current publication.
//gets the current session object
Session session = engine.GetSession();
//gets page object from the package
Item item = package.GetByType(ContentType.Page);
//gets TcmUri object of Page
TcmUri tcmObj = new TcmUri(item.GetValue("ID"));
//generate tcm string format with publication id
string publicationId = string.Format("tcm:0-{0}-1", tcmObj.PublicationId);
//gets publication object from the current session
Publication publication = new Publication(new TcmUri(publicationId), session);
//writes the publication URL into the package
package.PushItem("PublicationUrl", package.CreateStringItem(ContentType.Text, publication.PublicationUrl));
As you aware, variable “PublicationUrl” will be included in the package so this variable is accessible when you include this TBB on your page template.
Now you need to call this variable on the page DWT like below,
<link href="@@PublicationUrl@@/styles/main.css" rel="stylesheet" type="text/css"/>
