Skip to content

Commit 0e0cbd9

Browse files
committed
loadHtml method to load html directly, fixed add slashes bug, keep adjacent php code for template fragments
1 parent d261f76 commit 0e0cbd9

File tree

1 file changed

+41
-25
lines changed

1 file changed

+41
-25
lines changed

system/vtpl/vtpl.php

+41-25
Original file line numberDiff line numberDiff line change
@@ -1778,29 +1778,10 @@ private function loadFromExternalHtml($val, $node) {
17781778
return $elements;
17791779
}
17801780

1781-
function loadHtmlTemplate($filename) {
1782-
$extension = strtolower(trim(substr($filename, -4), '.'));
1783-
$this->documentType = $extension;
1784-
1785-
if (strpos($filename, DS) === false) {
1786-
$filename = $this->htmlPath . $filename;
1787-
}
1788-
1789-
if (! ($html = @file_get_contents($filename))) {
1790-
Vvveb\logError("can't load template $filename");
1791-
$this->debug->log('LOAD', '<b>ERROR</b> ' . $filename);
1792-
1793-
return false;
1794-
}
1795-
1796-
$this->htmlSourceFile = $filename;
1797-
1798-
$this->debug->log('LOAD', $filename);
1799-
1781+
function loadHtml($html) {
18001782
if (VTPL_DONT_ALLOW_PHP) {
18011783
$html = $this->removePhp($html);
18021784
}
1803-
18041785
//replace script tags with placeholders to preserve formatting.
18051786

18061787
//preg_match_all("@<script[^>]*>.*?script>@s", $html, $this->_scripts);
@@ -1832,7 +1813,7 @@ function loadHtmlTemplate($filename) {
18321813
@$this->document->loadHTML($html, LIBXML_NOERROR);
18331814
} else {
18341815
//convert json
1835-
if ($extension == 'json') {
1816+
if ($this->documentType == 'json') {
18361817
//remove json comments from line start
18371818
$html = Vvveb\removeJsonComments($html);
18381819
$json = json_decode($html, true);
@@ -1895,6 +1876,27 @@ function loadHtmlTemplate($filename) {
18951876
return $errors;
18961877
}
18971878

1879+
function loadHtmlTemplate($filename) {
1880+
$extension = strtolower(trim(substr($filename, -4), '.'));
1881+
$this->documentType = $extension;
1882+
1883+
if (strpos($filename, DS) === false) {
1884+
$filename = $this->htmlPath . $filename;
1885+
}
1886+
1887+
if (! ($html = @file_get_contents($filename))) {
1888+
Vvveb\logError("can't load template $filename");
1889+
$this->debug->log('LOAD', '<b>ERROR</b> ' . $filename);
1890+
1891+
return false;
1892+
}
1893+
1894+
$this->htmlSourceFile = $filename;
1895+
$this->debug->log('LOAD', $filename);
1896+
1897+
$this->loadHtml($html);
1898+
}
1899+
18981900
private function setMultiLanguageText($currentNode) {
18991901
if (! $currentNode || ! $currentNode->childNodes || $currentNode->childNodes->length == 0) {
19001902
return;
@@ -1978,8 +1980,6 @@ function ($matches) {
19781980
$before = $currentNode->childNodes->length;
19791981

19801982
if ($trimmed != '') {
1981-
$trimmed = addcslashes($trimmed, "'");
1982-
19831983
if (strlen($trimmed) < 1024) {
19841984
/*
19851985
$php = '<_script language="php"><![CDATA[ echo ' . $this->translationFunction . '(\'' . $trimmed . '\');]]></_script>';
@@ -1988,7 +1988,7 @@ function ($matches) {
19881988
$f = $this->document->createDocumentFragment();
19891989
$f->appendXML($php);
19901990
*/
1991-
$c = $this->document->createCDATASection('echo ' . $this->translationFunction . '(\'' . $trimmed . '\');');
1991+
$c = $this->document->createCDATASection('echo ' . $this->translationFunction . '(\'' . addcslashes($trimmed, "'") . '\');');
19921992
$f = $this->document->createElement('_script');
19931993
$f->setAttribute('language', 'php');
19941994
$f->appendChild($c);
@@ -2134,7 +2134,8 @@ function ($matches) use ($self) {
21342134
if (isset($matches[2])) {
21352135
$modifier = $matches[2];
21362136
}
2137-
$variable = Vvveb\dotToArrayKey($matches[1]);
2137+
$variable = str_replace('$this.', '$this->', $matches[1]);
2138+
$variable = Vvveb\dotToArrayKey($variable);
21382139
$template =
21392140
"<?php if (isset($variable)) {
21402141
if (is_array($variable)) {
@@ -2171,8 +2172,23 @@ function saveCompiledTemplate($compiledFile) {
21712172

21722173
if ($componentNode) {
21732174
$tmpDom = new DOMDocument();
2175+
2176+
//add before php code if available
2177+
$currentNode = $componentNode;
2178+
2179+
while (($currentNode = $currentNode->previousSibling) && $currentNode->nodeName === '_script') {
2180+
$tmpDom->appendChild($tmpDom->importNode($currentNode, true));
2181+
}
2182+
21742183
$tmpDom->appendChild($tmpDom->importNode($componentNode, true));
21752184

2185+
//add after php code if available
2186+
$currentNode = $componentNode;
2187+
2188+
while (($currentNode = $currentNode->nextSibling) && $currentNode->nodeName === '_script') {
2189+
$tmpDom->appendChild($tmpDom->importNode($currentNode, true));
2190+
}
2191+
21762192
if ($this->documentType == 'html') {
21772193
$html = $tmpDom->saveHTML();
21782194
} else {

0 commit comments

Comments
 (0)