Remove HTML whitespace from the template output

HTML published from Tridion is typically created using Tridion Dreamweaver Templates (DWTs).  These templates can involve a lot of ‘If’ statements to check that values exist or what their value is etc.  I like to put line breaks between each DWT statement so that its easier to read and work with, which usually looks something like this (actually it can get a lot crazier than this!):


The only issue with doing this is that HTML white-space is preserved, resulting in tonnes of extra line breaks in the published HTML. I’m anal about HTML, (there will be NO extra line breaks, NOT ON MY WATCH!) so I wrote a little TBB to clean up any lines that are blank, here’s the code:

        public override void Transform(Engine engine, Package package)
        {
            this.Initialize(engine, package);

            //get output item from the package
            string outputName = Package.OutputName;
            Item outputItem = package.GetByName(outputName);

            // if the output is text based, lets clean it up
           if ((outputItem.Type == PackageItemType.String))
            {
                Logger.Debug("Output exists, cleaning empty lines");
                string uglyOutput = outputItem.GetAsString();

                //Gets the converted output string
                string fixedOutput = Regex.Replace(uglyOutput, @"^\s*$\n", string.Empty, RegexOptions.Multiline);
                //Remove the old output string, and put the new one in place
                Logger.Debug("Output cleaned, updating item in package");
                package.Remove(outputItem);
                outputItem.SetAsString(fixedOutput);
                package.PushItem(outputName, outputItem);
            }
        }

The usual caveat about the code published here is that it is using the SDL Tridion base classes

3 thoughts on “Remove HTML whitespace from the template output

  1. Here is the same TBB, but as a C# Fragment. Simply, copy/paste and you’re good to go!

    //get output item from the package
    string outputName = Package.OutputName;
    Item outputItem = package.GetByName(outputName);

    // if the output is text based, lets clean it up
    if ((outputItem.Type == PackageItemType.String))
    {
    string uglyOutput = outputItem.GetAsString();

    //Gets the converted output string
    string fixedOutput = Regex.Replace(uglyOutput, @”^\s*$\n”, string.Empty, RegexOptions.Multiline);
    //Remove the old output string, and put the new one in place
    package.Remove(outputItem);
    outputItem.SetAsString(fixedOutput);
    package.PushItem(outputName, outputItem);
    }

    [WORDPRESS HASHCASH] The poster sent us ‘0 which is not a hashcash value.

  2. Of course the /right/ solution is to nag Tridion until they make the DWT mediator recognise TemplateBeginWhatever comments that have line breaks in them. Meanwhile that’s a nice bit of regexery, what with the linebreak after the $ and the multiline setting. Geek!

  3. For anyone else who stumbles upon this, if you go the C# Fragment route you also need .

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>