config files
| From: | Bob Mroczka | Date: | Fri, 03 May 2002 23:21:15 +0000 |
| Subject: | config files | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-4047@lists.php.net to get a copy of this message | ||
I found there is a limit to what is considered
a valid config file variable.
I tried creating a config file section to hold
a section containing links for a nav bar like
this:
[navlinks]
About Us = about.php
Site Map = sitemap.php
etc...
Since the keys have a space in them I was
unable to create my nav bar from them.
Incidently, I created a custom function to create
a template array variable based on the contents
of a config file section. It may prove useful to
others so here it is:
<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* Type: function
* Name: config_load_array
* Version: 1.0
* Purpose: load an array from key/value pairs in a config file section
* Input: file = name of config file
* section = name of config file section
* var = name of array variable to load
* -------------------------------------------------------------
*/
function smarty_function_config_load_array($params, &$smarty)
{
extract($params);
if (empty($file)) {
$smarty->trigger_error("config_load_array: missing 'file'
parameter");
return;
}
if (empty($section)) {
$smarty->trigger_error("config_load_array: missing 'section'
parameter");
return;
}
if (empty($var)) {
$smarty->trigger_error("config_load_array: missing 'var' parameter");
return;
}
if ($smarty->_conf_obj === null ||
!isset($smarty->_config[0]['files'][$file])) {
$smarty->_config_load($file,$section);
}
$smarty->assign($var, $smarty->_conf_obj->get($file,$section));
}
/* vim: set expandtab: */
?>
Here is how I call it from my template (my delimiters were changed
to appease Dreamweaver):
<{config_load_array file="web.conf" section="navlinks"
var="navlinks"}>
<{foreach name=links from=$navlinks key=text item=href}>
<{if $smarty.foreach.links.last}>
<a href="<{$href}>" class="toprow"><{$text}></a>
<{else}>
<a href="<{$href}>" class="toprow"><{$text}></a>
<font size="2">|</font>
<{/if}>
<{/foreach}>
To make my nav bar work I had to make two changes to Config_File.class.php
to permit spaces in config file variables.
Line 289 was changed from:
if (preg_match("/^\s*(\.?\w+)\s*=(.*)/", $line, $match)) {
to:
if (preg_match("/^([^=\n]+)=(.*)/", $line, $match)) {
and I commented out the 4 lines that check for a
bad variable name (308-311)
Bob