Skip to content

Commit

Permalink
让视图文件可以访问registry对象
Browse files Browse the repository at this point in the history
  • Loading branch information
web3d committed Sep 17, 2016
1 parent 08f2273 commit 923fb32
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 64 deletions.
2 changes: 1 addition & 1 deletion system/engine/loader.php
Expand Up @@ -112,7 +112,7 @@ public function view($route, $data = array())
}

if (!$output) {
$template = new Template($this->registry->get('config')->get('template_type'));
$template = new Template($this->registry);

foreach ($data as $key => $value) {
$template->set($key, $value);
Expand Down
42 changes: 26 additions & 16 deletions system/library/template.php
@@ -1,22 +1,32 @@
<?php
class Template {
private $adaptor;

public function __construct($adaptor) {
$class = 'Template\\' . $adaptor;
class Template
{
private $registry;
private $adaptor;

if (class_exists($class)) {
$this->adaptor = new $class();
} else {
throw new \Exception('Error: Could not load template adaptor ' . $adaptor . '!');
}
}
public function __construct($registry)
{
$this->registry = $registry;
$adaptor = $this->registry->get('config')->get('template_type');
$class = 'Template\\' . $adaptor;

public function set($key, $value) {
$this->adaptor->set($key, $value);
}
if (class_exists($class)) {
$this->adaptor = new $class($this->registry);
} else {
throw new \Exception('Error: Could not load template adaptor ' . $adaptor . '!');
}
}

public function set($key, $value)
{
$this->adaptor->set($key, $value);
}

public function render($template)
{
return $this->adaptor->render($template);
}

public function render($template) {
return $this->adaptor->render($template);
}
}
48 changes: 30 additions & 18 deletions system/library/template/php.php
@@ -1,26 +1,38 @@
<?php

namespace Template;
final class PHP {
private $data = array();

public function set($key, $value) {
$this->data[$key] = $value;
}

public function render($template) {
$file = DIR_TEMPLATE . $template;

if (is_file($file)) {
extract($this->data);
final class PHP
{
private $registry;
private $data = array();

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

public function set($key, $value)
{
$this->data[$key] = $value;
}

public function render($template)
{
$file = DIR_TEMPLATE . $template;

if (is_file($file)) {
extract($this->data);

ob_start();

ob_start();
require($file);

require($file);
return ob_get_clean();
}

return ob_get_clean();
}
trigger_error('Error: Could not load template ' . $file . '!');
exit();
}

trigger_error('Error: Could not load template ' . $file . '!');
exit();
}
}
70 changes: 41 additions & 29 deletions system/library/template/tiwg.php
@@ -1,32 +1,44 @@
<?php

namespace Template;
final class PHP {
private $data = array();

public function set($key, $value) {
$this->data[$key] = $value;
}

public function render($template) {
$loader = new Twig_Loader_Array(array(
'index' => 'Hello {{ name }}!',
));

$twig = new Twig_Environment($loader);

$file = DIR_TEMPLATE . $template;

if (is_file($file)) {
extract($this->data);

ob_start();

require($file);

return ob_get_clean();
}

trigger_error('Error: Could not load template ' . $file . '!');
exit();
}

final class PHP
{
private $registry;
private $data = array();

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

public function set($key, $value)
{
$this->data[$key] = $value;
}

public function render($template)
{
$loader = new Twig_Loader_Array(array(
'index' => 'Hello {{ name }}!',
));

$twig = new Twig_Environment($loader);

$file = DIR_TEMPLATE . $template;

if (is_file($file)) {
extract($this->data);

ob_start();

require($file);

return ob_get_clean();
}

trigger_error('Error: Could not load template ' . $file . '!');
exit();
}

}

0 comments on commit 923fb32

Please sign in to comment.