Skip to content

Commit

Permalink
Add php template
Browse files Browse the repository at this point in the history
  • Loading branch information
CraryPrimitiveMan committed Jun 8, 2017
1 parent 727ce44 commit 82bb4b9
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/vendor/*
.idea
runtime/cache
10 changes: 5 additions & 5 deletions controllers/SiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public function actionTest()

public function actionView()
{
$cache = Sf::createObject('cache');
$cache->set('111', '2222');
$result = $cache->get('111');
$cache->flush();
var_dump($result);die;
// $cache = Sf::createObject('cache');
// $cache->set('111', '2222');
// $result = $cache->get('111');
// $cache->flush();
// var_dump($result);die;
$this->render('site/view', ['body' => 'Test body information']);
}

Expand Down
9 changes: 5 additions & 4 deletions src/base/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ abstract class Application
*/
public function run()
{
try {
// try {
return $this->handleRequest();
} catch (Exception $e) {
return $e;
}
// } catch (Exception $e) {
// var_dump($e);
// return $e;
// }
}

/**
Expand Down
18 changes: 17 additions & 1 deletion src/web/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,24 @@ class Controller extends \sf\base\Controller
*/
public function render($view, $params = [])
{
$file = '../views/' . $view . '.sf';
$fileContent = file_get_contents($file);
$result = '';
foreach (token_get_all($fileContent) as $token) {
if (is_array($token)) {
list($id, $content) = $token;
if ($id == T_INLINE_HTML) {
$content = preg_replace('/{{(.*)}}/', '<?php echo $1 ?>', $content);
}
$result .= $content;
} else {
$result .= $token;
}
}
$generatedFile = '../runtime/cache/' . md5($file);
file_put_contents($generatedFile, $result);
extract($params);
return require '../views/' . $view . '.php';
require_once $generatedFile;
}

/**
Expand Down
14 changes: 14 additions & 0 deletions views/site/view.sf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
$title = 'It is a title';
$str = "{{ $title }}";
?>

<html>
<head>
<title>{{ $title }}</title>
<head>
<body>
<h2>{{ $str }}</h2>
<p>{{ $body }}<p>
</body>
</html>

0 comments on commit 82bb4b9

Please sign in to comment.