Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

How to call PHP functions in smarty?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
sandia
Smarty n00b


Joined: 04 Jan 2008
Posts: 3

PostPosted: Fri Jan 04, 2008 7:35 am    Post subject: How to call PHP functions in smarty? Reply with quote

Hi,

How to call PHP functions in smarty?

Thanks,

Sandia
Back to top
View user's profile Send private message
elpmis
Smarty Elite


Joined: 07 Jun 2007
Posts: 321

PostPosted: Fri Jan 04, 2008 8:30 am    Post subject: Reply with quote

What you want to do?

One way could be

Code:
{php}
echo "bla";
{/php}
Back to top
View user's profile Send private message
sandia
Smarty n00b


Joined: 04 Jan 2008
Posts: 3

PostPosted: Fri Jan 04, 2008 9:14 am    Post subject: How to call PHP functions in smarty? Reply with quote

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
View user's profile Send private message
elpmis
Smarty Elite


Joined: 07 Jun 2007
Posts: 321

PostPosted: Fri Jan 04, 2008 9:27 am    Post subject: Reply with quote

Smarty is a template engine, not a coding language Cool .

But you can assign values to Smarty

http://www.smarty.net/manual/en/api.assign.php

Then you can output values inside Smarty with a foreach loop

http://www.smarty.net/manual/en/language.function.foreach.php

Btw. Smarty has an option tag too

http://www.smarty.net/manual/en/language.function.html.options.php
Back to top
View user's profile Send private message
sandia
Smarty n00b


Joined: 04 Jan 2008
Posts: 3

PostPosted: Fri Jan 04, 2008 11:16 am    Post subject: Reply with quote

Thanks..

I got it..
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Jan 04, 2008 3:42 pm    Post subject: Reply with quote

Use custom functions and modifiers to call PHP code. See the plugins section of the manual. Stay away from {php} tags.
Back to top
View user's profile Send private message Visit poster's website
samsolomon
Smarty Rookie


Joined: 02 Feb 2009
Posts: 6
Location: Chennai, India

PostPosted: Mon Feb 02, 2009 7:07 am    Post subject: hi Reply with quote

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
View user's profile Send private message Send e-mail
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Sat Feb 07, 2009 3:58 pm    Post subject: Custom functions are modfiers are helpful. Reply with quote

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
View user's profile Send private message Visit poster's website
triphp
Smarty n00b


Joined: 24 Jul 2006
Posts: 3

PostPosted: Wed Jul 21, 2010 10:04 am    Post subject: Re: hi Reply with quote

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
View user's profile Send private message
ucntkilme
Smarty Regular


Joined: 03 Sep 2007
Posts: 84

PostPosted: Wed Jul 21, 2010 5:11 pm    Post subject: Reply with quote

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
View user's profile Send private message
slick4evah@yahoo.com
Smarty n00b


Joined: 21 May 2015
Posts: 1

PostPosted: Thu May 21, 2015 7:41 am    Post subject: Re: hi Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Fri May 22, 2015 7:08 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development 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