<commit_before><?php

/**
 * This file is part of the Miny framework.
 * (c) <NAME> <<EMAIL>>
 *
 * For licensing information see the LICENSE file.
 */

namespace Modules\Templating;

use Miny\Controller\Controller as ControllerBase;
use Miny\HTTP\Request;
use Miny\HTTP\Response;

class Controller extends ControllerBase
{
    private $layout_map;
    private $current_layout;
    private $template_variables = array();

    protected function initLayouts()
    {
        //need to be overridden
        //[action => template]
        return array();
    }

    protected function init()
    {
        $this->layout_map = $this->initLayouts();
    }

    public function layout($template)
    {
        //TODO: check
        $this->current_layout = $template;
    }

    public function assign($key, $value)
    {
        $this->template_variables[$key] = $value;
    }

    public function clean()
    {
        $this->template_variables = array();
    }

    protected function renderLayout()
    {
        if (isset($this->current_layout)) {
            $loader = $this->service('template_loader');
            $layout = $loader->load($this->current_layout);
            $layout->set($this->template_variables);
            echo $layout->render();
        }
    }

    protected function actionHasLayout($action)
    {
        return isset($this->layout_map[$action]);
    }

    protected function getLayoutForAction($action)
    {
        return $this->layout_map[$action];
    }

    public function run($action, Request $request, Response $response)
    {
        $action = $action ? : $this->default_action;
        if ($this->actionHasLayout($action)) {
            $this->layout($this->getLayoutForAction($action));
        }
        parent::run($action, $request, $response);
        $this->renderLayout();
    }
}<commit_msg>Option to inhibit rendering of template.<commit_after><?php

/**
 * This file is part of the Miny framework.
 * (c) <NAME> <<EMAIL>>
 *
 * For licensing information see the LICENSE file.
 */

namespace Modules\Templating;

use Miny\Controller\Controller as ControllerBase;
use Miny\HTTP\Request;
use Miny\HTTP\Response;

class Controller extends ControllerBase
{
    private $layout_map;
    private $current_layout;
    private $template_variables = array();

    protected function initLayouts()
    {
        //need to be overridden
        //[action => template]
        return array();
    }

    protected function init()
    {
        $this->layout_map = $this->initLayouts();
    }

    public function layout($template)
    {
        //TODO: check
        $this->current_layout = $template;
    }

    public function assign($key, $value)
    {
        $this->template_variables[$key] = $value;
    }

    public function clean()
    {
        $this->template_variables = array();
    }

    protected function renderLayout()
    {
        if (isset($this->current_layout)) {
            $loader = $this->service('template_loader');
            $layout = $loader->load($this->current_layout);
            $layout->set($this->template_variables);
            echo $layout->render();
        }
    }

    protected function actionHasLayout($action)
    {
        return isset($this->layout_map[$action]);
    }

    protected function getLayoutForAction($action)
    {
        return $this->layout_map[$action];
    }

    public function run($action, Request $request, Response $response)
    {
        $action = $action ? : $this->default_action;
        if ($this->actionHasLayout($action)) {
            $this->layout($this->getLayoutForAction($action));
        }
        if (parent::run($action, $request, $response) !== false) {
            $this->renderLayout();
        }
    }
}
{'fraction_non_alphanumeric': 0.1342084114653094, 'fraction_numerical': 0.0, 'mean_word_length': 2.4897196261682244, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3459848', 'n_tokens_mistral': 1056, 'n_tokens_neox': 1035, 'n_words': 233}