Using PHP In Templates

PHP code within SC templates can be used in two ways or phases:t

  • render the page contents - after the SC page rendering process
  • render the page contents - before the SC page rendering process

The following example is a simple page template that demonstrates usage of PHP code to render a part of page content after the SiteCake completes the page rendering. The configura       tion constant PHP_TEMPLATE (sitecake/server/config.php) should be set to false.

<html>
<head>

</head>
<body>
   <div class=”sc-content-editableArea”>
      <p>Page content</p>
   </div>
   <div class=”footer”>
      <?php
          include “extern_php_code.php”;
          echo render_footer();
      ?>
   </div>
</body>
</html>

SiteCake first renders the page filling in the content of the editableArea container. After that, the complete page is evaluated as a PHP script. This is the moment when the PHP script block can execute arbitrary action and, for example, render the content of the footer.

The next example is a simple page template that shows how PHP could be used to render a page template. The configuration constant PHP_TEMPLATE (sitecake/server/config.php) should be set to true.

<!-- this is a PHP template part -->
?php
   include “needed_scripts.php”;
   echo “<html><head>...</head><body>”;
   if ( isset($_GET[‘test’] ) {
      echo ‘<div class=”sc-content-testContainer”>’;
   } else {
      echo ‘<div class=”sc-content-realContainer”>’;
   }
?>

<!-- this is a PHP block that is evaluated after the page rendering -->
<?php echo “<?php”; ?>
   include “extern_php_code.php”;
   echo render_footer();
<?php  echo “?>”; ?>

<!-- this is again a PHP template part -->
<?php
   echo “</div>”;
   echo “</body></html>”;
?>