Completed
Push — master ( d0681a...916c2c )
by Mikael
02:18
created

THelpers::contentForRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Anax\View;
4
5
/**
6
 * Trait with view helpers, provided by the ThemeEngine to the views.
7
 *
8
 */
9
trait THelpers
10
{
11
    /**
12
     * Render a view with an optional data set of variables.
13
     *
14
     * @param string $template the template file, or array
15
     * @param array  $data     variables to make available to the
16
     *                         view, default is empty
17
     *
18
     * @return void
19
     */
20
    public function renderView($template, $data = [])
21
    {
22
        $template = $this->di->get("views")->getTemplateFile($template);
0 ignored issues
show
Bug introduced by
The property di does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
23
        $view = $this->di->get("view");
24
        $view->setDI($this->di);
25
        $view->set($template, $data);
26
        $view->render();
27
    }
28
29
30
31
    /**
32
     * Create a class attribute from a string or array.
33
     *
34
     * @param string|array $args variable amount of classlists.
35
     *
36
     * @return string as complete class attribute
37
     */
38 5
    public function classList(...$args)
39
    {
40 5
        $classes = [];
41
42 5
        foreach ($args as $arg) {
43 5
            if (empty($arg)) {
44 2
                continue;
45 4
            } elseif (is_string($arg)) {
46 4
                $arg = explode(" ", $arg);
47 4
            }
48 4
            $classes = array_merge($classes, $arg);
49 5
        }
50
51 5
        return "class=\"" . implode(" ", $classes) . "\"";
52
    }
53
54
55
56
    /**
57
     * Create an url for a static asset.
58
     *
59
     * @param string $uri part of uri to use when creating an url.
60
     *
61
     * @return string as resulting url.
62
     */
63
    public function asset($uri = null)
64
    {
65
        return $this->di->get("url")->asset($uri);
66
    }
67
68
69
70
    /**
71
     * Create an url and prepending the baseUrl.
72
     *
73
     * @param string $uri part of uri to use when creating an url. "" or null
74
     *                    means baseurl to current frontcontroller.
75
     *
76
     * @return string as resulting url.
77
     */
78
    public function url($uri = null)
79
    {
80
        return $this->di->get("url")->create($uri);
81
    }
82
83
84
85
    /**
86
     * Check if the region in the view container has views to render.
87
     *
88
     * @param string $region to check
89
     *
90
     * @return boolean true or false
91
     */
92
    public function regionHasContent($region)
93
    {
94
        return $this->di->get("views")->hasContent($region);
95
    }
96
97
98
99
    /**
100
     * Render views, from the view container, in the region.
101
     *
102
     * @param string $region to render in
103
     *
104
     * @return boolean true or false
105
     */
106
    public function renderRegion($region)
107
    {
108
        $this->di->get("views")->render($region);
109
    }
110
111
112
113
    /**
114
     * Load content from a route and return details to view.
115
     *
116
     * @param string $route to load content from.
117
     *
118
     * @return array with values to extract in view.
119
     */
120
    public function contentForRoute($route)
121
    {
122
        $content = $this->di->get("content")->contentForRoute($route);
123
        return $content->views["main"]["data"];
124
    }
125
}
126