PHP Habits

  • IcyDragoon
  • Student
  • Student
  • IcyDragoon
  • Posts: 65

Post 3+ Months Ago

Seven habits for writing secure PHP applications
http://www.ibm.com/developerworks/opensource/library/os-php-secure-apps/index.html

Five good programming habits in PHP
http://www.ibm.com/developerworks/opensource/library/os-php-5goodhabits/

Now comes my part:

When you guys write codes, do you do:
Code: [ Select ]
<?php
  //blah blah php codes
?>
HTML codes
<?php
  //blah blah php codes
?>
HTML codes
<?php
  //blah blah php codes
?>
HTML codes
  1. <?php
  2.   //blah blah php codes
  3. ?>
  4. HTML codes
  5. <?php
  6.   //blah blah php codes
  7. ?>
  8. HTML codes
  9. <?php
  10.   //blah blah php codes
  11. ?>
  12. HTML codes


or do you guys prefer
Code: [ Select ]
<?php
  //blah php codes
  echo <<<HTMLCODES
//html codes
HTMLCODES;
  //blah php codes
  echo <<<HTMLCODES
//html codes
HTMLCODES;
  //blah php codes
  echo <<<HTMLCODES
//html codes
HTMLCODES;
?>
  1. <?php
  2.   //blah php codes
  3.   echo <<<HTMLCODES
  4. //html codes
  5. HTMLCODES;
  6.   //blah php codes
  7.   echo <<<HTMLCODES
  8. //html codes
  9. HTMLCODES;
  10.   //blah php codes
  11.   echo <<<HTMLCODES
  12. //html codes
  13. HTMLCODES;
  14. ?>


I, myself, sometimes use both of them. is there any difference between these two styles?
Is it just a style and habit issue? or the second way is faster?or do you guys do it another way?


PS:since PHP5.3, there is nowdoc that is perfect to suit the HTMLs for the 2nd solution.
PS2: this example here uses heredoc instead of nowdoc, just in case people doesn't recognize the new syntax,nowdoc
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

Neither. I prefer keeping PHP and HTML completely separated unless I'm working with a framework (such as Cake) that would require otherwise.

A common way of separating concerns is to read in an HTML template to memory, inject content into it, then render the whole thing as HTML.
  • IcyDragoon
  • Student
  • Student
  • IcyDragoon
  • Posts: 65

Post 3+ Months Ago

May I ask which HTML template engine do you use?

I might wanna migrate myself to that direction also, after figuring out that it's better to manage contents that way xD
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8397
  • Loc: USA

Post 3+ Months Ago

I used to use SMARTY, until I got phpBB and merged my site into phpBB so then I used the template engine that phpBB has... but now, to test my knowledge I have created my own template engine that works quite well. I put it up in the PHP tutorials section if you want to take a look at it.
  • IcyDragoon
  • Student
  • Student
  • IcyDragoon
  • Posts: 65

Post 3+ Months Ago

I'll just take a tiny miny little peek okay?

hmmm.. let's see...
  • IcyDragoon
  • Student
  • Student
  • IcyDragoon
  • Posts: 65

Post 3+ Months Ago

...That, actually is what I am doing, except without the configuration file...

That is not related to HTML template engine

but the so-called seperate compilation in C++ world...

I guess that is what it is called in PHP world

....I am cool with seperate compilation or the template

seems like a piece of cake ;p
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8397
  • Loc: USA

Post 3+ Months Ago

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

I don't use an template engine in particular. For large projects I use CakePHP which uses an MVC architecture so my presentation code is all contained in views.

For anything else, I usually whip up something similar to this:

Code: [ Select ]
<?php
 
class BasicTemplate implements HTMLTemplate {
 
  private $html = "";
  private $vars = array();
 
  public function __construct__( $filename = null ) {
 
    if( $filename && file_exists(TPL_PATH.$filename) ) {
      $html = file_get_contents(TPL_PATH.$filename);
    }
  }
 
  public function set($name, value) {
 
    $this->vars[$name] = $value;
  }
 
  public function getHtml() {
 
    $retval = $this->html;
    foreach( $vars as $name => $value ) {
      $retval = str_replace($name, $value, $retval);
    }
    return $retval;
  }
 
}
  1. <?php
  2.  
  3. class BasicTemplate implements HTMLTemplate {
  4.  
  5.   private $html = "";
  6.   private $vars = array();
  7.  
  8.   public function __construct__( $filename = null ) {
  9.  
  10.     if( $filename && file_exists(TPL_PATH.$filename) ) {
  11.       $html = file_get_contents(TPL_PATH.$filename);
  12.     }
  13.   }
  14.  
  15.   public function set($name, value) {
  16.  
  17.     $this->vars[$name] = $value;
  18.   }
  19.  
  20.   public function getHtml() {
  21.  
  22.     $retval = $this->html;
  23.     foreach( $vars as $name => $value ) {
  24.       $retval = str_replace($name, $value, $retval);
  25.     }
  26.     return $retval;
  27.   }
  28.  
  29. }


Sometimes I use a magic function for the setter, I guess it just depends on my mood, lol
  • IcyDragoon
  • Student
  • Student
  • IcyDragoon
  • Posts: 65

Post 3+ Months Ago

hehe, I overlook that topic

I am getting what templates are for there..

Maybe I should wait and practice Templates after I have better understanding of PHP :)

hmmm,,,, maybe I should start learning how to make a flexible

template class..That would be great later as one for all solution xD

Post Information

  • Total Posts in this topic: 9 posts
  • Users browsing this forum: No registered users and 73 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.