Search Optimization
  Home arrow Search Optimization arrow Page 4 - Working 'Smarter' with Templates
SEO Chat Forums  
Choosing Keywords  
Google Optimization  
Link Trading  
MSN Optimization  
Search Engine News  
Search Engine Spiders  
Search Optimization  
Web Directories  
Website Marketing  
Website Promotion  
Website Submission  
Yahoo Optimization  
SEO Tools
Adsense Calculator
AdSense Preview
Advanced Meta-Tags
Alexa Rank Tool
Check Server Headers
Class C Checker
Code to Text Ratio
CPM Calculator
Domain Age Check
Domain Typos
Future PageRank
Google Dance
Google Keywords
Google Search
Google Suggest
Google vs Yahoo
Indexed Pages
Keyword Cloud
Keyword Density
Keyword Difficulty
Keyword Optimizer
Keyword Position
Keyword Typos
Link Popularity
Link Price Calculator
Meta Analyzer
Meta Tag Generator
Multiple Link Popularity
Page Comparison
Page Size
PageRank Lookup
PageRank Search
Robots.txt Generator
ROI Calculator 
S.E. Comparison 
S.E. Keyword Position 
Site Link Analyzer 
Spider Simulator 
URL Redirect Check 
URL Rewriting 
Mobile Linux 
APP Generation ROI 
IBM® developerWorks 
Sun Developer Network 
SEO Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
SEARCH OPTIMIZATION

Working 'Smarter' with Templates
By: Bill Sterzenbach
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    2004-08-23

    Table of Contents:
  • Working 'Smarter' with Templates
  • Selecting a Template Engine
  • A Typical Dynamic Web Page without Templates
  • Putting Smarty to Work - A Dynamic Template-driven Web Page
  • Places Where Smarty Shines
  • Common Mistakes with Smarty

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Working 'Smarter' with Templates - Putting Smarty to Work - A Dynamic Template-driven Web Page


    (Page 4 of 6 )

    The concept of templates can be hard to grasp at first so sometimes it's best to just dig right into the code. Assume that you have a simple script that displays the IP address of the visitor. There are a couple of ways to display this information, but in this example we will use a template via Smarty. Let's take a look at this typical use for a Smarty template page. Note that this example requires that Smarty is installed on your server. This process does not require compiling – simply unzip the smarty files to a location of your choice.

    // ShowIP.php
    // Include the necessary Smarty files
    include("/path/to/Smarty/libs/Smarty.class.php");
    //Create the Smarty object
    $smarty = new Smarty();
    //Set the compile check to true
    $smarty->compile_check = true;
    //Choose your delimiter types
    $smarty->left_delimiter = "{{";
    $smarty->right_delimiter = "}}";
    //Set your directories
    $smarty->template_dir = "/path/to/Smarty/templates/";
    $smarty->compile_dir = '/path/to/Smarty/templates_c/';
    $smarty->config_dir = '/path/to/Smarty/configs/';
    $smarty->cache_dir = '/path/to/Smarty/cache/';
    // Assign a smarty variable with a value
    $smarty->assign('VisitorIP', $_SERVER['REMOTE_ADDR']);
    // Parse and display the template
    $smarty->display("showiptemplate.htm");
    ?>

    Note the total lack of HTML in the above example. This is a beautiful thing for creating sites that can be easily re-skinned. I know that this is a very basic example, but believe me when I say it scales wonderfully to the most complex of web pages with no trouble. The real trick with Smarty is resolving to never mix your code and HTML. You will be tempted to put “just a little” html in your php scripts at times, but in almost every case, you'll find yourself getting back in there later and ripping the code out. You'll only truly leverage the power of Smarty when you totally buy in to this separation principle.

    Below is the template file that the script above would load and display:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Show IP</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    <a target="_Blank" class="linktext" href="http://{{$VisitorIP}}">Click Here to view server</a>
    </body>
    </html>

    More Search Optimization Articles
    More By Bill Sterzenbach


     

    SEARCH OPTIMIZATION ARTICLES

    - SEO Tricks That Will Lower Your Rankings
    - Building Search Engine Tag Trails
    - Blocking Complicated URLs with Robots.txt
    - Is Your Web Content Accessible?
    - Links and More SEO Tips for Beginners
    - Ten SEO Guidelines
    - SEO Overview and Tips for Beginners
    - Stumbling Blocks to Web Site Success
    - Web Pages to Include in Your Site
    - Big Sites Don`t Automatically Rule Search En...
    - You Need More Than One Site Map
    - The Whys and Hows of Video Search Optimizati...
    - An SEO`s Experience: 21 Rules for Performing...
    - An SEO Eyeful: Interview with Ronald Herskow...
    - Research Your Competition for SEO





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT