SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Nov 2004
    Location
    USORA
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how2make {template}

    How to make this {VAR1} where in place of "var1" echo a txt from that variable.
    For exm. if i make script i want to allow someone can change template like he want by puting {some_predefined_var} like {date} or {author} etc.
    if someone don't understand me, i want to make something like in CuteNews templates.

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

  3. #3
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $template file_get_contents('mytemplate.html');
    $template str_replace(array('{VAR1}','{VAR2}'), array($var1,$var2));
    echo 
    $template

  4. #4
    SitePoint Member
    Join Date
    Nov 2004
    Location
    USORA
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In detail and fixed...

    PHP Code:
    $var1="some_txt";
    $var2="some_txt";

    $template file_get_contents('mytemplate.html'); 
    $template str_replace(array('{VAR1}','{VAR2}'), array($var1,$var2),$template);
    echo 
    $template
    anyway tnx.

  5. #5
    SitePoint Member
    Join Date
    Jan 2005
    Location
    nm
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    another way to do this would be using strtr().

    PHP Code:
    $vars = array('{VAR1}' => "sometext"'{VAR2}' => "othertext");
    $template file_get_contents('mytemplate.html'); 
    echo 
    strtr($template$vars); 

  6. #6
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    Regina, SK
    Posts
    318
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One thing I don't particularaly like about some of the templating methods out there is their choice of "tags" to look for in the template to replace. Often they'll use {} or [] which can show up in the web browser if not replaced properly by the contents. Instead I tend to use
    Code:
    <xMyTag/>
    which at least the browser will interpret as an HTML tag and not display.

    It can be as simple as defining what these tags are and then doing a simple str_replace() on the file contents.

    I usually do something like:
    PHP Code:
    // Define Variables To Put In Template
    $today date("F j, Y, g:i a");

    // Get file template contents
    $template file_get_contents('some_template.html');

    // Replace Tags with actual Values
    $template str_replace('<xToday/>',$today,$template);

    echo 
    $template 
    This is how I do my templating. I've looked at things like Smarty and found them to be over complex. This is simple, and if its for someone other than myself, I'll create a simple document that lists the available tags that can be placed in a template and the type of data that will replace the tag.

    In some projects, for example, one of the tags actually represented a table of about 150 rows. Now instead of having PHP do all the html, I start nesting these tags... I'll try and explain it here:

    PHP Code:
    // $data is an array containing the information pulled from a database
    // $template contains the main page layout 

    $formatted_rows '';
    $table_template file_get_contents('table_template.html');
    $row_template file_get_contents('table_row_template.html');
    foreach (
    $data as $row) {
      
    $new_row '';
      
    $new_row str_replace('<xFirstName>',$row['first_name'],$row_template);
      
    $new_row str_repalce('<xLastName>',$row['last_name'],$new_row);
      
    $formatted_rows .= $new_row
    }

    //  So at this point I have $formatted_rows containing the html formatted
    //  rows of all the data I want to display... Something like:
    //
    //                 <tr>
    //                     <td>John</td><td>Smith</td>
    //                 </tr>
    //                 <tr>
    //                      <td>Jane</td><td>Doe</td>
    //                  </tr>
    //  Now we need to take these rows and drop them into the table template
    //  and then drop the table template into the main template and display the
    //  results

    $table str_replace('<xRows/>',$formatted_rows,$table_template);
    $template str_replace('<xTable/>',$table,$template);
    echo 
    $template 

    Hope that makes sense.


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
  •