Skip to content

Using BladeOne with YAF

Jorge Castro edited this page May 10, 2020 · 2 revisions

Editing bootstrap.php

Add the next method to the bootstrap class

(📄 application/Bootstrap.php)

    public function _initBladeOne(Yaf_Dispatcher $dispatcher) {
        $bladeOne = new BladeOne_Adapter(null, Yaf_Registry::get("config")->get("BladeOne"));
        $dispatcher->setView($bladeOne);
    }

Creating an adapter

1- In the folder 📁 application/library, create a folder called Bladeone: 📁 application/library/BladeOne

2- Copy 📄 BladeOne.php (https://github.com/EFTEC/BladeOne) in this new folder.

3- Inside this new folder, create the next adapter (📄 Adapter.php)

(📄 application/library/BladeOne/Adapter.php)

<?php /** @noinspection PhpDocSignatureInspection */

/** @noinspection PhpHierarchyChecksInspection */

use eftec\bladeone\BladeOne;

require 'BladeOne.php';

class BladeOne_Adapter implements Yaf_View_Interface
{
    /**
     * Smarty object
     * @var BladeOne
     */
    public $_bladeOne;
    
    public $_variables;

    /**
     * Constructor
     *
     * @param string $tmplPath
     * @param array $extraParams
     * @return void
     */
    public function __construct($tmplPath = null, $extraParams = array()) {
        $this->_bladeOne = new BladeOne(__DIR__.'/../../views'
            ,__DIR__.'/../../compiles');
        $this->_bladeOne->setFileExtension('.phtml');
        //$this->_bladeOne->setMode(BladeOne::MODE_DEBUG);
        if (null !== $tmplPath) {
            $this->setScriptPath($tmplPath);
        }
        $this->_variables=[];

        /*foreach ($extraParams as $key => $value) {
            $this->_bladeOne->$key = $value;
        }
        */
    }

    /**
     * Set the path to the templates
     *
     * @param string $path The directory to set as the path.
     * @return void
     */
    public function setScriptPath($path)
    {
        if (is_readable($path)) {
            throw new Exception("Not implemented");
            //$this->_bladeOne->template_dir = $path;
            return;
        }

        throw new Exception('Invalid path provided');
    }

    /**
     * Assign a variable to the template
     *
     * @param string $key The variable name.
     * @param mixed $val The variable value.
     * @return void
     */
    public function __set($key, $val)
    {
        $this->_variables[$key]=$val;
    }

    /**
     * Allows testing with empty() and isset() to work
     *
     * @param string $key
     * @return boolean
     */
    public function __isset($key)
    {
        throw new Exception("Not implemented");
       // return (null !== $this->_bladeOne->get_template_vars($key));
    }

    /**
     * Allows unset() on object properties to work
     *
     * @param string $key
     * @return void
     */
    public function __unset($key)
    {
        unset($this->_variables[$key]);
    }

    /**
     * Assign variables to the template
     *
     * Allows setting a specific key to the specified value, OR passing
     * an array of key => value pairs to set en masse.
     *
     * @see __set()
     * @param string|array $spec The assignment strategy to use (key or
     * array of key => value pairs)
     * @param mixed $value (Optional) If assigning a named variable,
     * use this as the value.
     * @return void
     */
    public function assign($spec, $value = null) {
        if (is_array($spec)) {
            $this->_variables=array_merge($this->_variables,$spec);
        } else {
            $this->_variables[$spec]=$value;
        }
    }

    /**
     * Clear all assigned variables
     *
     * Clears all variables assigned to Yaf_View either via
     * {@link assign()} or property overloading
     * ({@link __get()}/{@link __set()}).
     *
     * @return void
     */
    public function clearVars() {
        $this->_variables=[];
    }

    /**
     * Processes a template and returns the output.
     *
     * @param string $name The template to process.
     * @return string The output.
     */
    public function render($name, $value = NULL) {
        
        $name= str_replace(array('\\','/', '.phtml'), array('.','.', ''), $name);
        return $this->_bladeOne->run($name,$this->_variables);
    }

    public function display($name, $value = NULL) {
        $name= str_replace(array('\\','/', '.phtml'), array('.','.', ''), $name);

        echo $this->_bladeOne->run($name,$this->_variables);
    }

    function getScriptPath() {
        throw new Exception("Not implemented");
    }
}
?>

What it does?

  • YAG uses the notation folder/folder/view.phtml while BladeOne uses the notation folder.folder.view. So it is replaced
  • BladeOne does not allow to add variables but when it is executed. So we create a new variable container in the adapter.
  • BladeOne creates a folder called compiles. If not, then you must create manually. The folder compiles must be in the application folder.

📁application
.....📁compiles
.....📁views

Note:

You could find extra information at

https://github.com/EFTEC/BladeOne/issues/72#issuecomment-626328615