Smarty - prepend - append

2 Smarty plugins die bij elkaar horen: prepend en append.
Beide geven alleen output als de gegeven input-variabele niet leeg is.

Om ze tegelijk te gebruiken: {$var|prepend:'string ervoor '|append:' string erna'}.

Dump onderstaande bestanden in je plugins directory.

modifier.prepend.php:

<?php
/**
 *    prepend - a Smarty modifier plugin to prepend a string to a non-empty variable
 *    Copyright (C) 2008 Rob la Lau
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of version 3 of the GNU General Public License
 *    as published by the Free Software Foundation.
 *
 *    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.
 *
 *    The GNU General Public License can be found at <http://www.gnu.org/licenses/>.
 */

/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * Smarty {prepend} modifier plugin
 *
 * Type:     modifier
 * Name:     prepend
 * Date:     2008-01-23
 * Purpose:  Only output string when included variable is not empty
 *
 * Input:
 *           - var: variable (may be empty)
 *           - string: string to be prepended when $var is not empty
 *
 * Examples:
 *           {$order.amount|prepend:"&euro;&nbsp;"}
 *
 * ChangeLog:
 *           - 20080123
 *             initial version (0.1)
 *
 * Feedback:
 *           is welcomed at rob@nerdstock.org
 *
 * @link http://nerdstock.org/smarty_prepend_append
 * @author Rob la Lau <rob@nerdstock.org>
 * @version 0.1
 *
 * @param string
 * @param string
 * @return string
 */

function smarty_modifier_prepend($string, $prepend = '') {
	if (! empty($string)) {
		return $prepend . $string;
	}
	return "";
}
?>

modifier.append.php:

<?php
/**
 *    append - a Smarty modifier plugin to append a string to a non-empty variable
 *    Copyright (C) 2008 Rob la Lau
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of version 3 of the GNU General Public License
 *    as published by the Free Software Foundation.
 *
 *    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.
 *
 *    The GNU General Public License can be found at <http://www.gnu.org/licenses/>.
 */

/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * Smarty {append} modifier plugin
 *
 * Type:     modifier
 * Name:     append
 * Date:     2008-01-23
 * Purpose:  Only output string when input variable is not empty
 *
 * Input:
 *           - var: variable (may be empty)
 *           - string: string to be appended when $var is not empty
 *
 * Examples:
 *           {$user.age|append:" years"}
 *
 * ChangeLog:
 *           - 20080123
 *             initial version (0.1)
 *
 * Feedback:
 *           is welcomed at rob@nerdstock.org
 *
 * @link http://nerdstock.org/smarty_prepend_append
 * @author Rob la Lau <rob@nerdstock.org>
 * @version 0.1
 *
 * @param string
 * @param string
 * @return string
 */

function smarty_modifier_append($string, $append = '') {
	if (! empty($string)) {
		return $string . $append;
	}
	return "";
}
?>

Reacties zijn welkom:
rob[at]nerdstock.org
CC-GNU GPL