Skip to content

Commit 7a0c20b

Browse files
committed
Working on the HtmlBuilder component
1 parent a9615d7 commit 7a0c20b

File tree

5 files changed

+73
-15
lines changed

5 files changed

+73
-15
lines changed

app/Components/HtmlBuilder.php

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,66 @@
22

33
namespace TeachMe\Components;
44

5+
use Illuminate\Contracts\Config\Repository as Config;
6+
use Illuminate\Contracts\View\Factory as View;
57
use Collective\Html\HtmlBuilder as CollectiveHtmlBuilder;
8+
use Illuminate\Routing\UrlGenerator;
69

710
class HtmlBuilder extends CollectiveHtmlBuilder {
811

9-
public function menu()
12+
/**
13+
* @var Config
14+
*/
15+
private $config;
16+
/**
17+
* @var View
18+
*/
19+
private $view;
20+
21+
public function __construct(Config $config, View $view, UrlGenerator $url)
22+
{
23+
$this->config = $config;
24+
$this->view = $view;
25+
$this->url = $url;
26+
}
27+
28+
public function menu($items)
29+
{
30+
if ( ! is_array($items))
31+
{
32+
$items = $this->config->get($items, array());
33+
}
34+
35+
return $this->view->make('partials/menu', compact('items'));
36+
}
37+
38+
/**
39+
* Builds an HTML class attribute dynamically
40+
* Usage:
41+
* {!! Html::classes(['home' => true, 'main', 'dont-use-this' => false]) !!}
42+
* Returns:
43+
* class="home main".
44+
*
45+
* @param array $classes
46+
*
47+
* @return string
48+
*/
49+
public function classes(array $classes)
1050
{
11-
return view('partials/menu');
51+
$html = '';
52+
foreach ($classes as $name => $bool) {
53+
if (is_int($name)) {
54+
$name = $bool;
55+
$bool = true;
56+
}
57+
if ($bool) {
58+
$html .= $name.' ';
59+
}
60+
}
61+
if (! empty($html)) {
62+
return ' class="'.trim($html).'"';
63+
}
64+
return '';
1265
}
1366

1467
}

app/Providers/HtmlServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ protected function registerHtmlBuilder()
1515
{
1616
$this->app->bindShared('html', function($app)
1717
{
18-
return new HtmlBuilder($app['url']);
18+
return new HtmlBuilder($app['config'], $app['view'], $app['url']);
1919
});
2020
}
2121

config/teachme.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
return array(
4+
5+
'menu' => array(
6+
'tickets.latest' => 'Recientes',
7+
'tickets.popular' => 'Populares',
8+
'tickets.open' => 'Abiertas',
9+
'tickets.closed' => 'Finalizadas'
10+
)
11+
12+
);

resources/views/layout.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
4040

41-
{!! Html::menu() !!}
41+
{!! Html::menu('teachme.menu') !!}
4242

4343
<ul class="nav navbar-nav navbar-right">
4444
<li class="dropdown">
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
<ul class="nav navbar-nav">
2-
<li role="presentation">
3-
<a href="http://teachme.dev/">Recientes</a>
4-
</li>
5-
<li role="presentation">
6-
<a href="http://teachme.dev/populares">Populares</a>
7-
</li>
8-
<li role="presentation">
9-
<a href="http://teachme.dev/pendientes">Abiertas</a>
10-
</li>
11-
<li role="presentation">
12-
<a href="http://teachme.dev/tutoriales">Finalizadas</a>
2+
@foreach ($items as $route => $text)
3+
<li role="presentation" {!! Html::classes(['active' => Route::is($route)]) !!}>
4+
<a href="{{ route($route) }}">{{ $text }}</a>
135
</li>
6+
@endforeach
147
</ul>

0 commit comments

Comments
 (0)