Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Dedicated server web hosting provided by Guru-host.eu.
Printing value of a Magical variable

 
Post new topic   Reply to topic    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 419
Location: Kathmandu, Nepal

PostPosted: Sat Apr 24, 2010 2:04 am    Post subject: Printing value of a Magical variable Reply with quote

It is not really good to write lengthy codes Smile This magical modifier reduces possible PHP codes into a brief template code. It searches for values of an index in $_POST, $_GET, $_SESSION and $_COOKIE according to your priorities.

For example, the normal PHP code would be:
Code:

$subdomain_id = $_POST['subdomain_id'];
#... search more in $_GET, $_SESSION, $_COOKIE...
$smarty->assign('subdomain_id', $subdomain_id);


This can lead to too much of repetitions in your several .php files. But the below mentioned magical modifier removes this burden completely, and puts everything within the template code's FORM elements. A sample usage is:
Code:
<select name="subdomain_id">{html_options options=$options selected='subdomain_id'|magical}</select>


Traditionally, you would first need to assigin the variable, and use it:
Code:
<select name="subdomain_id">{html_options options=$options selected=$subdomain_id}</select>


Download?
Click here to download this plugin.

Basic Code: modifier.magical.php
Code:

<?php
/**
* Magical value reader
* Searching for an existing indexed value on $_POST, $_GET, $_SESSION, $_COOKIE.
* Makes sure that the return value is always a string.
*/
function smarty_modifier_magical($variable_index='')
{
   $value = '';
   switch(true)
   {
   case isset($_POST[$variable_index]):
      # Most frequent chance
      $value = "{$_POST[$variable_index]}";
      break;
   case isset($_GET[$variable_index]):
      $value = "{$_GET[$variable_index]}";
      break;
   case isset($_SESSION[$variable_index]):
      # Average change
      $value = "{$_SESSION[$variable_index]}";
      break;
   case isset($_COOKIE[$variable_index]):
      $value = "{$_COOKIE[$variable_index]}";
      break;
   default:
   }
   return($value);
}
?>


I appreciate your comments and queries about this modifier.
_________________
Skype: pbimal
To hire instantly as a freelancer - https://www.odesk.com/o/profiles/users/_~~657b70cc7f2c616a/
Visit my website - http://bimal.org.np/ for more articles
800+ screenshots to learn about website mistakes: http://mistakes.sanjaal.com/
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 419
Location: Kathmandu, Nepal

PostPosted: Sat Apr 24, 2010 2:08 am    Post subject: Usage in template? Reply with quote

For realtime usage in a template file, please see here
_________________
Skype: pbimal
To hire instantly as a freelancer - https://www.odesk.com/o/profiles/users/_~~657b70cc7f2c616a/
Visit my website - http://bimal.org.np/ for more articles
800+ screenshots to learn about website mistakes: http://mistakes.sanjaal.com/
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Plugins 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