Smarty-Plugin: Stringvariable in Einzelkomponenten zerlegen

17. Dezember 2008
Hier möchte ich euch mein Smarty-Plugin vorstellen. Die Idee dazu kam mir, als ich über Mehrsprachigkeit in meinem neuen Projekt nachdachte. Hier gibt es zwei Smarty-Dateien für die Sprachkonfigurationen (de,en). Ziel war es, die Begriffe/Sprachelemente nicht unnötig über das Projekt zu verteilen. D.h., ich wollte nicht einen Teil der Begriffe in den Konfigurationdateien und einen anderen Teil in die PHP-Dateien integrieren.

Nun stand ich aber vor dem Problem, dass ich in den Smarty-Konfigurationsdateien keine Array-Element definieren konnte bzw. kann. Außerdem wollte ich sowieso schon immer mal ein Smarty-Plugin schreiben :-)
Herausgekommen ist also folgendes Plugin, das ihr euch hier runterladen könnt. Es steht unter der GPL.
 
<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/*
 * Distributed under the terms of the GPL:
 * Copyright (c) 2008, Matthias Sonnenkalb
 * (www.matthias-sonnenkalb.net, msonnenkalb@gmx.de)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/**
 * Smarty {explodevar}{/explodevar} block plugin
 *
 * Type:     block function<br>
 * Name:     explodevar<br>
 * Purpose:  splits a string into chunks devided by a delimiter
 * @param array
 * <pre>
 * Params:   value: string to split
 *           delim: delimiter used to separate value
 * </pre>
 * @author Mathias Sonnenkalb (msonnenkalb@gmx.de)
 *         http://www.matthias-sonnenkalb.net/
 * @param string contents of the block
 * @param Smarty
 * @return string $content.
 */
function smarty_block_explodevar($params, $content, &$smarty, &$repeat)
{
    static $retary = array();
    static $count = 0;

    // check for required parameters
    if( !isset( $params['value'] )) {
        $smarty->trigger_error('explodevar :: missing "value" parameter.');
        $repeat = false;
        return;
    }
    if( !isset($params['delim'] )) {
        $smarty->trigger_error('explodevar :: missing "delim" parameter');
        $repeat = false;
        return;
    }

    // enter first function pass (call opening tag)
    if( is_null( $content )) {
        $vals = explode($params['delim'], $params['value']);
        foreach( $vals as $val ) {
            if( !isset( $retary[$count] )) {
                $retary[$count] = $val;
                $count++;
            }
        }
    }

    // all subsequent function passes
    if( list($key, $val) = each( $retary )) {
        $smarty->assign('key', $key);
        $smarty->assign('val', $val);
        $repeat = true;
    } else {
        $repeat = false;
    }

    if( !is_null( $content )) {
        return $content;
    }
}

?>
 
Der Aufruf bzw. das Einbinden ins Template sieht dann z.B. wie folgt aus:
 
{* -- Smarty-Template: direkte Wertübergabe -- *}

<select name="auswahl" size="1">
    {explodevar value="Sachsen-Anhalt:Sachsen:Brandenburg:Bayern" delim=":"}
        <option value="{$key}">{$val}</option>
    {/explodevar}
</select>


{* -- Smarty-Template: Wert aus Element der Config-Datei -- *}

<select name="auswahl" size="1">
    {explodevar value=#countries# delim=":"}
        <option value="{$key}">{$val}</option>
    {/explodevar}
</select>
 
Es wurde noch kein Kommentar abgegeben. Bist Du der Erste?
HILFE
Code-Beispiele werden mit den BBCodes [code][/code] dargestellt. Jedes Tag muss eine Zeile für sich allein haben, d.h der Beispiel-Code muss wirklich ZWISCHEN den Tags stehen. Beispiel:
[code] ACHTUNG! HIER UMBRUCH WICHTIG. TAG [code] MUSS ALLEIN STEHEN
#include <stdio.h>

int main( void ){
    return 0;
} ACHTUNG! HIER UMBRUCH WICHTIG. NACHFOLGENDES TAG [/code] MUSS ALLEIN STEHEN
[/code]

Alles was zwischen den beiden Tags [register] und [/register] eingetragen wird, ist nur für registrierte (und eingeloggte) User sichtbar.
Dein Name *
Deine Email
Deine Website
Vorschau