mirror of
https://github.com/processwire/processwire.git
synced 2025-08-22 06:13:57 +02:00
Add a setting() function to the Functions API for more convenient management of site-specific runtime settings
This commit is contained in:
@@ -430,3 +430,47 @@ function region($key = '', $value = null) {
|
|||||||
return wireRegion($key, $value);
|
return wireRegion($key, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get or set a runtime site setting
|
||||||
|
*
|
||||||
|
* This is a simple helper function for maintaining runtime settings in a site profile.
|
||||||
|
* It simply sets and gets settings that you define. It is preferable to using ProcessWire’s
|
||||||
|
* `$config` or `config()` API var/function because it is not used to store anything else for
|
||||||
|
* ProcessWire. It is also preferable to using a variable (or variables) because it is always
|
||||||
|
* in scope and accessible anywhere in your template files, even within existing functions.
|
||||||
|
*
|
||||||
|
* ~~~~~
|
||||||
|
* // set a setting named “foo” to value “bar”
|
||||||
|
* setting('foo', 'bar');
|
||||||
|
*
|
||||||
|
* // get a setting named “foo”
|
||||||
|
* $value = setting('foo');
|
||||||
|
*
|
||||||
|
* // set or replace multiple settings
|
||||||
|
* setting([
|
||||||
|
* 'foo' => 'value',
|
||||||
|
* 'bar' => 123,
|
||||||
|
* 'baz' => [ 'foo', 'bar', 'baz' ]
|
||||||
|
* ]);
|
||||||
|
*
|
||||||
|
* // get all settings in associative array
|
||||||
|
* $a = setting();
|
||||||
|
*
|
||||||
|
* // to unset a setting
|
||||||
|
* setting(false, 'foo');
|
||||||
|
* ~~~~~
|
||||||
|
*
|
||||||
|
* @param string|array $name Setting name, or array to set multiple
|
||||||
|
* @param string|int|array|float|mixed $value Value to set, or omit if getting value of $name (default=null)
|
||||||
|
* @return array|string|int|bool|mixed|null
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
function setting($name = '', $value = null) {
|
||||||
|
static $settings = [];
|
||||||
|
if($name === '') return $settings;
|
||||||
|
if(is_array($name)) return $settings = array_merge($settings, $name);
|
||||||
|
if($name === false) { unset($settings[(string) $value]); return null; }
|
||||||
|
if($value !== null) $settings[$name] = $value;
|
||||||
|
return isset($settings[$name]) ? $settings[$name] : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user