Pattern Coverage Problem

I am trying to hack the skeleton system of View in PHP, but I cannot figure out how to get inline views to get my parent variables. For example:

View class

class View
{
    private $_vars=array();
    private $_file;

    public function __construct($file)
    {
        $this->_file='views/'.$file.'.php';
    }

    public function set($var, $value=null)
    {
        if (is_array($var))
        {
            $this->_vars=array_merge($var, $this->_vars);
        }
        else
            $this->_vars[$var]=$value;

        return $this;
    }

    public function output()
    {
        if (count($this->_vars))
            extract($this->_vars,  EXTR_REFS);
        require($this->_file);
        exit;
    }

    public static function factory($file)
    {
        return new self($file);
    }
}

test.php (top level view)

<html>
    <body>
        Hey <?=$name?>! This is <?=$adj?>!
        <?=View::factory('embed')->output()?>
    </body>
</html>

embed.php (built in test.php

<html>
    <body>
        Hey <?=$name?>! This is an embedded view file!!
    </body>
</html>

Code:

$vars=array(
    'name' => 'ryan',
    'adj' => 'cool'
);
View::factory('test')->set($vars)->output();

Output:

Hey ryan! This is cool! Hey [error for $name not being defined] 
this is an embedded view file!!

The problem is that the variables set in the top-level view are not passed to the inline view. How can i do this?

+3
source share
4 answers

So, I do not quite answer your question, but here is my super simple manual template system. It supports what you are trying to do, although the interface is different.

// Usage
$main = new SimpleTemplate("templating/html.php");
$main->extract($someObject);
$main->extract($someArray);
$main->name = "my name";
$subTemplate = new SimpleTemplate("templating/another.php");
$subTemplate->parent($main);
$main->placeholderForAnotherTemplate = $subTemplate->run();
echo $main; // or $main->run(); 

// html.php
<html><body><h1>Title <?= $name ?></h1><p><?= $placeHolderForAnotherTemplate ?></p></body></html>

    <?php
// SimpleTemplate.php
function object_to_array($object)
{
    $array = array();
    foreach($object as $property => $value)
    {
        $array[$property] = $value;
    }

    return $array;
}

class SimpleTemplate
{
    public $source;
    public $path;
    public $result;
    public $parent;

    public function SimpleTemplate($path=false, $source=false)
    {
        $this->source = array();
        $this->extract($source);
        $this->path($path);
    }

    public function __toString()
    {
        return $this->run();
    }

    public function extract($source)
    {
        if ($source)
        {
            foreach ($source as $property => $value)
            {
                $this->source[$property] = $value;
            }
        }
    }

    public function parent($parent)
    {
        $this->parent = $parent;
    }

    public function path($path)
    {
        $this->path = $path;
    }

    public function __set($name, $value)
    {
        $this->source[$name] = $value;
    }

    public function __get($name)
    {
        return isset($this->source[$name]) ? $this->source[$name] : "";
    }

    public function mergeSource()
    {
        if (isset($this->parent))
            return array_merge($this->parent->mergeSource(), $this->source);
        else
            return $this->source;
    }

    public function run()
    {
        ob_start();
        extract ($this->mergeSource());
        include $this->path;
        $this->result = ob_get_contents();
        ob_end_clean();
        return $this->result;
    }
}
+1
source

, , . , .

edit: factory

0

, . :

<?=View::factory('embed')->output()?>

"" , , .

, .

# 1 - .

"" , output() . View, . :

$pView = new View_Parent_Class();
$cView = new View_Child_Class();
$pView->addView($cView);

$pview->render() .

, .

# 2 -

, , , , . :

public function output($extra_vars = null)
{
    if (count($this->_vars))
        extract($this->_vars,  EXTR_REFS);
    if (is_array($extra_vars)) extract($extra_vars, EXTR_REFS);
    require($this->_file);
    exit;
}

getter:

public function get_vars()
{
    return $this->_vars;
}

, - :

<?=View::factory('embed')->output($this->get_vars())?>

$this , .. . , - extract.

0

$_vars , , , .

... array_merge() set() , 2 .

0

Source: https://habr.com/ru/post/1713177/


All Articles