Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 6 of 6

Thread: Template Engine

  1. #1
    Senior Member leprechaun13's Avatar
    Join Date
    May 2005
    Location
    Northampton
    Posts
    487
    Member #
    10058
    Can anyone point me in the direction of a template engine similar to the one used in cube cart.

    Can someone also give me a brief tutorial on how to open,edit and write to files via php as I cant find any tutorials that make it simple enough.
    Regards Phil,


  2.  

  3. #2
    Senior Member visualAd's Avatar
    Join Date
    Jan 2003
    Location
    Slough, UK
    Posts
    201
    Member #
    434
    Template engine: http://smarty.php.net/

    File handling is already very simple in PHP:

    http://uk.php.net/filesystem

    The easiest functions to use are file_get_contents() and file_put_contents().

  4. #3
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    Smarty is popular. But, if you're ambitious, cough:

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" xmlns:wdf="http://www.webdesignforums.net/">
        <head>
            <title>Web Design Forums.net: Log In</title>
            
            <wdf:include template="head.xml" />
            <wdf:baseHref />
        </head>
        
        <body>
            <h1>Log into WDF v6</h1>
    		
    		<wdf:onEventError>
    			<p class="event-error"><wdf:eventErrorMessage /></p>
    		</wdf:onEventError>
    		
            <wdf:form event="login">
                <dl>
                    <dt>Username:</dt>
                    <dd>
                        <wdf:eventField name="username" />
                        <wdf:onFieldError name="username">
                            <p class="field-validation-error"><wdf:fieldErrorMessage /></p>
                        </wdf:onFieldError>
                    </dd>
                    
                    <dt>Password:</dt>
                    <dd>
                        <wdf:eventField name="password_hash" />
                        <wdf:onFieldError name="password_hash">
                            <p class="field-validation-error"><wdf:fieldErrorMessage /></p>
                        </wdf:onFieldError>
                    </dd>
                </dl>
                
                <div><wdf:submitButton title="Log In" /></div>
            </wdf:form>
            
            <p>Completed in <wdf:appStopwatch /> sec (<wdf:databaseStatus property="count" /> queries in <wdf:databaseStatus property="time" /> sec)</p>
        </body>
    </html>
    Who said that!?
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  5. #4
    Senior Member leprechaun13's Avatar
    Join Date
    May 2005
    Location
    Northampton
    Posts
    487
    Member #
    10058
    filburt thats just gone way over my head and I could never work smarty out so i think ill leave that well alone for now
    Regards Phil,


  6. #5
    Senior Member visualAd's Avatar
    Join Date
    Jan 2003
    Location
    Slough, UK
    Posts
    201
    Member #
    434
    Filburt is suggesting you make your own template engine or maybe use the WDF one . This can be quite simple, if you want something quite simple that is . Simply do the following:
    1. Create a class called Template, with a simple method called display()
      PHP Code:
      <?php
      /* template.php */
      class Template
      {
          
      // somewhere to hold the template variables
          
      private $tplVars = array();

          public function 
      display($file)
          {
              include(
      $file);
          }

          
      /* this will be called for each property set i.e: $this->propertyName = 'value'; */
          
      public function __set($name$value)
          {
              
      $this->tplvars[$name] = $value;        
          }

          
      /* this will be called for every property retrieved i.e: echo($this->propertyName) */
          
      public function __get($name)
          {
              if (isset(
      $this->tplVars[$name])) {
                  return 
      $this->tplVars[$name];
              } else {
                  return 
      null;
              }
          }
      }
      ?>
    2. Create your template. You should use PHP's alternative construct syntax here (it looks nicer ):
      PHP Code:
      <?php /* fruits.php - template file */ ?>
      <html>
          <head>
          </head>

          <body>
              <h1>Hello <?php echo($this->name?></hi>

              <?php if ($this->age >= 18): ?>
                  <p>you are an adult </p>
              <?php endif; ?>

              <h3>You like these fruits: </h3>
              <ul>       
                  <?php foreach($this->fruits as $fruit): ?>
                      <li><?php echo($fruit?></li>        
                  <?php endforeach; ?>
              </ul>
          </body>
      </html>
    3. Now create the file that uses the template:
      PHP Code:
      <?php
      require _once 'template.php';

      $template = new Teamplate;

      // assign some variables
      $template->name 'Adam';
      $template->age 23;
      $template->fruits = array('Apples''Grapes''Melons');

      // now display the appropriate template file
      $template->display('fruits.php');
      ?>

    Of course, you can expand this to include more complex functions. I do however prefer something with an XML like syntax. The JSTL specification for JSP seems to have it right, fully extensible via the use of tag libraries.

  7. #6
    Senior Member leprechaun13's Avatar
    Join Date
    May 2005
    Location
    Northampton
    Posts
    487
    Member #
    10058
    ahh thanks will try that when i get a spare few mins. Sorry for the late reply been unplugged for a few days
    Regards Phil,



Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 03:57 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com