SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    My precious!!! astericks's Avatar
    Join Date
    Mar 2002
    Location
    Vancouver, BC
    Posts
    1,971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Templating - what am I doing wrong

    Hi guys, I was bored, so I've been trying to do something with templates...however, I cant get eregi_replace to work properly.

    Here's a simple version of it [dont mind the syntax etc]:

    PHP Code:
     class template{
         
         var 
    $html;
         
         function 
    get_tpl($template) {
              
    $this->html .= join(''file($template));
         }
         
         function 
    replace($rep = array()) {
             foreach (
    $rep as $tag => $info) {
                     
    $this->html eregi_replace('{' $tag '}'$info,  $this->html);
             }
         }
         
         function 
    output() {
             print 
    $this->html;
         }
     } 
    This is my template [header.tpl, for e.g]:
    HTML Code:
     <table>
       <tr>
     	<td>{$top}</td>
      	<td>{$middle}</td>
      	<td>{$bottom}</td>
       </tr>
     </table>
    And this is my index.php
    PHP Code:
     $vars = array(
     
    'top' => 'Something top',
     
    'middle' => 'Middle part',
     
    'bottom' => 'Some footer'
     
    );
     
     
    $tpl = new template;
     
    $tpl->get_tpl('header.tpl');
     
    $tpl->replace($vars);
     
    $tpl->output(); 
    Upon running the page, the HTML source is still showing the {$tag} instead of eregi_replace taking care of the replacement.

    Please let me know what am I doing wrong here.

    cheers.
    asT.

  2. #2
    get into it! bigduke's Avatar
    Join Date
    May 2004
    Location
    Australia
    Posts
    847
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    might as well use str_replace instead since you know specifically what string needs to be replaced. Thats what I did in my template class.

  3. #3
    My precious!!! astericks's Avatar
    Join Date
    Mar 2002
    Location
    Vancouver, BC
    Posts
    1,971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That doesnt seem to work either...this is pretty frustrating, and I thought it was gonna be fun.

    Any other ideas? Or point me to a template class example. Just tried a search on this forum and google..and they have not been too generous so far.

  4. #4
    SitePoint Zealot cholmon's Avatar
    Join Date
    Mar 2004
    Location
    SC
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by astericks
    That doesnt seem to work either...this is pretty frustrating, and I thought it was gonna be fun.

    Any other ideas? Or point me to a template class example. Just tried a search on this forum and google..and they have not been too generous so far.

    Make sure you stick the $ in the string to be replaced. Right now, it's trying to find {top} instead of {$top}:
    PHP Code:
    $this->html str_replace('{$' $tag '}'$info$this->html); 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •