Write PHP code in PHP
<?php echo htmlentities('<?php echo \'aoeu\'; ?>'); ?>
Try This
<<< is Heredoc
<?php
echo <<< EOF
<?php
echo <<< EOF
EOF;
?>
EOF;
?>
If you output some complex code definitely use a template engine like smarty.. otherwise your code will look a complete mess.
I once patched the propel ORM which does output PHP code without using a template engine. It generates all the model classes based on the XML configuration files. Their code was a big mess. Don't do it.