Working with PHP in your SDL Tridion templates

Who doesn’t love PHP?  PHP is used by over 20 million web sites and 1 million servers so chances are you may need to build a website in SDL Tridion that is publishing out PHP files to your web server.

If you’re going down the route of outputting php code into your website you may have noticed that php syntax for example..

 <?php echo "hello internets"; ?> removed when it is pushed to the Package as an HTML string, for example:

"&lt;?php echo "hello internets";?&gt;
&lt;?php echo "hello internets"; ?&gt;

will simply give you the package string:


It’s likely that the phpstring will also contain HTML code (my favourite part :)) so if this is pushed as to the package as a ContentType.Text any html will be encoded :(

To workaround I have found is to do this:

  1. In your original php string switch the<?php and ?> php tags to [?php and ?]
  2. Push this string as normal to the package as
  3. Create a new TBB that will switch these values [?php and ?] back to and ?>
  4. Ensure the original output is removed and push the new output back to the package with the
  5. Place this TBB into your template after the DWT has ran

The following C# tbb provides the steps for 3 and 4 noted above:

public void Transform(Engine engine, Package package)
{ // get the output
 Item outputItem = package.GetByName(Package.OutputName);
 string badOutput = outputItem.GetAsString();
 // switch the php tags back from [ to < etc string
 fixedOutput = badOutput.Replace("[?", "<?");  fixedOutput = fixedOutput.Replace("?]", "?>");
 // remove the old package item
 // put the new one

3 thoughts on “Working with PHP in your SDL Tridion templates

  1. Edit: I’m having fun with with the Code Highlighter plugin in wordpress you might have to clean out some of the HTML mark up in the code snippets above :)

  2. You can avoid using this ‘replace’ TBB by specifying the full php tag, . It seems only the short tag conflicts with the DWT mediator.

    Also, here is what the PHP documentation states:
    PHP also allows for short tags (which are discouraged because they are only available if enabled with short_open_tag php.ini configuration file directive, or if PHP was configured with the –enable-short-tags option.

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>