[MVC] Aanbeland bij het genereren van mijn menu
ik heb natuurlijk een model menu.mdl.php waar in ik alle menu items ophaal en als objecten in een array opsla, die array geef ik dan mee aan mijn view.
Echter lijkt me dat er nu iets te veel code moet komen in de view om alles netjes en correct weer te geven, even een voorbeeld van hoe het (nu al) eruit ziet in mijn view.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
$menuOutput = null;
foreach($menu as $item){
// Check if we link to an internal page or to an external website url
if (empty($item -> targetURL)) {
// Check which is our target, blank true or false
if ($item -> targetBlank) {
$menuOutput .= '<li>';
$menuOutput .= '<a href="'. SITE_URL . $item -> title .'" target="_blank">'. $item -> title .'</a>';
$menuOutput .= '</li>';
} else {
$menuOutput .= '<li>';
$menuOutput .= '<a href="'. SITE_URL . $item -> title .'">'. $item -> title .'</a>';
$menuOutput .= '</li>';
}
} else {
// Check which is our target, blank true or false
if ($item -> targetBlank) {
$menuOutput .= '<li>';
$menuOutput .= '<a href="'. $item -> targetURL .'" target="_blank">'. $item -> title .'</a>';
$menuOutput .= '</li>';
} else {
$menuOutput .= '<li>';
$menuOutput .= '<a href="'. $item -> targetURL .'">'. $item -> title .'</a>';
$menuOutput .= '</li>';
}
}
}
echo '<ul>' . $menuOutput . '</ul>';
?>
$menuOutput = null;
foreach($menu as $item){
// Check if we link to an internal page or to an external website url
if (empty($item -> targetURL)) {
// Check which is our target, blank true or false
if ($item -> targetBlank) {
$menuOutput .= '<li>';
$menuOutput .= '<a href="'. SITE_URL . $item -> title .'" target="_blank">'. $item -> title .'</a>';
$menuOutput .= '</li>';
} else {
$menuOutput .= '<li>';
$menuOutput .= '<a href="'. SITE_URL . $item -> title .'">'. $item -> title .'</a>';
$menuOutput .= '</li>';
}
} else {
// Check which is our target, blank true or false
if ($item -> targetBlank) {
$menuOutput .= '<li>';
$menuOutput .= '<a href="'. $item -> targetURL .'" target="_blank">'. $item -> title .'</a>';
$menuOutput .= '</li>';
} else {
$menuOutput .= '<li>';
$menuOutput .= '<a href="'. $item -> targetURL .'">'. $item -> title .'</a>';
$menuOutput .= '</li>';
}
}
}
echo '<ul>' . $menuOutput . '</ul>';
?>
En dan moet er nog een hele hoop gebeuren. Waar zou ik dit stuk nog kunnen plaatsen of hoe zou ik het kunnen oplossen? Om dit ook in de model te stelen zie ik niet zitten aangezien er toch wat html in voorkomt...
Bedankt alvast ;)
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
$menuOutput = null;
$a = null;
foreach ($menu as $item) {
// Check if we link to an internal page or to an external website url
if (empty($item->targetURL)) {
// Check which is our target, blank true or false
if ($item->targetBlank) {
$a = '<a href="' . SITE_URL . $item->title . '" target="_blank">';
}
else {
$a = '<a href="' . SITE_URL . $item->title . '">';
}
}
else {
// Check which is our target, blank true or false
if ($item->targetBlank) {
$a = '<a href="' . $item->targetURL . '" target="_blank">';
}
else {
$a = '<a href="' . $item->targetURL . '">';
}
}
}
$menuOutput .= '<li>';
$menuOutput .= $a . $item->title . '</a>';
$menuOutput .= '</li>';
echo '<ul>' . $menuOutput . '</ul>';
?>
$menuOutput = null;
$a = null;
foreach ($menu as $item) {
// Check if we link to an internal page or to an external website url
if (empty($item->targetURL)) {
// Check which is our target, blank true or false
if ($item->targetBlank) {
$a = '<a href="' . SITE_URL . $item->title . '" target="_blank">';
}
else {
$a = '<a href="' . SITE_URL . $item->title . '">';
}
}
else {
// Check which is our target, blank true or false
if ($item->targetBlank) {
$a = '<a href="' . $item->targetURL . '" target="_blank">';
}
else {
$a = '<a href="' . $item->targetURL . '">';
}
}
}
$menuOutput .= '<li>';
$menuOutput .= $a . $item->title . '</a>';
$menuOutput .= '</li>';
echo '<ul>' . $menuOutput . '</ul>';
?>
Jan Koehoorn schreef op 15.07.2008 22:11:
Is het zo niet simpeler:
...
...
Maakt het toch wel een stukje korter en vooral overzichtelijker weer inderdaad, bedankt!!! Zou je dit op deze manier wel in een controller of model durven gooien? (al zou model eigenlijk soieso een slecht idee zijn aangezien dat alleen voor het beheren van data is)
Of wat zou een goede oplossing zijn?
Uit je model rolt op een gegeven moment de variabele $menu. Deze wordt vervolgens door een controller doorgegeven en de view zorgt er vervolgens voor dat de inhoud op de juiste manier weergegeven wordt.
Jan Koehoorn schreef op 15.07.2008 22:11:
Is het zo niet simpeler:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
$menuOutput = null;
$a = null;
foreach ($menu as $item) {
// Check if we link to an internal page or to an external website url
if (empty($item->targetURL)) {
// Check which is our target, blank true or false
if ($item->targetBlank) {
$a = '<a href="' . SITE_URL . $item->title . '" target="_blank">';
}
else {
$a = '<a href="' . SITE_URL . $item->title . '">';
}
}
else {
// Check which is our target, blank true or false
if ($item->targetBlank) {
$a = '<a href="' . $item->targetURL . '" target="_blank">';
}
else {
$a = '<a href="' . $item->targetURL . '">';
}
}
}
$menuOutput .= '<li>';
$menuOutput .= $a . $item->title . '</a>';
$menuOutput .= '</li>';
echo '<ul>' . $menuOutput . '</ul>';
?>
$menuOutput = null;
$a = null;
foreach ($menu as $item) {
// Check if we link to an internal page or to an external website url
if (empty($item->targetURL)) {
// Check which is our target, blank true or false
if ($item->targetBlank) {
$a = '<a href="' . SITE_URL . $item->title . '" target="_blank">';
}
else {
$a = '<a href="' . SITE_URL . $item->title . '">';
}
}
else {
// Check which is our target, blank true or false
if ($item->targetBlank) {
$a = '<a href="' . $item->targetURL . '" target="_blank">';
}
else {
$a = '<a href="' . $item->targetURL . '">';
}
}
}
$menuOutput .= '<li>';
$menuOutput .= $a . $item->title . '</a>';
$menuOutput .= '</li>';
echo '<ul>' . $menuOutput . '</ul>';
?>
Kan nog korter.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
$menuOutput = null;
$a = null;
foreach ($menu as $item) {
// Check which is our target, blank true or false
// Check if we link to an internal page or to an external website url, what we do with the ternary operator
if ($item->targetBlank) {
$a = '<a href="' . ((empty($item->targetURL)) ? SITE_URL : $item->targetURL) . $item->title . '" target="_blank">';
}
else {
$a = '<a href="' . ((empty($item->targetURL)) ? SITE_URL : $item->targetURL) . $item->title . '">';
}
}
$menuOutput .= '<li>';
$menuOutput .= $a . $item->title . '</a>';
$menuOutput .= '</li>';
echo '<ul>' . $menuOutput . '</ul>';
?>
$menuOutput = null;
$a = null;
foreach ($menu as $item) {
// Check which is our target, blank true or false
// Check if we link to an internal page or to an external website url, what we do with the ternary operator
if ($item->targetBlank) {
$a = '<a href="' . ((empty($item->targetURL)) ? SITE_URL : $item->targetURL) . $item->title . '" target="_blank">';
}
else {
$a = '<a href="' . ((empty($item->targetURL)) ? SITE_URL : $item->targetURL) . $item->title . '">';
}
}
$menuOutput .= '<li>';
$menuOutput .= $a . $item->title . '</a>';
$menuOutput .= '</li>';
echo '<ul>' . $menuOutput . '</ul>';
?>
Volgens mij ook een mooi voorbeeld van waar je de ternary operator kan gebruiken :-).
Zoals het nu is:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$menuOutput = null;
$a = null;
foreach ($p_aData as $item) {
// Check which is our target, blank true or false
// Check if we link to an internal page or to an external url, what we do with the ternary operator
$item -> targetBlank ? $param = 'target="_blank"' : $param = null;
$a = '<a href="' . ((empty($item -> targetURL)) ? SITE_URL : $item -> targetURL) . $item -> title . '" '. $param .'>';
$menuOutput .= '<li>';
$menuOutput .= $a . $item -> title . '</a>';
$menuOutput .= '</li>';
}
echo '<ul>' . $menuOutput . '</ul>';
?>
$menuOutput = null;
$a = null;
foreach ($p_aData as $item) {
// Check which is our target, blank true or false
// Check if we link to an internal page or to an external url, what we do with the ternary operator
$item -> targetBlank ? $param = 'target="_blank"' : $param = null;
$a = '<a href="' . ((empty($item -> targetURL)) ? SITE_URL : $item -> targetURL) . $item -> title . '" '. $param .'>';
$menuOutput .= '<li>';
$menuOutput .= $a . $item -> title . '</a>';
$menuOutput .= '</li>';
}
echo '<ul>' . $menuOutput . '</ul>';
?>
Gewijzigd op 01/01/1970 01:00:00 door Sjoerd