Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
trying to learn to use Smarty.

 
Post new topic   Reply to topic    Smarty Forum Index -> Article Discussions
View previous topic :: View next topic  
Author Message
edemocracyub
Smarty n00b


Joined: 27 Feb 2004
Posts: 1

PostPosted: Fri Feb 27, 2004 6:40 pm    Post subject: trying to learn to use Smarty. Reply with quote

Hi ,

I'm trying to learn to use smarty...I have read all the documentation and few tutorials but I still don't get it.

btw. it would be really helpful if someone knows a web site where the use of smarty codes can be studied.

any help will be very helpful.
Thanks.
Back to top
View user's profile Send private message
Duncan
Smarty Pro


Joined: 16 Dec 2003
Posts: 166

PostPosted: Fri Feb 27, 2004 7:15 pm    Post subject: Reply with quote

The examples provided in the Smarty distribution already give a nice overview of some its abilities as well as the Smarty CrashCourse: http://smarty.php.net/crashcourse.php

Otherwise I would recommend the following two tutorials, which provide a detailed walkthrough to create some sample pages:

http://www.zend.com/zend/tut/tutorial-cezar.php

http://www.zend.com/zend/tut/tutorial-stump.php
Back to top
View user's profile Send private message
MindPhlash
Smarty Rookie


Joined: 14 May 2008
Posts: 10

PostPosted: Thu May 15, 2008 5:51 pm    Post subject: Reply with quote

I bought the one book that I could find on it - Smarty - PHP Template programming and Applications, but it's still not the world's greatest reference.

I find myself doing a lot of {php} {/php} as hacks around just to make things work, but I know there's ways to do things in smarty because I see the tags for them, the tags just arn't explained / documented all that well


another thing I'd reccomend would be the smarty cheat sheet http://hasin.wordpress.com/2006/06/10/smarty-cheat-sheet-version-20/

(it's from one of the guys that wrote the smarty book), and I keep it at my desk with my favorite CSS cheat sheet
Back to top
View user's profile Send private message
shannera
Administrator


Joined: 13 Feb 2006
Posts: 802
Location: Edertal, Germany

PostPosted: Fri May 16, 2008 12:07 am    Post subject: Reply with quote

If you have to use {php} tags, you are surely making something wrong. I'm using Smarty for a few years, and hadn't used any php tags, because anything that can be placed in PHP tags can also be placed in the calling script.

Also I don't understand that you find the documentation isn't good enough. What tags are you referring to?
Back to top
View user's profile Send private message
MindPhlash
Smarty Rookie


Joined: 14 May 2008
Posts: 10

PostPosted: Fri May 16, 2008 12:28 pm    Post subject: Reply with quote

On of the tags I've been finding lacking on documentation is the $smarty.session reserved variable

I'm using sessions in my current site, however I've tried a few times to use the reserve $smarty tag but I could never seem to get the exact way on how it worked even after reading http://www.smarty.net/manual/en/language.variables.smarty.php

so instead I've been using a piece of code such as :

Code:
{php}
session_start();
//echo $_SESSION["logged_in"];
if(!session_is_registered(myusername)){
header("location:index.php");
}
{/php}
{include file='header.tpl'}


if you could give me a pointer on how to make this more efficient then I'm more then willing to listen and learn. Very Happy

After all I'm about to add more things to my sessions then just checking to see if the username is registered
Back to top
View user's profile Send private message
Celeb
Administrator


Joined: 17 Apr 2007
Posts: 1025
Location: Vienna

PostPosted: Fri May 16, 2008 1:01 pm    Post subject: Reply with quote

$smarty.session is nothing but a "name" for the $_SESSION superglobal array.
You can do stuff like
Code:
{if isset($smarty.session.username)}
  Hello {$smarty.session.username|capitalize}!
{/if}


Maybe we could help you further, if you'd post a piece of code that doesn't work correctly for you.
Btw, I'd do all the session handling, authentication, etc in PHP rather than in the template and only set some flags to let Smarty know which parts of the website you want to be displayed.
_________________
Darn computers always do what I tell them to instead of what I want them to do.
Back to top
View user's profile Send private message
MindPhlash
Smarty Rookie


Joined: 14 May 2008
Posts: 10

PostPosted: Fri May 16, 2008 1:33 pm    Post subject: Reply with quote

I just tried the {if} statement that you gave me for the smarty sessions, but nothing showed up

here's where I declared my session variables upon login:

Code:

session_register("myusername");
session_register("mypassword");
$_SESSION['views'] = $_SESSION['views'] + 1; // store session data


then here's where I took the code you gave me and thought I changed the variable right to display the username???

Code:
{if isset($smarty.session.myusername)}
  Hello {$smarty.session.myusername|capitalize}!
{/if}


(now this page still does have my origional {PHP} session codeblock at the top to check and make sure that the user is logged in) - not sure if that is effecting anything
Back to top
View user's profile Send private message
Celeb
Administrator


Joined: 17 Apr 2007
Posts: 1025
Location: Vienna

PostPosted: Fri May 16, 2008 1:52 pm    Post subject: Reply with quote

The use of session_register() is deprecated.

Why don't you do it like this:
Code:
session_start();
$_SESSION['myusername'] = 'bob';
$_SESSION['mypassword'] = 'hidden';
$_SESSION['views']++;


You can also print_r($_SESSION) to see which variables are set at all.
_________________
Darn computers always do what I tell them to instead of what I want them to do.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Article Discussions 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


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP