What is Smarty?
Why use it?
Use Cases and Work Flow
Syntax Comparison
Template Inheritance
Best Practices
Crash Course
You may use the Smarty logo according to the trademark notice.
For sponsorship, advertising, news or other inquiries, contact us at:
Variablen-Modifikatoren sind kleine Funktionen, die auf eine Variable angewendet werden, bevor sie ausgegeben oder weiterverwendet wird. Variablen-Modifikatoren können aneinadergereiht werden.
mixed smarty_modifier_name( |
$value, | |
$param1) ; |
mixed $value
;[mixed $param1, ...]
;Der erste an das Modifikator-Plugin übergebene Parameter ist der Wert mit welchem er arbeiten soll. Die restlichen Parameter sind optional und hängen von den durchzuführenden Operationen ab.
Der Modifikator muss das Resultat seiner Verarbeitung zurückgeben.
Sehen Sie dazu: register_modifier(), unregister_modifier().
Example 16.3. Einfaches Modifikator-Plugin
Dieses Plugin dient als Alias einer PHP-Funktion und erwartet keine zusätzlichen Parameter.
<?php /* * Smarty plugin * ------------------------------------------------------------- * File: modifier.capitalize.php * Type: modifier * Name: capitalize * Purpose: capitalize words in the string * ------------------------------------------------------------- */ function smarty_modifier_capitalize($string) { return ucwords($string); } ?>
Example 16.4. Komplexes Modifikator-Plugin
<?php /* * Smarty plugin * ------------------------------------------------------------- * File: modifier.truncate.php * Type: modifier * Name: truncate * Purpose: Truncate a string to a certain length if necessary, * optionally splitting in the middle of a word, and * appending the $etc string. * ------------------------------------------------------------- */ function smarty_modifier_truncate($string, $length = 80, $etc = '...', $break_words = false) { if ($length == 0) return ''; if (strlen($string) > $length) { $length -= strlen($etc); $fragment = substr($string, 0, $length+1); if ($break_words) $fragment = substr($fragment, 0, -1); else $fragment = preg_replace('/\s+(\S+)?$/', '', $fragment); return $fragment.$etc; } else return $string; } ?>