Adding if statement in templates

This commit is contained in:
jerome 2014-04-19 00:01:14 +02:00
parent 9c56b73db9
commit 69e3b85516
3 changed files with 19 additions and 3 deletions

View File

@ -86,6 +86,7 @@ class LogController extends AbstractServerController {
'server_log_entries',
array(
'logtitle' => $key,
'has_users' => ($key == 'status') ? false : true,
)
);
$this->tpl->addTemplateData(

View File

@ -118,6 +118,10 @@ class Template {
}
$source = $this->addTemplateData($source, $subdata, true);
} else {
// replace if statements
$if_replacement = empty($value) ? '' : '$1';
$source = preg_replace('{<!--\?'.$key.'-->(.*?)<!--\?\?'.$key.'-->}is', $if_replacement, $source);
$source = str_replace('{'.$key.'}', $value, $source);
}
}
@ -202,10 +206,18 @@ class Template {
$tmp_string = str_replace('{'.$k.'}', $repeat_html, $tmp_string);
} else {
foreach($v as $vk => $vv) {
// replace if statements
$if_replacement = empty($vv) ? '' : '$1';
$tmp_string = preg_replace('{<!--\?'.$k.'_'.$vk.'-->(.*?)<!--\?\?'.$k.'_'.$vk.'-->}is', $if_replacement, $tmp_string);
$tmp_string = str_replace('{'.$k.'_'.$vk.'}', $vv, $tmp_string);
}
}
} else {
// replace if statements
$if_replacement = empty($v) ? '' : '$1';
$tmp_string = preg_replace('{<!--\?'.$k.'-->(.*?)<!--\?\?'.$k.'-->}is', $if_replacement, $tmp_string);
$tmp_string = str_replace('{'.$k.'}', $v, $tmp_string);
}
}
@ -225,6 +237,9 @@ class Template {
// check if there are any unused tpl_repeat templates, and if there are remove them
$result = preg_replace('{<!--%(.+?)-->(.*?)<!--%%\\1-->}is', '', $this->templates[$id]);
// check if there are any unused if, and if there are remove them
$result = preg_replace('{<!--\?(.+?)-->(.*?)<!--\?\?\\1-->}is', '', $result);
// check for tpl variables that have not been replaced. ie: {name}. ignore literal stuff, though. ie: {{name}} is {name} and should not be removed
preg_match_all('~{?{(\w+?)}}?~', $result, $matches);

View File

@ -26,14 +26,14 @@
<col />
<col />
<col style="width: 135px" />
<col />
<!--?has_users--><col /><!--??has_users-->
</colgroup>
<thead>
<tr>
<th>{label_server}</th>
<th>{label_message}</th>
<th>{label_date}</th>
<th>{label_users}</th>
<!--?has_users--><th>{label_users}</th><!--??has_users-->
</tr>
</thead>
<tbody>
@ -42,7 +42,7 @@
<td>{server}</td>
<td>{message}</td>
<td>{datetime_format}</td>
<td>{users}</td>
<!--?has_users--><td>{users}</td><!--??has_users-->
</tr>
<!--%%tpl_repeat_entries-->
{entries}