SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jun 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Template Class Function Help

    I am totally new to php and I have racked my brain on this for about a week. Any help or even a nudge in the right direction would be of great help. So basically, in a nutshell, I am designing a small cms for my clients and I would like my little cms to do variable replacement. I am assuming the only way to do this is with a template class, but I am not sure. The template class example that I used was found here, http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=1433&lngWId=8

    I like the idea of separating the design from the code, but my main issue is I would like to enter a "tag" such as, #photoalbum#, into a page in the cms editor and then associate #photoalbum# with an include file that parses the code out to the page. I have successfully done that on part of the page, (design.html), but I have been unsuccessful in writing a function that will take place of the current page that is detecting the id of the page and printing the content (page.php).

    Currently inside the cms I have a "Contact Us" page that has a #hello# tag in the content. In the index.php page I have $template->replace("hello", "Hello Wes"); that I was using for testing to see if I could at least just get "Hello Wes" to appear in the browser window. Right now, that isn't happening.

    Below is the code for the pages.

    Files that I am using to complete the template class.

    index.php - This file is where the includes are located and where the array is defined.

    PHP Code:
    <?php
    include "vars.php";

    include 
    "template.class.php";

    $template = new Template;
    $template->load("design.html");
    $template->replace("title""$title");
    $template->replace("keywords""$keywords");
    $template->replace("description""$description");   
    $template->replace("hello""Hello Wes");
    $template->publish();

    ?>
    page.php - This file is an include file that is included in design.html that detects the id that is associated with the url and then pull's the correct content from the database. Currently this file is not being parsed by the template class, (template.class.php) so, #hello#, that is entered on the Contact Us page is showing up as #hello#, instead of it being parsed and showing, "Hello Wes" as defined on the index.php page.

    PHP Code:
    <?php
    $id 
    $_GET['id'];

    if (
    $id == '') {
        
    $query "SELECT * from content WHERE id = $startpage";
        }
        elseif (
    is_numeric($id)) {
            
    $query "SELECT * from content WHERE id = $id";
        }
        else {
            
    $query "SELECT * from content WHERE pagename = '$id'";

    }
    $error mysql_error();

    if (!
    $result mysql_query($query)) {
        print 
    "$error";
        exit;
    }

    while(
    $row mysql_fetch_object($result)){
        
    $content $row->text;
         print(
    "$content");
    }

    ?>
    design.html - This is the file that has the template of the website. Currently there are includes in this file, nav.php and page.php, but I would rather have them replaced with #nav# and #page# so that the code and the content is completely separated. I tried to define those on the index.php, but was unsuccessful doing a replacement. I even tried to create, $nav = include 'nav.php'; and then define it, replace("nav", "$nav") in the index.php file and that didn't work. It inserted the code outside of the template.

    Code:
    <html>
    <head>
    
    <title>#title#</title>
    <meta name="keywords" content="#keywords#">
    <meta name="description" content="#description#">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link href="style.css" rel="stylesheet" type="text/css" />
    </head>
    <body id="mainBody">
    <div id="wrap">
    	<div id="header"><img src="Image/hdr_skoglinhomes.jpg" alt="Welcome To SkoglinHomes.com" width="725" height="56" longdesc="http://www.skoglinhomes.com" /></div>
    	<div id="sidebar">
    		<?php include('nav.php'); ?>
    		<div id="joinnewsletter"><img src="Image/hdr_joinnewsletter.gif" alt="Join The Tim Skoglin Newsletter" longdesc="http://www.tswinternet.com" /><p>Welcome to the Join Newsletter area.</p></div>
    		<div id="latestnews"><img src="Image/hdr_latestnews.gif" alt="Check Out Tim Skoglin's Latest News" longdesc="http://www.timskoglin.com" /><p>Welcome to the latest news area</p></div>
    		<div id="mylogo"><img src="Image/logo_myrealestate.jpg" alt="My Real Estate" longdesc="http://www.timskoglin.com" /></div>
    	</div>
    	<div>
    <?php include('page.php'); ?>
       </div>
    </div>
    </body>
    </html>
    template.class.php - This is where all the functions are defined. I thought that maybe I would just have to define a function for #page# where it would basically run the code on page.php as a function, but that didn't work either.

    PHP Code:
    <?
    class Template {
       var 
    $template;
       function 
    load($filepath) {
          
    $this->template file_get_contents($filepath);
       }
       function 
    replace($var$content) {
          
    $this->template str_replace("#$var#"$content$this->template);
       }
       function 
    publish() {
          eval(
    "?>".$this->template."<?");
       }
    }
    ?>
    So there you have it. I REALLY would appreciate any help. Thanks!

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use output buffering to capture the output generated by the files you want to include.


    PHP Code:

    ob_start
    ();
    include 
    'page.php';
    $page_output ob_get_clean();


    ob_start();
    include 
    'nav.php';
    $nav_output ob_get_clean();

    $template->replace("page"$page_output);
    $template->replace("nav"$nav_output); 

    other options:
    in the included files, dont output anything to the browser. instead of echo, just build a variable to store the output. this is essentially output buffering done by hand.

    you can use return statements in a file which is included, kinda like as if it were a function.

    foo.php
    PHP Code:
    $var $_GET['var'];
    return 
    "$var and some more foo"

    index.php
    PHP Code:
    $return_value_of_foo = include 'foo.php'

  3. #3
    SitePoint Member
    Join Date
    Jun 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Variable Replacement

    Thanks SO much Clamcrusher for the include advice. I am going to try that now. Does anyone know the function that I can use to parse a variable, #photoalbum#, that is stored in $content from a mysql database? I know that I can do it though the template class, I just don't know how to write a function that will recognize #photoalbum# and then put an include file in it's place. Thanks for the help guys!

  4. #4
    SitePoint Member
    Join Date
    Jun 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    okay, that was it!

    Yeah!!! That did the trick! Wow, I never imagined that it would be so simple. That handles the includes and also does the variable replace of #variable# that get's sent from the database.

    You guys don't even know the pages of code I was going through to see how to make that happen. Clamcrusher, that really helped. I hope I will be able to contribute to someones php problems sometime.


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
  •