Error » Certification & Programming center Error !! » Programming tutorials » Basic Smarty, Small explanation about Smarty

Programming tutorials All Knowledge Info and links to posted here

Post New Thread Reply
  Basic Smarty, Small explanation about Smarty
LinkBack Thread Tools Display Modes
Old 27-Oct-2006, 01:07 AM   #1 (permalink)
Administrator
 
Admin's Avatar

Posts: 876
Join Date: Oct 2005
Rep Power: 10 Admin has disabled reputation

IM:
Default Basic Smarty, Small explanation about Smarty

Let's start
im gonna try to explain some basics for using Smarty
i just started using it and i figured out it's really easy to use, my PHP skills aint that great either, but i'll try to keep it kinda usable

First the structure

<!--c1-->

<!--ec1-->
Code:
When you downloaded the Smarty package, you got a folder inside the zip file called Libs,
you need to extract it to your absolute home path, so let's say
/usr/home/smarty/libs for example on unix
c:\program files\webserver\Smarty\libs\ on windows

after you got that this will be the structure inside your folder which contains your website

/www/sitename/templates/configs/ --> Not yet used it but i guess it's used for some configs from Smarty
/www/sitename/templates/cache/ --> Here it will remember the pages the users visited (kind of cookies)
/www/sitename/templates/templates/ --> Your template files
/www/sitename/templates/templates_c/ --> In this dir your files will be compiled



Now we're gonna call Smarty (You can also set it in your INI, but i don't use that)
Create a new file, like for example config.php



//Config

define('SMARTY_DIR', 'c:/program files/webserver/smarty/libs/');
define('TEMPLATE_DIR', 'c:/program files/webserver/www/sitename/skins');

// Mysql settings here
$host = localhost;
$user = <username>;
$pass = <pass>;
$daba = database;

mysql_connect($host,$user,$pass);
mysql_select_db($daba);



Save this some where you want..
Create another file, let's say index.php
<!--c1-->
<!--ec1-->
Code:
// Calling config.php
require_once(config.php);
// Here we gonna call smarty
require_once(SMARTY_DIR . "Smarty.class.php"); // Take a notice of the caps in Smarty

// Assign the basic settings to smarty
$smarty = new Smarty(); // Again watch the uppercase S

// Tell smarty the folder it needs to use
$smarty->template_dir = TEMPLATE_DIR . '/templates/';
$smarty->compile_dir = TEMPLATE_DIR . '/templates_c/';
$smarty->config_dir = TEMPLATE_DIR . '/config/';
$smarty->cache_dir = TEMPLATE_DIR . '/cache/';

// Making a database connection to get some info
$info = mysql_query("SELECT * FROM info");

// Putting everything in an array, using assoc
while ($row = mysql_fetch_assoc($info)) {
        $info[] = array('id'=>$row['id'],
                               'name'=>$row['name'],
                               'age'=>$row['age'],
                               'hobby'=>$row['hobby']);
}

// Here we're gonna detect if there are some rows in the query
if (mysql_num_rows($info) < 1){
        $smarty->assign('error', 'No entries'); // If there are no rows, tell the template tot show this
} else {
        $smarty->assign('info', $info); // Otherwise, show the info in the array
}
$smarty->display('index.tpl'); // here we will display the template



You can also use arrays inside the assign party if you got more info
for example:
<!--c1-->
Code:

$smarty->assign(array('name'=>$name,
                                   'age'=>$age,
                                   'other'=>$other'));
From now on we need to create template, it will be very short and simple

<!--c1-->

<!--ec1-->
Code:
{* Index.tpl *} <!-- Smarty comments, won't be shown in the source -->
<html>
<head>
<title>Information</title>
</head>

<body>
<div>
{* check if there are any entries *}
{if isset($info)}
{foreach item=info from=$info} {* This will loop through the $info array we made *}
      <span><b>Name:</b>{$info.name}</span><br />
      <span><b>Age:</br>{$info.age}</span><br />
      <span><b>Hobbys:</b>{$info.hobby}</span>
{/foreach}
{else}
      <span>{$error}</span>
{/if}
</div>
</body>
</html>
Admin is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
   


   
Old 30-Oct-2006, 10:38 AM   #2 (permalink)
Administrator
 
tijnema's Avatar

Posts: 289
Join Date: Jan 2006
Rep Power: 10 tijnema is on a distinguished road

IM:
Default Re: Basic Smarty, Small explanation about Smarty

hmm, nice script, but it's better to use " " when you define variables for example:
NOT:

Code:
$host = localhost;
$user = <USERNAME>;
$pass = ;
$daba = database;
BUT:

Code:
$host = "localhost";
$user = ""<USERNAME>;
$pass = "";
$daba = "database";


------------------
2oo7
tijnema is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 04-Dec-2006, 04:12 AM   #3 (permalink)
Fix my Error!
 
gtrchrdie's Avatar

Posts: 1
Join Date: Dec 2006
Rep Power: 0 gtrchrdie is on a distinguished road

IM:
Post Microsoft

No Spamming here idiot

Last edited by Admin; 04-Dec-2006 at 07:40 AM.
gtrchrdie is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 04-Dec-2006, 08:38 PM   #4 (permalink)
Fix my Error!
 
somebady's Avatar

Posts: 1
Join Date: Dec 2006
Rep Power: 0 somebady is on a distinguished road

IM:
Post No Spaming here

No Spaming here

Last edited by Admin; 04-Dec-2006 at 10:35 PM.
somebady is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 15-Dec-2006, 07:51 AM   #5 (permalink)
Fix my Error!
 
WickedGirl's Avatar

Posts: 1
Join Date: Dec 2006
Rep Power: 0 WickedGirl is on a distinguished road

IM:
Post Heyo, im new

Heyo out der, im new in www.errorforum.com .... well, i didnt know i should've come first over here n introduce myself to u ppl but here i am, a 21 years old Female, committed hehe, oh shoot u all gonna avoid me now hehe, nah I know u ppl aint gonna trash me right from da beginning im into makin fun, playin basketball, travellin, movies n music.. a chatterbox??? well...... maybe hehehe, nah aint dat much I hope I'd be able to drag many funny things for ya'll !! keep it real n thx for welcomin me in yr house as a long-lasting member Peace ya'll _________________ earrings diamond
WickedGirl is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 16-Dec-2006, 06:44 PM   #6 (permalink)
Administrator
 
Anilrgowda's Avatar

Posts: 18,715
Join Date: Jan 2006
Rep Power: 10 Anilrgowda is on a distinguished road

IM:
Default Re: Basic Smarty, Small explanation about Smarty

welcome to error forum
Anilrgowda is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Post New Thread Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Small Soldiers SEO Game Cheats 0 02-Apr-2008 09:31 PM
Small Optimization Game Cheats 0 02-Apr-2008 09:31 PM
Basic And Dynamic Disks In Vista (Part II)Vista supports both basic and dynamic disks Jokes Microsoft windows vista error 0 19-Apr-2007 11:27 PM
Where do I get an explanation for a particular registry setting? ps3cheats Microsoft Windows xp error 1 12-Mar-2007 02:02 AM
What to do with small i.e <40 GB C:\ drives Admin Microsoft Operating Systems Error 0 29-Nov-2006 03:44 AM


All times are GMT -8. The time now is 10:37 AM.

Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0

DMCA Policy

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166