Top Poster
#1 ramon fincken 397
#2 Site admin 3
#3 Ber|Art 2
#4 braddmark 1
#5 accentmedia 1
#6 Xarzu 1
#7 pEcosespedex 1
#8 khadish 1
#9 moxxx 1
#10 nistelrock 1
[tutorial] Smarty crash course for dummies :)



Rating : 4.5 / 5

 
Post new topic   Reply to topic    Ramonfincken.com Forum Index -> PHP coding
Beta
Want to be notified by email when this topic gets a reply?  
View previous topic :: View next topic  
Author Message
ramon fincken
Site's programmer

Get a free globally recognized avatar
It's free!


Joined: 03 Aug 2007
Posts: 397
Location: A'dam/Diemen, The Netherlands

PostPosted: Mon Mar 31, 2008 1:03 pm    Post subject: [tutorial] Smarty crash course for dummies :) Reply with quote

Smarty crash course for dummies Smile


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
View user's profile Send private message Visit poster's website
Google adsense
Advertisement





PostPosted: Mon Mar 31, 2008 1:03 pm    Post subject: [tutorial] Smarty crash course for dummies :)

Advertisement
Back to top
GravityForms
Advertisement





PostPosted: Mon Mar 31, 2008 1:03 pm    Post subject: [tutorial] Smarty crash course for dummies :)

Advertisement
Gravity Forms Plugin for WordPress
Back to top
ramon fincken
Site's programmer

Get a free globally recognized avatar
It's free!


Joined: 03 Aug 2007
Posts: 397
Location: A'dam/Diemen, The Netherlands

PostPosted: Mon Mar 31, 2008 1:04 pm    Post subject: [tutorial] Smarty crash course for dummies :) Reply with quote

Reserved
Back to top
View user's profile Send private message Visit poster's website
ramon fincken
Site's programmer

Get a free globally recognized avatar
It's free!


Joined: 03 Aug 2007
Posts: 397
Location: A'dam/Diemen, The Netherlands

PostPosted: Sun Oct 19, 2008 12:10 pm    Post subject: Re: [tutorial] Smarty crash course for dummies :) Reply with quote

Pagination with Smarty?

No problem: http://jorgev2.com/2007/05/19/smarty-and-pagination/
Back to top
View user's profile Send private message Visit poster's website
Post new topic   Reply to topic    Ramonfincken.com Forum Index -> PHP coding All times are GMT
Page 1 of 1

 
Jump to:  
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
General forums
The world of freelance
Programming
Other projects
Poll
I love gifts

Yes goodies and presents! [3]
No but I love polls ! [1]

Related google ads