problem in smarty .....

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

problem in smarty .....

Post by PHPycho »

Hello forum
i am getting problem in smarty
case:
wwwroot/mySite/admin has files like
-index.php
-listA.php
-listB.php
/templates/ folder has
-header.tpl
-footer.tpl
-index.tpl
-listA.tpl
-listB.tpl
index.php contains

Code: Select all

<?php
//in upper section it includes all the class files and creates the respective objects
// neccessary processing goes here...
$smarty->display("index.tpl");
?>
index.tpl contains

Code: Select all

{php}
if(isset($_GET['action']))
{
	$filename = $_GET['action'].".php";
	if(file_exists($filename))
	{
		include "$filename";
	}
	else
	{
//error
        }
{/php}
:::means it automatically includes the file as per $_GET action
suppose i clicked on the link ?action=listA then it auto includes the page listA.php which then display the array data in listA.tpl

problem:
It was working in normal case ie without using smarty but Now whenever i clik on the links
it shows:
Call to a member function selectAll() on a non-object in C:\Program Files\xampp\htdocs\mySite\admin\listA.php on line 39
i am not getting the solution please help.
Thanks in advance to all of you!!
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

Well first off, the problem is in the file you are including, or because you are not including something it requires. Or it could be because smarty is compiling the file and there are problems with the code after its cached. Or it could be because the smarty template is ran inside a function meaning the variables you are trying to access in listA.php are out of scope.

Secondly, don't put all that code in tpl files, put the code in your php file and pass data to smarty to display, otherwise there is no point at all in using smarty. Doing this might solve your problem as well.

Thirdly, do not take something from the URL and use that as a file name to include. That is a pretty big security hole.
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post by PHPycho »

Thanks dude....
I got my problem solved

My mistake:
I tried to include the file in .tpl so the objects are unknown to them
unless u pass by assigning....

I better solved by removing the index.tpl file , instead used

Code: Select all

<?php
//in upper section it includes all the class files and creates the respective objects
// neccessary processing goes here...
$smarty->display("header.tpl");

// .php files are included here as per $_GET action

$smarty->display("footer.tpl");
?>
Thanks all Devnetworkians for partipitation.....!
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post by PHPycho »

one more question please !
how to perform the following with smarty ?

Code: Select all

<?php
$count = 0;
while()
{
echo $count;
$count ++;
}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post by PHPycho »

Thanks dude !!
That function is really Awsome.....

One important thing i want to ask
1> where the best advance tutorials about smarty can be found ?
2> any smarty based OOP scripts available ?
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

PHPycho wrote:Thanks dude !!
That function is really Awsome.....

One important thing i want to ask
1> where the best advance tutorials about smarty can be found ?
2> any smarty based OOP scripts available ?

1. http://smarty.php.net/manual/en/index.php
2. http://www.phpinsider.com/smarty-forum/
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post by PHPycho »

when to use the {php} tag in case of .tpl
is there any such case ??
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

PHPycho wrote:when to use the {php} tag in case of .tpl
is there any such case ??
my advice.. in veeeery rare cases.
I use smarty for two years now and never had to use it.
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post by PHPycho »

How would you use smarty in this case:

case:

Code: Select all

<?php
if(!isset($_GET['action']))
{
echo "Hello this is index page";
}
else
{
echo "Hello this is non index page";
}
?>
what i mean to say is that :
is there any function like isset in smarty ??

i would also like to know the reasons for not using the {php} tags
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

jmut wrote:
PHPycho wrote:when to use the {php} tag in case of .tpl
is there any such case ??
my advice.. in veeeery rare cases.
I use smarty for two years now and never had to use it.
and when you say "veeeery rare cases" you mean never. You should always separate your presentation layer from your business layer.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

PHPycho wrote:How would you use smarty in this case:

case:

Code: Select all

<?php
if(!isset($_GET['action']))
{
echo "Hello this is index page";
}
else
{
echo "Hello this is non index page";
}
?>
what i mean to say is that :
is there any function like isset in smarty ??

i would also like to know the reasons for not using the {php} tags
You wouldn't do this in Smarty. You would handle the GET request in the code and set a variable then parse that variable in template.

Code: Select all

<?php
$page_text = isset($_GET['action']) ? 'Hello this is not index' : 'Hello this is index';

$tpl->assign(array(
    'pagetext' => $page_text)
);

$tpl->display('template.tpl');
?>
Then in the template you would do something like

Code: Select all

<p>{$pagetext}</p>
As for not using {php} tags in the template... it is a template. It is markup. The PHP files are for handling the code logic, not the template. That is the whole point of using a template parser.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

PHPycho wrote:How would you use smarty in this case:

case:

Code: Select all

<?php
if(!isset($_GET['action']))
{
echo "Hello this is index page";
}
else
{
echo "Hello this is non index page";
}
?>
what i mean to say is that :
is there any function like isset in smarty ??

i would also like to know the reasons for not using the {php} tags

Smarty is for separating your logic from the view. If you start putting a bunch of logic (code) into the templates then you are defeating the purpose.

To solve the problem above, you can do it in two ways

Code: Select all

if( !isset($_GET['action']) )
{
  $msg = "Hello this is index page";
}
else
{
  $msg ="Hello this is non index page";
}

$smarty->assign('msg', $msg) ;

// then have the following in your template somewhere

{$msg}
Or you can put an if right in the template using smarty ifs, but again, this is logic in your presentation and you should avoid it if you can

Code: Select all

$smarty->assign('action', $_GET['action']) ;

// in your .tpl
{if $action neq ''}
  Hello this is index page
{else}
  Hello this is non index page
{/if}
Also, as I said previously, don't take something from $_GET[] and use it to include a file, that is a security vulnerabilty/very bad idea.
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post by PHPycho »

Thanks dude!!
Now i am crystal clear about smarty and the coding style in .tpl...
Also, as I said previously, don't take something from $_GET[] and use it to include a file, that is a security vulnerabilty/very bad idea.
So whats the solution then ?
I would really appreciate if i get the solution
Post Reply