![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
![]() |

|
| Programming tutorials All Knowledge Info and links to posted here |
![]() |
|
Basic Smarty, Small explanation about Smarty
|
LinkBack | Thread Tools | Display Modes |
|
|
#1 (permalink) |
|
Administrator
Posts: 876
Join Date: Oct 2005
Rep Power: 10
IM:
|
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
<!--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:
Code:
$smarty->assign(array('name'=>$name,
'age'=>$age,
'other'=>$other'));
<!--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>
|
|
|
|
|
|
|
|
|
#2 (permalink) |
|
Administrator
Posts: 289
Join Date: Jan 2006
Rep Power: 10
IM:
|
NOT: Code:
$host = localhost; $user = <USERNAME>; $pass = ; $daba = database; Code:
$host = "localhost"; $user = ""<USERNAME>; $pass = ""; $daba = "database"; ------------------ 2oo7 |
|
|
|
|
|
#5 (permalink) |
|
Fix my Error!
Posts: 1
Join Date: Dec 2006
Rep Power: 0
IM:
|
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 |
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |