text: User Tag List
  Results 1 to 1 of 1
  - Join Date
  - Jan 2008
  - Location
  - Knoxville TN
  - 2,011
  - Mentioned
  - 56 Post(s)
  - Tagged
  - 0 Thread(s)
  Tutorial - PHP Templating - Braceless Syntax and Short Tags
  This is a short tutorial on using braceless syntax and PHP short tags to produce elegant and easily readable PHP template files, as well as a short intro on how to use such templates in an MVC situation without relying on template engines such as patTemplates or Smarty. Only entry level PHP experience is expected here, nothing major.
  First, on Short Tags. PHP short tags are an optional part of the PHP engine that can be turned on or off in the PHP.ini file. For this reason most all PHP based software products don't use them. I do for the following reason - it is impossible to block access to short tags while blocking access to mod_rewrite. If you have the latter you will have the former. Short tags can be turned on in an .htaccess file with the first of the three commands below:
  Code:
  # A recommended series of php settings. php_flag short_open_tag on php_flag register_globals off php_flag magic_quotes_gpc off
  The short tags themselves look like this
  PHP Code:
  <html>
  <body>
  <?php echo 'this is a snippet of php code in the familiar tags' ?>
  <script type="php"> echo 'this snippet is in the rarely if ever used script tag (usually used for javascript only)' </script>
  <? echo 'This is a php short tag #1' ?>
  <?= 'This string will be echoed out too - short tag #2 for echoing stuff' ?>
  </body>
  </html>
  Braceless syntax works by putting a colon after the condition and an ending statement to close the group. They look like this
  PHP Code:
  <!-- A braceless if statement block -->
  <? if ( $a == $b): ?>
  <p><?= $a ?> equals <?= $b ?></p>
  <? elseif ( $a > $b): ?>
  <p><?= $a ?> is greater than <?= $b ?></p>
  <? else: ?>
  <p><?= $a ?> is less than <?= $b ?></p>
  <? endif ?>
  <!-- A braceless for in block -->
  <ul>
  <? for($a = 1; $a < 11; $a++): ?>
  <li>#<?= $a ?></li>
  <? endfor ?>
  </ul>
  PHP Code:
  <? include($this->findTemplate('exceptionPageStart')) ?>
  <div id="page">
  <? if( PAMWF_DEBUG ): ?>
  <p style="font-weight: bold;"><?= $title ?> in file <?= $file ?> at line <?= $line ?>.</p>
  <h4>Message</h4>
  <p style="font-style: italic;"><?= $message ?></p>
  <h4>Stack Trace</h4>
  <p><?= $trace ?></p>
  <h4>Included Files</h4>
  <p><?= $includes ?></p>
  <? else: // Don't be chatty in production mode - just inform the user an error occured. ?>
  <p>We're sorry, but an internal server error occured. A log has been made
  of this error and the administrators have been notified.</p>
  <? endif; ?>
  </div>
  <? include($this->findTemplate('exceptionPageEnd')) ?>
  Now, stick around this forum long enough and you'll see talk of the MVC design pattern and the importance of separating view from control and model. It is to this end that template engines in PHP were written, but those engines were written in defiance of the simple fact that PHP is itself a template engine. Even the lightest ones add a huge amount of overhead to the site code. Separation of logic is, in my opinion, a matter of programmer self discipline.
  PHP templates can be easily isolated off from the rest of your PHP code's variables by including them into a function like so
  PHP Code:
  <?php
  function parseTemplate( $template ) {
  include( $template );
  }
  ?>
  PHP Code:
  <?php
  function parseTemplate( $template, $output ) {
  extract($output);
  include( $template );
  }
  ?>
  PHP Code:
  <?php
  function parseTemplate( $template, $output ) {
  extract($output);
  ob_start();
  include( $template );
  return ob_get_clean();
  }
  ?>
  Well, that's about it. Comments and discussion welcome and I'll answer what questions I can. Information provided in the hopes it will be useful and all that.
  Bookmarks