Skip to content

Commit 2c1dc0d

Browse files
committed
update: Added loadfile to fill var
1 parent a6f90b3 commit 2c1dc0d

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

html_files/index.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
<title><_template:title/></title> <!-- One method for var reference -->
88
</head>
99
<body>
10-
<_template:block(loop)>
11-
<_template:loadFile(block.html)/>
12-
</_template:block>
10+
<span style="display:block;text-align:right"><_template:title/></span>
1311

14-
<_template:function.ucwords({{title}})/>
1512

13+
<p style="color:{{cor}}">Sou um parágrafo</p>
1614
</body>
17-
</html>
15+
</html>
16+
17+

index.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,13 @@
66
require "template.class.php";
77

88
$vars = array(
9-
"title" => "php template engine",
9+
"title" => "PHP Template Engine",
10+
"cor" => "#f52342",
11+
"meu_cabelo_eh_vermelho" => false
1012
);
1113

1214
$layout = new Template();
1315
$layout->setDir("./html_files");
1416
$layout->loadFile("index.html");
15-
//$layout->setVar($vars);
16-
$layout->setIf("hasVar", true);
17-
$block = $layout->getBlock("childBlock");
18-
if ($block) {
19-
$block->setBlockLoop();
20-
for ($i = 0; $i < 8; $i++) {
21-
$block->setBlock(array("i" => $i));
22-
}
23-
$layout->setBlock($block);
24-
}
25-
17+
$layout->setVar($vars);
2618
$layout->render(true);

template.class.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ public function code($code)
454454
}
455455

456456
/** This method gets the contents of the given file. If $raw == true then it will return the raw HTML code */
457-
public function loadFile($filePath = false, $raw = false)
457+
public function loadFile($filePath = false, $raw = false, $var = false)
458458
{
459459
if ($filePath) {
460460
$filePath = preg_replace("/\/{1,}|\\{1,}/", DIRECTORY_SEPARATOR, $filePath);
@@ -464,8 +464,8 @@ public function loadFile($filePath = false, $raw = false)
464464
}
465465
$path = $instance->templateDir . $filePath;
466466
if (file_exists($path)) {
467-
$instance->HTML = file_get_contents($path);
468-
if (preg_match_all($this->templateIncludeRegex, $instance->HTML, $matches)) {
467+
$HTML = file_get_contents($path);
468+
if (preg_match_all($this->templateIncludeRegex, $HTML, $matches)) {
469469
$matches = $matches[1];
470470
$class = get_class($instance);
471471
foreach ($matches as $match) {
@@ -479,10 +479,17 @@ public function loadFile($filePath = false, $raw = false)
479479
unset($data);
480480
$fileName = explode(DIRECTORY_SEPARATOR, $fileName);
481481
$fileName = $fileName[count($fileName) - 1];
482-
$instance->HTML = preg_replace("/\<\_template\:loadFile\(.*?" . addslashes($fileName) . "\)( )?(\/)?\/>/", $childNode->rawRender(), $instance->HTML);
482+
$HTML = preg_replace("/\<\_template\:loadFile\(.*?" . addslashes($fileName) . "\)( )?(\/)?\/>/", $childNode->rawRender(), $HTML);
483483
unset($childNode, $aux, $fileName);
484484
}
485485
}
486+
if ($var) {
487+
$instance->setVar([$var => $HTML]);
488+
$instance->prepareDocument();
489+
} else {
490+
$instance->HTML = $HTML;
491+
}
492+
unset($HTML);
486493
if ($raw) {
487494
return $instance->rawRender();
488495
}

0 commit comments

Comments
 (0)