Smarty-Plugin: Stringvariable in Einzelkomponenten zerlegen

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>
Über Anregungen bzw. Vorschläge würde ich mich sehr freuen. Ein paar Kommentare wären für den Anfang aber auch nicht soooo schlecht :-)
 
 Artikel als Bookmark:
Beitrag wurde bisher 447 mal geöffnet
RA Zabel/Stolze
  • 96 Artikel  (zeigen)
  • 66527 Klicks
  • 56 Kommentare
  • 9 Artikel (2010)  (zeigen)
  • 1854 Klicks (2010)
  • 0 Kommentare (2010)
Freunde / Linkparade (4)