2017-10-05 80 views
-2

反正有沒有可以簡化?類內回聲

<?php 
    $menu = JSite::getMenu(); 
    $arr = (array)$menu->getActive(); 
    $alias = $arr['alias']; 

    if ($alias == "accueil") { 
     echo('<h2 class="moduletitle"><span>title 1</h2></span>'); 
    } 
    elseif ($alias == "welcome") { 
     echo('<h2 class="moduletitle"><span>title 2</h2></span>'); 
    } 
    else { 
     echo "<h1 class='moduletitle'><span>"; 
     echo $this->escape($this->params->get('page_title')); 
     echo "</span></h1>"; 
    } 
?> 
+3

如果這是一個工作的代碼,你可以試試[**代碼審查**](https://開頭codereview.stackexchange.com/) – Swellar

+0

您從未解釋過上述代碼的功能。我們能幫你什麼嗎?這個問題也不屬於這裏。檢查代碼審查@Swellar注意到 – Akintunde007

+0

這應該是關閉的,因爲它不清楚OP要求什麼 –

回答

1

至少試着改用開關很多的if else:

<?php 
$menu = JSite::getMenu(); 
$arr = (array)$menu->getActive(); 
$alias = $arr['alias']; 

$headingType = "h1"; 

switch ($alias) { 
    case 'accueil': 
     $headingType = "h2"; 
     $title = "title 1"; 
     break; 

    case 'welcome': 
     $headingType = "h2"; 
     $title = "title 2"; 
     break; 

    default: 
     $title = $this->escape($this->params->get('page_title')); 
} 

echo "<" . $headingType . " class=\"moduletitle\"><span>" . 
$title . 
"</span></" . $headingType . ">";