Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Variable variable plugins: a function and a modifier

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


Joined: 04 Feb 2010
Posts: 2
Location: Lawrence, KS

PostPosted: Thu Feb 04, 2010 9:01 pm    Post subject: Variable variable plugins: a function and a modifier Reply with quote

In our office, we had a need for variable variables within Smarty, and until v3 leaves beta (I believe varvars are in the new version), we are locked into v2. So I wrote the following code to get the functionality we needed. Hopefully it will be helpful to someone else.

varvar function:
Usage: {varvar var=$mainvar}
Code:
Code:
/**
 * Smarty {varvar} function plugin
 *
 * Type:     function
 * Name:     varvar
 * Purpose:  Assume the input is a string, and if a smarty variable with that name
 *          is set, return its value. Otherwise, return the original string
 * @param array
 * @param Smarty
 */
function smarty_function_varvar($params, &$smarty) {
    $value = $smarty->get_template_vars($params['var']);
    if ($value) {
        return $value;
    } else {
        return $params['var'];
    }
}


varvar modifier:
Usage: {$mainvar|varvar}, {assign var=myvar value=$mainvar|varvar}, etc
Code:
Code:
/*
* Smarty plugin
*
-------------------------------------------------------------
* Purpose: Assume the input is a string, and if a smarty variable with that name
*          is set, return its value. Otherwise, return the original string
* Install: Drop into the plugin directory.
*
* Usage: {$myvar|varvar} checks if the string $myvar is assigned as a smarty
* variable
*
-------------------------------------------------------------
*/
function smarty_modifier_varvar($var) {
    $smarty = Smarty::getInstance();
    $value = $smarty->get_template_vars($var);
    if ($value) {
        return $value;
    } else {
        return $var;
    }
}


The code for both of these was partly derived from code I found searching the forums here, but nothing was *quite* what we needed... hence the new stuff.

Also, for the modifier, we needed a way to access the Smarty object without knowing what scope it was in. So we added code to the base class that partly implemented the singleton design pattern. Here is a link to that post.

http://www.smarty.net/forums/viewtopic.php?p=62503

Thanks, and I hope these are helpful to others out there.
_________________
Bill Parrott
Web Developer, University of Kansas
Co-Owner and Developer, Eternal Second Designs
Blogger and General Geek, ChimericDream
Back to top
View user's profile Send private message Visit poster's website
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