((Very) very) small template function

Post modules, themes or any other code you want to share with the community.

Moderator: Developers

jamesarthur
Posts: 55
Joined: Fri Nov 08, 2013 9:15 am

((Very) very) small template function

Postby jamesarthur » Mon Mar 24, 2014 5:17 pm

I made a little template function :). It does simple text replacement of {foo} with bar if $params contains 'foo' => 'bar'.

Code: Select all


    
/**
     * Simple quick and dirty templating system, required new-ish php (for closure)
     * 
     * @param String $template The template with tags like {this}
     * @param array $params The strings to replace and their replacements
     * @return String the compiled template
     */
    private static function renderTpl($template, $params) {
        return preg_replace_callback('/\{(\w+)\}/', function($matches) use ($params) {
            if(array_key_exists($matches[1], $params)) {
                return $params[$matches[1]];
            } else {
                return '';
            }
        }, $template);
    }
 

Return to “Contributed development”

Who is online

Users browsing this forum: No registered users and 1 guest

cron