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

New Function: PHP to JavaScript variable converter

 
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 -> Plugins
View previous topic :: View next topic  
Author Message
Rival67
Smarty n00b


Joined: 15 Oct 2004
Posts: 1

PostPosted: Fri Oct 15, 2004 8:49 pm    Post subject: New Function: PHP to JavaScript variable converter Reply with quote

Hey all,
I have a code contribution for smarty of a plugin function to convert any php variable (intergers, strings, arrays and objects) into a javascript expression. This function has been quite useful and I would hope that it could be reused by other folks. Enjoy,

Nathan Cassano

Code:

<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 * @version     $Revision: 1 $
 */


/**
 * Smarty {php2js} function plugin
 *
 * Type:     function<br>
 * Name:     php2js
 * Date:     Oct, 15 2004
 * Purpose:  Convert a php variable into an javascript expression
 * Input:<br>
 *         - var = Any php variable
 * Examples:
 * <pre>
 * <script language="JavaScript1.2">
 * var MyVariable = {php2js var="$var_assignment"};
 * </script>
 * </pre>
 * @version  1.0
 * @author   Nathan Cassano (nathan out resolutehosting dart com)
 * @param    mixed
 * @param    Smarty
 * @return   string
 *
 * COPYRIGHT:
 *     Copyright (c) 2004 Nathan Cassano
 *     This software is released under the GNU Lesser General Public License.
 *     Please read the following disclaimer
 *
 *      THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED
 *      WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 *      OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *      DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
 *      LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 *      OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 *      OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 *      OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 *      LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *      NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *      SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *     See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

function smarty_function_php2js($params, &$smarty)
{
    if (!isset($params['var'])) {
        $smarty->trigger_error("php2js: missing 'var' parameter");
        return;
    }

   return php2js($params['var']);
}

function php2js($var)
{
   $retval = '';

   $vartype = gettype($var);

   switch ($vartype)
   {
      case 'boolean':
         $retval = ($var) ? 'true' : 'false';
         break;

      case 'integer':
         $retval = strval($var);
         break;

      case 'double':
         $retval = strval($var);
         break;

      case 'string':
         $retval = '"'.addcslashes($var, "\0\r\n\"").'"';
         break;

      case 'array':

         // Determine if array is an indexed sequence
         $Indexed = true;
         $index = 0;
         foreach (array_keys($var) as $key)
         {
            if ($key !== $index)
            {
               $Indexed = false;
               break;
            }

            $index++;
         }

         // If indexed array
         if ($Indexed)
         {
            foreach ($var as $value)
            {
               if ( ! empty($retval)) $retval .= ',';

               $retval .= php2js($value);
            }
            
            $retval = '['. $retval .']';
         }
         // Else treat it as an associative array
         else
         {
            foreach ($var as $key => $value)
            {
               if ( ! empty($retval)) $retval .= ',';

               $retval .= php2js($key) . ':' .
                  php2js($value);
            }
            $retval = '{'. $retval .'}';
         }
         break;

      case 'object':
         
         foreach (get_object_vars($var) as $key => $value)
         {
            if ( ! empty($retval)) $retval .= ',';

            $retval .= php2js($key) . ':' .
               php2js($value);
         }
         $retval = '{'. $retval .'}';
         break;

      case 'resource':
         $retval = 'null';
         break;

      case 'NULL':
         $retval = 'null';
         break;

      default:
         $retval = "''";
         break;
   }

   return $retval;
}

/* vim: set expandtab: */

?>
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 -> 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