+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Beginning with the Smarty Template Engine

  1. #1
    Affix is offline Learning Programmer
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    47
    Blog Entries
    2
    Rep Power
    12

    Beginning with the Smarty Template Engine

    Smarty is a PHP Template engine that will allow you to create a PHP Backend without using full HTML within your PHP Files. Smarty will use a cache directory to store the template cache for each session and clear it once it reaches a certain age.

    Smarty has build in loop functions to minimise the use of PHP Code from within your template documents. In this tutorial I am going to show you how to create a simple page To output an array using a loop in Smarty. The array will be outputted in a simple HTML Table to show how smarty works.

    Ok So we need the smarty Libs to do this you can retreive the latest smarty distribution from Smarty : Template Engine

    Once you have downloaded it Upload it onto your web server or put it in your WAMPP/LAMPP htdocs directory.

    On your server create 2 new folders with the following configuration.

    Code:
    Dir Name                      CHMOD
    -----------------------------------
    html                            665
    cache                         777
    Ok now create a new file and call it config.php and place the follwoing code into the file

    Code:
    <?php
      $dir 
    dirname(__FILE__); //Just to get the directory we are in

      
    define ("SMARTY_DIR""<WHERE YOU STORE SMARTY>"); //Use an Absolute path (/home/<user>/htdocs/smarty on linux)

      
    require_once (SMARTY_DIR "Smarty.class.php");
      
    $smarty = new Smarty;
      
    $smarty->compile_dir "$dir/cache";
      
    $smarty->template_dir "$dir/html";
    ?>
    Now we have our basic smarty config setup we are ready to use smarty to output some data.

    Create a new file call it index.php this is where our aarray is gonna be and It will call out template file.

    Code:
    <?php
      
    require_once ("./config.php");
      
    $phpGods = array("Affix","Jordan","CodeCall"); 
      
    $smarty->assign("gods"$phpGods); //Here we assign the Array to a smarty var
      
    $smarty->display ("array.tpl"); //This will display the file array.tpl from our html directory
    ?>
    Now in the HTML Direcotry we need to create our array.tpl file. To do this make a new HTML file and save it as array.tpl in the html directory.
    In this file use the following code.

    HTML Code:
    <h1>PHP Gurus</h1>
    <div align="center"><table width="50%" height="50%>
      {section name=i loop=$gods}
          <tr>
            <td>{$gods[i]}</td>
         <tr>
      {/section}
    </table></div>
    Now we have this done Upload your script and test it.

    You will notice the loop I created using smarty. This is basically a for each loop from PHP.

    Now you know the basics of smartyPHP hope you enjoyed my tutorial and will stop including HTML in your PHP Scripts

    any questions email me : affix@fedoraproject.org

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Beginning with the Smarty Template Engine

    Excellent tutorial, +rep! I've been meaning to give Smarty a try.

  4. #3
    Affix is offline Learning Programmer
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    47
    Blog Entries
    2
    Rep Power
    12

    Re: Beginning with the Smarty Template Engine

    Glad you like

  5. #4
    Join Date
    Sep 2008
    Location
    Australia
    Posts
    4,834
    Blog Entries
    10
    Rep Power
    51

    Re: Beginning with the Smarty Template Engine

    Good work mate +rep

    What PHP framework do you use most commonly?
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

  6. #5
    Affix is offline Learning Programmer
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    47
    Blog Entries
    2
    Rep Power
    12

    Re: Beginning with the Smarty Template Engine

    I write my own frameworks usually

  7. #6
    Jordan Guest

    Re: Beginning with the Smarty Template Engine

    Then is it really a framework?

  8. #7
    TkTech's Avatar
    TkTech is offline The Crazy One
    Join Date
    Jun 2006
    Location
    Canada
    Posts
    1,412
    Blog Entries
    1
    Rep Power
    31

    Re: Beginning with the Smarty Template Engine

    Quote Originally Posted by Jordan View Post
    Then is it really a framework?
    Certainly, over time people who work frequently in one language usually end up making their own.

    Fedora Developer
    Oh? Whats your focus as a Fedora Developer?

    Nice tut.

  9. #8
    Affix is offline Learning Programmer
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    47
    Blog Entries
    2
    Rep Power
    12

    Re: Beginning with the Smarty Template Engine

    Quote Originally Posted by TkTech View Post
    Certainly, over time people who work frequently in one language usually end up making their own.



    Oh? Whats your focus as a Fedora Developer?

    Nice tut.
    I do application and kernel development. Im working on my Own Distro atm

  10. #9
    Jordan Guest

    Re: Beginning with the Smarty Template Engine

    Quote Originally Posted by TkTech View Post
    Certainly, over time people who work frequently in one language usually end up making their own.
    I disagree. Just because someone makes 3-4 classes and includes them in several projects, this does not constitute a framework. A framework is much more than the ability to do something - it also consists of a complete methodology.

  11. #10
    TkTech's Avatar
    TkTech is offline The Crazy One
    Join Date
    Jun 2006
    Location
    Canada
    Posts
    1,412
    Blog Entries
    1
    Rep Power
    31

    Re: Beginning with the Smarty Template Engine

    I do application and kernel development. Im working on my Own Distro atm
    Ever hang around @ #osdev on freenode? I'm in there 24/7 if you stop by.

    Jordan, I'll make a nice long post for you when I get home.

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. XSLT, PHP native or PHP template engine
    By pierrehs in forum PHP Development
    Replies: 0
    Last Post: 04-30-2011, 03:39 PM
  2. Intermediate Python on Google App Engine: Creating blog engine. Part 2
    By Vladimir in forum Python Tutorials
    Replies: 0
    Last Post: 11-28-2010, 02:44 PM
  3. Replies: 6
    Last Post: 01-24-2009, 11:57 AM
  4. Anybody use SMARTY template system?
    By Dan in forum Website Design
    Replies: 7
    Last Post: 07-26-2006, 09:25 PM

Tags for this Thread

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