Smarty 변수가 이미 할당되었는지 확인하려면 어떻게해야합니까?
-
20-08-2019 - |
문제
특정 값이 이미 Smarty에 할당되었고 (기본값) 값을 할당하지 않은지 확인하려면 어떻게해야합니까?
대답:
if ($this->cismarty->get_template_vars('test') === null) {
$this->cismarty->assign('test', 'Default value');
}
해결책
Smarty 2
if ($smarty->get_template_vars('foo') === null)
{
$smarty->assign('foo', 'some value');
}
Smarty 3
if ($smarty->getTemplateVars('foo') === null)
{
$smarty->assign('foo', 'some value');
}
주목하십시오 Smarty 3, 당신은 사용해야합니다 $smarty->getTemplateVars
대신에.
다른 팁
get_template_vars()
변수를 설정하지 않으면 NULL을 반환합니다.
if ($smarty->get_template_vars('test') === null) {
echo "'test' is not assigned or is null";
}
그러나 변수가 할당되었지만 NULL로 설정하면 수표가 실패합니다.
$tmp = $smarty->get_template_vars();
if (!array_key_exists('test', $tmp)) {
echo "'test' is not assigned";
}
당신이 할 수 있다고 확신합니다 :
if (!isset($smarty['foo']))
{
$smarty->assign('foo', 'some value');
}
제휴하지 않습니다 StackOverflow