Created
September 10, 2016 01:27
-
-
Save hscstudio/4ffe5734650db9f3b719178d84ce8db6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Description of DView | |
* | |
* @author Misbahul D Munir <[email protected]> | |
* @since 1.0 | |
*/ | |
class DView | |
{ | |
const POS_HEAD = 1; | |
const POS_END = 2; | |
const POS_READY = 3; | |
private $_files = []; | |
public $title; | |
public $params = []; | |
public $js = []; | |
// [ | |
// 'style.css'=>'head/end', | |
// ] | |
public $cssFiles = []; | |
public $jsFiles = []; | |
public function render($view, $params = []) | |
{ | |
if (strncmp($view, '/', 1) === 0 || count($this->_files) === 0) { | |
$view = Dee::$app->basePath . '/views/' . ltrim($view, '/'); | |
} else { | |
$view = dirname(end($this->_files)) . '/' . $view; | |
} | |
if (is_file($view)) { | |
return $this->renderPhp($view, $params); | |
} elseif (is_file($view . '.php')) { | |
return $this->renderPhp($view . '.php', $params); | |
} | |
throw new Exception("View {$view} not found"); | |
} | |
protected function renderPhp($_file_, $_params_ = []) | |
{ | |
$this->_files[] = $_file_; | |
ob_start(); | |
ob_implicit_flush(false); | |
extract($_params_, EXTR_OVERWRITE); | |
require($_file_); | |
array_pop($this->_files); | |
return ob_get_clean(); | |
} | |
public function registerJs($js, $pos = self::POS_READY) | |
{ | |
$this->js[$pos][] = $js; | |
} | |
public function begin() | |
{ | |
ob_start(); | |
ob_implicit_flush(false); | |
} | |
public function end() | |
{ | |
$content = ob_get_clean(); | |
$jsHead = empty($this->js[self::POS_HEAD]) ? '' : | |
"<script>\n" . implode("\n", $this->js[self::POS_HEAD]) . "\n</script>"; | |
$jsEnd = empty($this->js[self::POS_READY]) ? '' : | |
"(function($){\n" . implode("\n", $this->js[self::POS_READY]) . "\n})(jQuery);"; | |
$jsEnd .= empty($this->js[self::POS_END]) ? '' : "\n" . implode("\n", $this->js[self::POS_END]); | |
$jsEnd = empty(trim($jsEnd)) ? '' : "<script>\n{$jsEnd}\n</script>"; | |
echo strtr($content, [ | |
'<!--#SCRIPT_HEAD-->' => $jsHead, | |
'<!--#SCRIPT_END-->' => $jsEnd, | |
]); | |
} | |
public function setCssFiles($cssFiles) | |
{ | |
foreach($cssFiles as $file => $pos) | |
$this->cssFiles[$file] = $pos; | |
} | |
public function setJsFiles($jsFiles) | |
{ | |
foreach($jsFiles as $file => $pos) | |
$this->jsFiles[$file] = $pos; | |
} | |
public function getAssetUrl() | |
{ | |
return Dee::$app->request->getBaseUrl()."/asset"; | |
} | |
public function buildIncludeFile($file,$type) | |
{ | |
if(substr($file,0,7)=="http://" or substr($file,0,8)=="https://"){ | |
if($type=="css"){ | |
return "<link href='".$file."' rel='stylesheet' />"; | |
} | |
else if($type=="js"){ | |
return "<script src='".$file."'></script>"; | |
} | |
} | |
else{ | |
if($type=="css"){ | |
return "<link href='".$this->getAssetUrl().'/css/'.$file."' rel='stylesheet' />"; | |
} | |
else if($type=="js"){ | |
return "<script src='".$this->getAssetUrl().'/css/'.$file."'></script>"; | |
} | |
} | |
} | |
public function registerHeadFiles() | |
{ | |
$blockFile = ""; | |
foreach($this->cssFiles as $cssFile => $pos){ | |
if($pos=="head") | |
$blockFile .= $this->buildIncludeFile($cssFile, "css")."\n"; | |
} | |
foreach($this->jsFiles as $jsFile => $pos){ | |
if($pos == "head") | |
$blockFile .= $this->buildIncludeFile($jsFile, "js")."\n"; | |
} | |
return $blockFile; | |
} | |
public function registerBodyFiles() | |
{ | |
$blockFile = ""; | |
foreach($this->cssFiles as $cssFile => $pos){ | |
if($pos=="body") | |
$blockFile .= $this->buildIncludeFile($cssFile, "css")."\n"; | |
} | |
foreach($this->jsFiles as $jsFile => $pos){ | |
if($pos == "body") | |
$blockFile .= $this->buildIncludeFile($jsFile, "js")."\n"; | |
} | |
return $blockFile; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* @var $this DView */ | |
$this->begin(); | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
<!-- Meta, title, CSS, favicons, etc. --> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title><?= $this->title ?></title> | |
<?php | |
$this->setCssFiles([ | |
'bootstrap.min.css'=>'head', | |
'font-awesome.min.css'=>'head', | |
'nprogress.css'=>'head', | |
'animate.min.css'=>'head', | |
'custom.min.css'=>'head', | |
]); | |
$this->setJsFiles([ | |
'jquery.min.js'=>'body', | |
'bootstrap.min.js'=>'body', | |
'fastclick.js'=>'body', | |
'nprogress.js'=>'body' | |
]); | |
?> | |
<?= $this->registerHeadFiles() ?> | |
<!--#SCRIPT_HEAD--> | |
</head> | |
<body> | |
<div class="wrap"> | |
<div class="container"> | |
<div class="pull-right"> | |
<?php if (isset(Dee::$app->user->id)): ?> | |
<?= Dee::$app->user->id ?> <a href="<?= Dee::createUrl('site/logout') ?>">Logout</a> | |
<?php else: ?> | |
<a href="<?= Dee::createUrl('site/login') ?>">Login</a> | |
<?php endif; ?> | |
</div> | |
<h1><?= $this->title ?></h1> | |
<?= $content ?> | |
</div> | |
</div> | |
<?= $this->registerBodyFiles() ?> | |
<!--#SCRIPT_END--> | |
</body> | |
</html> | |
<?php | |
$this->end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment