<commit_before><?php

namespace vakata\views;

/**
 * A simple template class.
 */
class View
{
    protected static $dirs = [];
    protected static $data = [];

    protected $template = null;
    protected $sectionData = null;

    protected $layout = null;
    protected $layoutData = [];
    protected $layoutSection = null;
    protected $layoutSections = [];

    /**
     * Add a directory to the template list. The name is used when searching for the template.
     * @method addDir
     * @param string $dir  the directory to add
     * @param string $name the alias of the directory
     * @return self
     */
    public static function addDir($dir, $name = '')
    {
        static::$dirs[$name] = $dir;
    }
    /**
     * Share a single variable or an array of variables in all templates.
     * @method shareData
     * @param string|array $var   the variable name, or an array of variables
     * @param mixed        $value if $var is an array omit this parameter, otherwise this is the value of the variable
     */
    public static function shareData($var, $value = null)
    {
        if (is_array($var) && $value === null) {
            static::$data = array_merge(static::$data, $var);
        } else {
            static::$data[$var] = $value;
        }
    }
    /**
     * Render a template.
     * @method get
     * @param  string $template the template to render
     * @param  array  $data     optional data to use in rendering
     * @return string           the result
     */
    public static function get($template, array $data = [])
    {
        return (new self($template))->render($data);
    }

    /**
     * Create an instance.
     * @method __construct
     * @param  string      $template    the template to be rendered
     * @param  array       $sectionData optional available sections
     */
    public function __construct($template, $sectionData = [])
    {
        $template = explode('::', $template, 2);
        if (!isset($template[1])) {
            array_unshift($template, '');
        }
        if (!isset(static::$dirs[$template[0]])) {
            throw new \Exception('Unknown directory: '.$template[0]);
        }
        $template = static::$dirs[$template[0]].DIRECTORY_SEPARATOR.preg_replace('(\.php$)', '', $template[1]).'.php';
        if (!is_file($template)) {
            throw new \Exception('Template not found: '.$template);
        }
        $this->template = $template;
        $this->sectionData = $sectionData;
    }

    /**
     * Specifies a master template to load the current template in. Available only inside templates.
     * @method layout
     * @param  string $template the master template
     * @param  array  $data     optional data to pass to the template
     */
    protected function layout($template, array $data = [])
    {
        $this->layout = $template;<commit_msg>added name & dir helper functions<commit_after><?php

namespace vakata\views;

/**
 * A simple template class.
 */
class View
{
    protected static $dirs = [];
    protected static $data = [];

    protected $template = null;
    protected $sectionData = null;

    protected $dir = null;
    protected $name = null;
    protected $layout = null;
    protected $layoutData = [];
    protected $layoutSection = null;
    protected $layoutSections = [];

    /**
     * Add a directory to the template list. The name is used when searching for the template.
     * @method addDir
     * @param string $dir  the directory to add
     * @param string $name the alias of the directory
     * @return self
     */
    public static function addDir($dir, $name = '')
    {
        static::$dirs[$name] = $dir;
    }
    /**
     * Share a single variable or an array of variables in all templates.
     * @method shareData
     * @param string|array $var   the variable name, or an array of variables
     * @param mixed        $value if $var is an array omit this parameter, otherwise this is the value of the variable
     */
    public static function shareData($var, $value = null)
    {
        if (is_array($var) && $value === null) {
            static::$data = array_merge(static::$data, $var);
        } else {
            static::$data[$var] = $value;
        }
    }
    /**
     * Render a template.
     * @method get
     * @param  string $template the template to render
     * @param  array  $data     optional data to use in rendering
     * @return string           the result
     */
    public static function get($template, array $data = [])
    {
        return (new self($template))->render($data);
    }

    /**
     * Create an instance.
     * @method __construct
     * @param  string      $template    the template to be rendered
     * @param  array       $sectionData optional available sections
     */
    public function __construct($template, $sectionData = [])
    {
        $template = explode('::', $template, 2);
        if (!isset($template[1])) {
            array_unshift($template, '');
        }
        list($this->dir, $this->name) = $template;
        if (!isset(static::$dirs[$template[0]])) {
            throw new \Exception('Unknown directory: '.$template[0]);
        }
        $template = static::$dirs[$template[0]].DIRECTORY_SEPARATOR.preg_replace('(\.php$)', '', $template[1]).'.php';
        if (!is_file($template)) {
            throw new \Exception('Template not found: '.$template);
        }
        $this->template = $template;
        $this->sectionData = $sectionData;
    }

    /**
     * Get the current template directory handle
     * @method dir
     * @return string directory_handle
     */
    protected function dir()
    {
        return $this->dir;
    }
    /**
     * Get the current template short name
     * @method name
     * @return string name
     */
    protected function name()
    {
        return $this->name;
    }

    /**
     * Specifies a master template to load the current template in. Available only inside templates.
     * @method layout
     * @param  string $template the master template
     * @param  array  $data     optional data to pass to the template
     */
    protected function layout($template, array $data = [])
    {
        $this->layout = $template;
{'fraction_non_alphanumeric': 0.12371630295250322, 'fraction_numerical': 0.0019255455712451862, 'mean_word_length': 2.251434533124674, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '5579195', 'n_tokens_mistral': 1698, 'n_tokens_neox': 1697, 'n_words': 560}