Jump to content

Beginning with the Smarty Template Engine

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
17 replies to this topic

#1
Affix

Affix

    Learning Programmer

  • Members
  • PipPipPip
  • 47 posts
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.


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

<?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.


<?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.


<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 :D

any questions email me : affix@fedoraproject.org

#2
Guest_Jordan_*

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

#3
Affix

Affix

    Learning Programmer

  • Members
  • PipPipPip
  • 47 posts
Glad you like :)

#4
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
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!


#5
Affix

Affix

    Learning Programmer

  • Members
  • PipPipPip
  • 47 posts
I write my own frameworks usually

#6
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Then is it really a framework?

#7
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts

Jordan said:

Then is it really a framework?

Certainly, over time people who work frequently in one language usually end up making their own.

Quote

Fedora Developer

Oh? Whats your focus as a Fedora Developer?

Nice tut.

#8
Affix

Affix

    Learning Programmer

  • Members
  • PipPipPip
  • 47 posts

TkTech said:

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

#9
Guest_Jordan_*

Guest_Jordan_*
  • Guests

TkTech said:

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.

#10
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts

Quote

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.

#11
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
Do you distribute your frameworks?
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!


#12
Affix

Affix

    Learning Programmer

  • Members
  • PipPipPip
  • 47 posts
Not really

My Open Source forum Im gonna distribute the framework of that :)