View.php 2.03 KB
Newer Older
1 2
<?php

Tino Goratsch's avatar
Tino Goratsch committed
3 4 5 6 7
/**
 * Copyright (c) by the ACP3 Developers.
 * See the LICENSE file at the top-level module directory for licensing details.
 */

8 9
namespace ACP3\Core;

10
use ACP3\Core\View\Renderer\RendererInterface;
11

12
class View
13 14
{
    /**
15
     * @var RendererInterface
16 17 18 19
     */
    protected $renderer;

    /**
20
     * Gets the renderer.
21
     *
22
     * @return RendererInterface
23 24 25 26 27 28 29
     */
    public function getRenderer()
    {
        return $this->renderer;
    }

    /**
30
     * View constructor.
Tino Goratsch's avatar
Tino Goratsch committed
31
     *
32
     * @param \ACP3\Core\View\Renderer\RendererInterface $renderer
33
     */
34
    public function __construct(RendererInterface $renderer)
35
    {
36
        $this->renderer = $renderer;
37 38 39
    }

    /**
40
     * Fetches a template and outputs its contents.
41
     *
42
     * @param string $template
43
     */
44
    public function displayTemplate($template)
45
    {
46
        $this->renderer->display('asset:' . $template);
47 48 49
    }

    /**
50
     * Fetches a template and returns its contents.
51
     *
52
     * @param string $template
53 54 55
     *
     * @return string
     */
56
    public function fetchTemplate($template)
57
    {
58
        return $this->renderer->fetch('asset:' . $template);
59 60
    }

61 62 63 64 65 66 67 68 69 70 71 72 73 74
    /**
     * @param string $template
     *
     * @return string
     */
    public function fetchStringAsTemplate($template)
    {
        try {
            return $this->renderer->fetch('string:' . $template);
        } catch (\Exception $e) {
            return $template; // fail silently when invalid statements have been given in the string template
        }
    }

75
    /**
76
     * Checks, whether a template exists or not.
77 78 79
     *
     * @param string $template
     *
80
     * @return bool
81 82 83
     */
    public function templateExists($template)
    {
84
        return $this->renderer->templateExists('asset:' . $template);
85 86 87
    }

    /**
88
     * Assigns a new template variable.
89
     *
Tino Goratsch's avatar
Tino Goratsch committed
90
     * @param string|array $name
Tino Goratsch's avatar
Tino Goratsch committed
91
     * @param mixed        $value
92
     *
93
     * @return $this
94 95 96
     */
    public function assign($name, $value = null)
    {
97 98 99
        $this->renderer->assign($name, $value);

        return $this;
100 101
    }
}