PHP Classes

File: src/App/View/ViewTrait.php

Recommend this page to a friend!
  Packages of Thierry Feuzeu   Jaxon   src/App/View/ViewTrait.php   Download  
File: src/App/View/ViewTrait.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Jaxon
Call PHP classes from JavaScript using AJAX
Author: By
Last change:
Date: 1 year ago
Size: 1,643 bytes
 

 

Contents

Class file image Download
<?php

namespace Jaxon\App\View;

use function ltrim;
use function rtrim;

trait ViewTrait
{
    /**
     * The template directories
     *
     * @var array
     */
    protected $aDirectories = [];

    /**
     * The directory of the current template
     *
     * @var string
     */
    protected $sDirectory = '';

    /**
     * The extension of the current template
     *
     * @var string
     */
    protected $sExtension = '';

    /**
     * Add a namespace to the view renderer
     *
     * @param string $sNamespace    The namespace name
     * @param string $sDirectory    The namespace directory
     * @param string $sExtension    The extension to append to template names
     *
     * @return void
     */
    public function addNamespace(string $sNamespace, string $sDirectory, string $sExtension = ''): void
    {
        $this->aDirectories[$sNamespace] = ['path' => $sDirectory, 'ext' => $sExtension];
    }

    /**
     * Set the namespace of the template being rendered
     *
     * @param string $sNamespace    The namespace name
     *
     * @return void
     */
    public function setCurrentNamespace(string $sNamespace): void
    {
        $this->sDirectory = '';
        $this->sExtension = '';
        if(isset($this->aDirectories[$sNamespace]))
        {
            // Make sure there's only one '/' at the end of the string
            $this->sDirectory = rtrim($this->aDirectories[$sNamespace]['path'], '/') . '/';
            // Make sure there's only one '.' at the beginning of the string
            $this->sExtension = '.' . ltrim($this->aDirectories[$sNamespace]['ext'], '.');
        }
    }
}