|
|
|
View previous topic
::
View next topic
|
| Author |
Message |
ramon fincken Site's programmer
Joined: 03 Aug 2007 Posts: 93 Location: Amsterdam, the Netherlands/Europe
|
Posted: Mon Mar 31, 2008 1:03 pm Post subject: [tutorial] Smarty crash course for dummies :) |
|
|
Smarty crash course for dummies
INIT
Lets start right away, I presume you allready have smarty running + included this file uppon your php pages:
| Code: | smartyinclude.php
<?php
/**
* Created on 26-jan-2008 -- 17:52:35
* smartyinclude.php
*
* @author Ramon Fincken, WebsiteFreelancers.nl RamonFincken.com
*/
require 'smarty/Smarty.class.php';
$smarty = new Smarty;
$smarty->compile_check = true;
function show_template()
{
global $smarty, $template;
$smarty->display($template);
}
?> |
PHP CODE, EASY PART
| Code: | index.php
<?php
include('smartyinclude.php');
$template = 'index.tpl';
// Repeat for all your vars
$somevar = 'Smarty rocks!';
$smarty->assign('title', $somevar);
// Repeat for all your vars
// Show template !
show_template();
?> |
TPL CODE, EASY PART
| Code: | index.tpl
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<title>{$title}</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body{$onload}>
<h1>{$title}</h1>
<p>Some body text over here.</p>
</body>
</html> |
TPL INCLUDING
| Code: | index.tpl
{include file="header.tpl"}
<h1>{$title}</h1>
<p>Some body text over here.</p>
</body>
</html> |
| Code: | header.tpl
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<title>{$title}</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body{$onload}> |
TPL IF's
Works mostly like your PHP if else, vars in your IF are not defined as {$foo} but just as your PHP vars : $foo
| Code: | index.tpl
{include file="header.tpl"}
<h1>{$title}</h1>
{if $title == 'Smarty rocks!'}
Smarty is soooo cool !
{/if}
<p>Some body text over here.</p>
</body>
</html> |
LOOPING, EASY PART, PHP CODE
Note, some functions are not included in this example, pay attention to the
* global $smarty;
* $data = array();
* $data[] = $row;
* $smarty->assign('nav_cats', $data);
| Code: | function show_nav($top_id) {
global $smarty;
$data = array ();
$sql = getSelectSQL('data', array (
'*'
), array (
'parentid' => $top_id
), -1);
dbconnect();
$result = dbquery($sql);
dbclose();
while ($row = mysql_fetch_array($result)) {
$row['seotitle'] = create_modrewrite($row['title']);
$data[] = $row;
}
$smarty->assign('nav_cats', $data);
} |
LOOPING, EASY PART, TPL CODE
| Code: | <ul>
{section name=loopdata loop=$nav_cats}
<li>
<a href="./{$nav_cats[loopdata].id}/{$nav_cats[loopdata].seotitle}.html" title="{$nav_cats[loopdata].cat}">{$nav_cats[loopdata].title}</a></li>
{/section}
</ul> |
USING PHP IN YOUR TPLS
| Code: | | {PHP}echo 'Test'; {/PHP} |
USING BRACKETS / JAVASCRIPT / CSS STYLE INFO IN YOUR TPLS
Make sure if you use { and } which are NOT var related ( example {$title} ) to place the whole code within
{literal} and {/literal}
For an example ( and the error code if you forget this ) see:
http://www.ramonfincken.com/31/PHP+codi...d+tag.html
More info? Read on:
http://www.smarty.net/quick_start.php
http://www.smarty.net/manual/en/
http://www.unixoshints.com/modules/nvRe...fiers.html
Last edited by ramon fincken on Sat Sep 27, 2008 8:33 am; edited 3 times in total |
|
| Back to top |
|
 |
Google adsense Advertisement
|
Posted: Mon Mar 31, 2008 1:03 pm Post subject: [tutorial] Smarty crash course for dummies :) |
|
|
Advertisement
|
|
| Back to top |
|
 |
ramon fincken Site's programmer
Joined: 03 Aug 2007 Posts: 93 Location: Amsterdam, the Netherlands/Europe
|
Posted: Mon Mar 31, 2008 1:04 pm Post subject: [tutorial] Smarty crash course for dummies :) |
|
|
| Reserved |
|
| Back to top |
|
 |
ramon fincken Site's programmer
Joined: 03 Aug 2007 Posts: 93 Location: Amsterdam, the Netherlands/Europe
|
|
| Back to top |
|
 |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
Technorati Profile
|