View previous topic :: View next topic |
Author |
Message |
sandia Smarty n00b
Joined: 04 Jan 2008 Posts: 3
|
Posted: Fri Jan 04, 2008 7:35 am Post subject: How to call PHP functions in smarty? |
|
|
Hi,
How to call PHP functions in smarty?
Thanks,
Sandia |
|
Back to top |
|
elpmis Smarty Elite
Joined: 07 Jun 2007 Posts: 321
|
Posted: Fri Jan 04, 2008 8:30 am Post subject: |
|
|
What you want to do?
One way could be
Code: | {php}
echo "bla";
{/php} |
|
|
Back to top |
|
sandia Smarty n00b
Joined: 04 Jan 2008 Posts: 3
|
Posted: Fri Jan 04, 2008 9:14 am Post subject: How to call PHP functions in smarty? |
|
|
Thanku for the reply...
Is there any other way to call PHP functions in smarty?
My function is:
Code: | function country_dropdown($country_dropdown_name,$default="",$style_name="",$tabindex="")
{
if($default!="")
$$default = "selected";
global $GEOIP_COUNTRY_NAMES_CODES;
asort($GEOIP_COUNTRY_NAMES_CODES);
reset($GEOIP_COUNTRY_NAMES_CODES);
ECHO "<SELECT NAME=\"$country_dropdown_name\" class=\"$style_name\" tabindex=\"$tabindex\">";
while (list ($key, $val) = each ($GEOIP_COUNTRY_NAMES_CODES))
{
?>
<option value="<?php echo $key; ?>" <?php if($default == $key) echo 'selected="selected"';?>><?php echo $val; ?></option>
<?php
}
echo "</SELECT>";
}
|
im calling in php as:
Code: | <?country_dropdown("shipping_country", "GB", "input");?> |
But how to write it in SMARTY?
Thanks,
Sandia |
|
Back to top |
|
elpmis Smarty Elite
Joined: 07 Jun 2007 Posts: 321
|
|
Back to top |
|
sandia Smarty n00b
Joined: 04 Jan 2008 Posts: 3
|
Posted: Fri Jan 04, 2008 11:16 am Post subject: |
|
|
Thanks..
I got it.. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri Jan 04, 2008 3:42 pm Post subject: |
|
|
Use custom functions and modifiers to call PHP code. See the plugins section of the manual. Stay away from {php} tags. |
|
Back to top |
|
samsolomon Smarty Rookie
Joined: 02 Feb 2009 Posts: 6 Location: Chennai, India
|
Posted: Mon Feb 02, 2009 7:07 am Post subject: hi |
|
|
you can call the php function in the smarty.
Write the php function in the class file.
then create the object for the php in the php file.
Then assign the object to another smarty variable like,
$objSmarty->assign("common", "objectname");
Then call the function in the smarty like,
{$common->function_name($arg1,$arg2)}
I used to call the php functions in the smarty like this.
If you have any doubts on this i will surely help you...
Bye...... |
|
Back to top |
|
bimal Smarty Elite
Joined: 19 Apr 2007 Posts: 423
|
Posted: Sat Feb 07, 2009 3:58 pm Post subject: Custom functions are modfiers are helpful. |
|
|
mohrt wrote: | Use custom functions and modifiers to call PHP code. See the plugins section of the manual. Stay away from {php} tags. |
Writing custom functions and modifiers is a nicest idea. Though Smarty supports {php} and {/php} tags, it is better, not to use them. Since, you can write native php codes within these tags, there is no meaning of using template engine. However, for extreme cases only, {php} tags are useful.
Keep your templates clean.
I appeal to the beginers NOT to read about support for {php} tags. Using this will make your template programming more complex. You are likely to use your business logic within the template with {php} tags, that is against the theme of Smarty. |
|
Back to top |
|
triphp Smarty n00b
Joined: 24 Jul 2006 Posts: 3
|
Posted: Wed Jul 21, 2010 10:04 am Post subject: Re: hi |
|
|
samsolomon wrote: | you can call the php function in the smarty.
Write the php function in the class file.
then create the object for the php in the php file.
Then assign the object to another smarty variable like,
$objSmarty->assign("common", "objectname");
Then call the function in the smarty like,
{$common->function_name($arg1,$arg2)}
I used to call the php functions in the smarty like this.
If you have any doubts on this i will surely help you...
|
Can you please show us how you did it? |
|
Back to top |
|
ucntkilme Smarty Regular
Joined: 03 Sep 2007 Posts: 84
|
Posted: Wed Jul 21, 2010 5:11 pm Post subject: |
|
|
here is an easy way...
replace your function with this:
function country_dropdown($country_dropdown_name,$default="",$style_name="",$tabindex="")
{
if($default!="")
$$default = "selected";
global $GEOIP_COUNTRY_NAMES_CODES;
asort($GEOIP_COUNTRY_NAMES_CODES);
reset($GEOIP_COUNTRY_NAMES_CODES);
$cntry = '<SELECT NAME="'.$country_dropdown_name.'" class="'.$style_name.'" tabindex="'.$tabindex.'">';
while (list ($key, $val) = each ($GEOIP_COUNTRY_NAMES_CODES))
{
$cntry .= '<option value="'. $key .'" selected="'.$default.'">'.$val.'</option>
}
$cntry .= "</SELECT>";
return $cntry;
}
then in your php file you would call it using something like
$data = $aws->country_dropdown(); or even just $data = country_dropdown();
then of course to submit it to smarty using $smarty->assign('data', $data);
and to display in tpl: {$data}
tis the way i handle a few things like that when i need special data put in. _________________ FCKit V1. : http://www.smarty.net/forums/viewtopic.php?t=17748 |
|
Back to top |
|
slick4evah@yahoo.com Smarty n00b
Joined: 21 May 2015 Posts: 1
|
Posted: Thu May 21, 2015 7:41 am Post subject: Re: hi |
|
|
samsolomon wrote: | you can call the php function in the smarty.
Write the php function in the class file.
then create the object for the php in the php file.
Then assign the object to another smarty variable like,
$objSmarty->assign("common", "objectname");
Then call the function in the smarty like,
{$common->function_name($arg1,$arg2)}
I used to call the php functions in the smarty like this.
If you have any doubts on this i will surely help you...
Bye...... |
This works perfect, with the exception that you need to use the PHP object itself, not object name.
Quote: | $objSmarty->assign("common", $objectname); |
|
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Fri May 22, 2015 7:08 am Post subject: |
|
|
I Smarty 3 you acn call PHP function directly unless it'a not disabled by security
like
Code: | {funcname(....)}
or
{$foo = funcname(...)}
|
|
|
Back to top |
|
|