Location: PHPKode > projects > PHPLayouts > PHPLayouts/libs/Layout.php
<?php

/**
 * Layout Handler
 *
 * @author rodix53 <hide@address.com>
 * @version 0.9.1-beta
 * @package php-layouts
 * @license http://creativecommons.org/licenses/by-nc/2.0/fr/ Creative Commons BY-NC 2.0
 */
 
class Layout
{
    
    /**
     * Define directories where are located layouts files
     * @var string
     */
    private $_layout_dir           =  null;
    
    /**
     * Variable whose says if the layout handler is enabled or not
     * @var bool
     */
    private $_layout_activated     =  false;
    
    /**
     * An array containing all the blocks of the layout
     * @var array
     */
    private $_blocks               =  array();
    
    /**
     * A variable containing the name of the layout
     * @var string
     */
    private $_layout               =  null;
	
	public function __construct($tpl_dir)
	{
        $this->_layout_dir = $tpl_dir;
	}
    
    /**
     * Return if the layout handler is activated or not
     * @return bool
     */
    public function isActivated()
    {
        return (bool) $this->_layout_activated;
    }
    
    /**
     * Enable the layout handler
     */
    public function activate()
    {
        $this->_layout_activated = true;
    }
    
    /**
     * Disable the layout handler
     */
    public function desactivate()
    {
        $this->_layout_activated = false;
    }
    
    /**
     * Set layout directory
     * @param string $layout_dir Folder where the layout file is located
     * @return bool
     */
    public function setLayoutDir($layout_dir)
    {
        if (!is_dir($layout_dir))
		{
            Template::trigger_error('The layout directory provided seems to do not exists', E_USER_WARNING);
            return false;
        }
        $this->_layout_dir = trim($layout_dir, '/') . DIRECTORY_SEPARATOR;
        return true;
    }
    
    /**
     * Get layout directory
     * @return string
     */
    public function getLayoutDir()
    {
        return $this->_layout_dir;
    }
    
    /**
     * Gestion des blocks pour la page finale.
     */
    public function addBlock($name, $file)
    {
        if (!is_file($this->_layout_dir . $file))
        {
            Template::trigger_error('Template file ' . $file . ' doesn\'t exists in the layout directory', E_USER_WARNING);
            return;
        }
        
        $this->_blocks[$name] = array('name'  =>  $name, 'file'  =>  $file, 'content'  =>  null);
    }
    
    /**
     * Delete the specified block
     *
     * @param string $block_name Block name
     */
    public function removeBlock($block_name)
    {
        if (in_array($block_name, $this->_blocks))
        {
            unset($this->_blocks[$block_name]);
        }
    }
    
    /**
     * Returns the contents of the specified block
     * @param string $block_name Block name
     * @return string
     */
    public function getBlock($block_name)
    {
        return $this->_blocks[$block_name]['content'];
    }
    
    /**
     * Return all blocks in an array
     * @return array
     */
    public function getBlocks()
    {
        return (array) $this->_blocks;
    }
    
    /**
     * Return the number of block
     * @return integer
     */
    public function countBlocks()
    {
        return count($this->_blocks);
    }
    
    /**
     * Set the content of a block of the layout
     */
    public function _setContentBlock($name, $content)
    {
        $this->_blocks[$name]['content'] = $content;
    }
    
    /**
     * Return the name of the main layout
     * @return string
     */
    public function getLayout()
    {
        return $this->_layout;
    }
    
    /**
     * Set the name of the main layout
     * @return bool
     */
    public function setLayout($layout)
    {
        if (!is_file($this->_layout_dir . $layout))
        {
            Template::trigger_error('Layout file don\'t exists in layout_dir', E_USER_WARNING);
            return false;
        }
        $this->_layout = $layout;
        return true;
    }
}
Return current item: PHPLayouts