Page 1 of 1
problem in smarty .....
Posted: Fri Mar 02, 2007 4:00 am
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!!
Posted: Fri Mar 02, 2007 7:37 am
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.
Posted: Fri Mar 02, 2007 8:30 am
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.....!
Posted: Fri Mar 02, 2007 8:35 am
by PHPycho
one more question please !
how to perform the following with smarty ?
Code: Select all
<?php
$count = 0;
while()
{
echo $count;
$count ++;
}
?>
Posted: Fri Mar 02, 2007 8:39 am
by feyd
Posted: Fri Mar 02, 2007 8:48 am
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 ?
Posted: Fri Mar 02, 2007 9:18 am
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/
Posted: Fri Mar 02, 2007 9:20 am
by PHPycho
when to use the {php} tag in case of .tpl
is there any such case ??
Posted: Fri Mar 02, 2007 9:26 am
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.
Posted: Fri Mar 02, 2007 10:08 am
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
Posted: Fri Mar 02, 2007 10:42 am
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.
Posted: Fri Mar 02, 2007 11:33 am
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
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.
Posted: Fri Mar 02, 2007 11:35 am
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.
Posted: Fri Mar 02, 2007 12:55 pm
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