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"; ?>
..is removed when it is pushed to the Package as an HTML string, for example:
package.PushItem("phpstring",
package.CreateStringItem(ContentType.Html,
"<?php echo "hello internets";?>
<?php echo "hello internets"; ?>
"));
will simply give you the package string:
 <p></p>
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:
- In your original php string switch the<?php and ?> php tags to [?php and ?]
- Push this string as normal to the package as
ContentType.Html
- Create a new TBB that will switch these values [?php and ?] back to and ?>
- Ensure the original output is removed and push the new output back to the package with the
ContentType.Text
- 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
package.Remove(outputItem);
// put the new one
package.PushItem(Package.OutputName,
package.CreateStringItem(ContentType.Text,
fixedOutput));
}
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
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.
Hi Nick, that’s strange, in the machine I’m working on both and