开发者

PHP - Extremely light templating system

does anyone know a php templating system that is very simple, something li开发者_如何学Goke almost as simple as str_replace("{variable}", $variable); ?

I need this for a series of textareas in the administration panel, where the site admin should change templates for various elements of the website (not complex stuff like pages etc, just blocks of content)


/**
 * Renders a single line. Looks for {{ var }}
 *
 * @param string $string
 * @param array $parameters
 *
 * @return string
 */
function renderString($string, array $parameters)
{
    $replacer = function ($match) use ($parameters)
    {
        return isset($parameters[$match[1]]) ? $parameters[$match[1]] : $match[0];
    };

    return preg_replace_callback('/{{\s*(.+?)\s*}}/', $replacer, $string);
}


$findReplaces = array(
    'first_name' => $user['first_name'],
    'greeting' => 'Good ' . (date('G') < 12 ) ? 'morning' : 'afternoon'
);

$finds = $replaces = array();

foreach($findReplaces as $find => $replace) {
    $finds[] = '{' . $find . '}';
    $replaces[] = $replace;
}

$content = str_replace($finds, $replaces, $content);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜