Skip to content

Commit 67d8c8b

Browse files
committed
Added Vtpl JSON support for structured data generation.
1 parent 94c9d93 commit 67d8c8b

File tree

2 files changed

+68
-25
lines changed

2 files changed

+68
-25
lines changed

system/component/component.php

+39-12
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class Component {
5353

5454
private $view = false;
5555

56+
private $documentType = 'html';
57+
5658
private $cache = true;
5759

5860
static function getInstance($view = false, $regenerate = false, $content = false) {
@@ -75,7 +77,7 @@ function __construct($view, $regenerate = false, $content = false) {
7577
$view = View::getInstance();
7678
}
7779

78-
$this->view = $view;
80+
$this->view = $view;
7981

8082
$this->componentsFile = $view->serviceTemplate() . '.component';
8183
$this->content = $content;
@@ -317,12 +319,19 @@ function generateRequiredComponents() {
317319

318320
$view = view::getInstance();
319321
libxml_use_internal_errors(true);
322+
$this->documentType = $view->getDocumentType();
320323

321324
if ($this->content) {
322-
@$document->loadHTML($this->content);
325+
if ($this->documentType == 'html') {
326+
@$document->loadHTML($this->content);
327+
} else {
328+
@$document->loadXML($this->content);
329+
}
323330
} else {
324-
$view = $this->view;
325-
$template = $view->template();
331+
$view = $this->view;
332+
$template = $view->template();
333+
$extension = strtolower(trim(substr($template, -4), '.'));
334+
$this->documentType = $extension;
326335

327336
if (strpos($template, 'plugins/') === 0) {
328337
$template = str_replace('plugins/', '', $template);
@@ -339,16 +348,29 @@ function generateRequiredComponents() {
339348
$template = $view->getTemplatePath() . $template;
340349
}
341350

342-
@$document->loadHTMLFile($template,
343-
LIBXML_NOWARNING | LIBXML_NOERROR);
351+
if ($this->documentType == 'html') {
352+
@$document->loadHTMLFile($template,
353+
LIBXML_NOWARNING | LIBXML_NOERROR);
354+
} else {
355+
$content = file_get_contents($template);
356+
357+
if ($this->documentType == 'json') {
358+
$json = json_decode($content, true);
359+
$json = \Vvveb\prepareJson($json);
360+
$xml = \Vvveb\array2xml($json);
361+
@$document->loadXML($xml);
362+
} else {
363+
@$document->loadXML($content,
364+
LIBXML_NOWARNING | LIBXML_NOERROR);
365+
}
366+
}
344367
}
345368

346369
$xpath = new \DOMXpath($document);
370+
$i = 0;
347371

348372
//include froms in case any component_ is included
349-
$elements = $xpath->query('//*[ @data-v-copy-from or @data-v-save-global ]');
350-
351-
if ($elements && $elements->length) {
373+
while (($elements = $xpath->query('//*[ @data-v-copy-from or @data-v-save-global ]')) && $elements->length && $i++ < 2) {
352374
$fromDocument = new \DomDocument();
353375
$fromDocument->preserveWhiteSpace = false;
354376
$fromDocument->recover = true;
@@ -360,12 +382,18 @@ function generateRequiredComponents() {
360382

361383
foreach ($elements as $element) {
362384
$attribute = $element->getAttribute('data-v-copy-from') ?: $element->getAttribute('data-v-save-global');
385+
$element->removeAttribute('data-v-copy-from');
386+
$element->removeAttribute('data-v-save-global');
363387

364388
if (preg_match('/([^\,]+)\,([^$,]+)/', $attribute , $from)) {
365389
$file = html_entity_decode(trim($from[1]));
366390
$selector = html_entity_decode(trim($from[2]));
367391

368-
$fromDocument->loadHTMLFile($view->getTemplatePath() . $file);
392+
if ($this->documentType == 'html') {
393+
$fromDocument->loadHTMLFile($view->getTemplatePath() . $file);
394+
} else {
395+
$fromDocument->loadXML($view->getTemplatePath() . $file);
396+
}
369397

370398
$fromXpath = new \DOMXpath($fromDocument);
371399

@@ -392,8 +420,7 @@ function generateRequiredComponents() {
392420
}
393421
}
394422

395-
//search for elements that have a class starting with component_
396-
//$elements = $xpath->query('//*[ contains(@class, "component_") ]');
423+
//search for elements that have an attribute starting with data-v-component-
397424
$elements = $xpath->query('//*[@*[starts-with(name(), "data-v-component-")]]');
398425

399426
foreach ($elements as $element) {

system/vtpl/vtpl.php

+29-13
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class Vtpl {
5959

6060
private $removeVattrs = false;
6161

62-
private $isHTML = true;
62+
private $documentType = 'html';
6363

6464
private $checkSyntax = true;
6565

@@ -157,6 +157,10 @@ function removeVattrs($flag = true) {
157157
$this->removeVattrs = $flag;
158158
}
159159

160+
function getDocumentType() {
161+
return $this->documentType;
162+
}
163+
160164
function addCommand($selector, $command = false) {
161165
if ($selector) {
162166
$this->template .= "\n $selector";
@@ -1099,7 +1103,7 @@ function ($matches) use ($node) {
10991103
$value = $node->getAttribute($matches[1]);
11001104
$this->debug->log('VTPL_ATTRIBUTE', '<b>ATTRIB NAME</b> ' . $matches[1]);
11011105
//expand shorthand expression (*) to regex ([a-zA-Z_0-9-]+)
1102-
$regex = str_replace('(*)', '([a-zA-Z_0-9-]+)', $matches[1]);
1106+
$regex = str_replace('(*)', '([a-zA-Z_0-9-\.]+)', $matches[1]);
11031107

11041108
foreach ($node->attributes as $name => $attrNode) {
11051109
if (preg_match("@$regex@", $name, $_match)) {
@@ -1187,7 +1191,7 @@ private function innerHTML($nodeList, $html = false) {
11871191
$doc->appendChild($doc->importNode($child, true));
11881192
}
11891193

1190-
if ($this->isHTML) {
1194+
if ($this->documentType == 'html') {
11911195
return $doc->saveHTML();
11921196
} else {
11931197
return $doc->saveXML();
@@ -1257,7 +1261,7 @@ private function outerHTML(&$nodeList, $html = false) {
12571261
$node->parentNode->replaceChild($doc->importNode($child, true), $node);
12581262
}
12591263

1260-
if ($this->isHTML) {
1264+
if ($this->documentType == 'html') {
12611265
return $doc->saveHTML();
12621266
} else {
12631267
return $doc->saveXML();
@@ -1758,7 +1762,7 @@ private function loadFromExternalHtml($val, $node) {
17581762

17591763
$document = new DomDocument();
17601764

1761-
if ($this->isHTML) {
1765+
if ($this->documentType == 'html') {
17621766
@$document->loadHTML($html);
17631767
} else {
17641768
@$document->loadXML($html);
@@ -1772,7 +1776,8 @@ private function loadFromExternalHtml($val, $node) {
17721776
}
17731777

17741778
function loadHtmlTemplate($filename) {
1775-
$this->isHTML = (substr($filename, -3) != 'xml');
1779+
$extension = strtolower(trim(substr($filename, -4), '.'));
1780+
$this->documentType = $extension;
17761781

17771782
if (strpos($filename, DS) === false) {
17781783
$filename = $this->htmlPath . $filename;
@@ -1819,9 +1824,16 @@ function loadHtmlTemplate($filename) {
18191824
$html = str_replace(array_keys($this->replaceConstants),array_values($this->replaceConstants),$html);
18201825
}
18211826

1822-
if ($this->isHTML) {
1827+
if ($this->documentType == 'html') {
18231828
@$this->document->loadHTML($html, LIBXML_NOERROR);
18241829
} else {
1830+
//convert json
1831+
if ($extension == 'json') {
1832+
$json = json_decode($html, true);
1833+
$json = Vvveb\prepareJson($json);
1834+
$xml = Vvveb\array2xml($json);
1835+
$html = $xml;
1836+
}
18251837
@$this->document->loadXML($html, LIBXML_NOERROR);
18261838
}
18271839

@@ -1844,7 +1856,7 @@ function loadHtmlTemplate($filename) {
18441856

18451857
$tmpDom = new DomDocument();
18461858

1847-
if ($this->isHTML) {
1859+
if ($this->documentType == 'html') {
18481860
@$tmpDom->loadHTML($this->componentContent);
18491861
} else {
18501862
@$tmpDom->loadXML($this->componentContent);
@@ -1946,7 +1958,11 @@ function ($matches) {
19461958
$text = \Vvveb\stripExtraSpaces($text);
19471959
$trimmed = trim($text);
19481960

1949-
if (strlen($trimmed) < 2) {
1961+
if (strlen($trimmed) < 2 ||
1962+
(substr_compare($trimmed, '{$', 0, 2) == 0) ||
1963+
(substr_compare($trimmed, 'http', 0, 4) == 0) ||
1964+
(is_numeric($trimmed[0]))
1965+
) {
19501966
continue;
19511967
}
19521968

@@ -1966,7 +1982,7 @@ function ($matches) {
19661982
$c = $this->document->createCDATASection('echo ' . $this->translationFunction . '(\'' . $trimmed . '\');');
19671983
$f = $this->document->createElement('_script');
19681984
$f->setAttribute('language', 'php');
1969-
$f->append($c);
1985+
$f->appendChild($c);
19701986
$node = $node->parentNode->replaceChild($f, $node);
19711987
}
19721988
//$node->parentNode->replaceChild($f, $node);
@@ -2122,22 +2138,22 @@ function saveCompiledTemplate($compiledFile) {
21222138
$tmpDom = new DOMDocument();
21232139
$tmpDom->appendChild($tmpDom->importNode($componentNode, true));
21242140

2125-
if ($this->isHTML) {
2141+
if ($this->documentType == 'html') {
21262142
$html = $tmpDom->saveHTML();
21272143
} else {
21282144
$html = $tmpDom->saveXML();
21292145
}
21302146

21312147
$html = trim($html);
21322148
} else {
2133-
if ($this->isHTML) {
2149+
if ($this->documentType == 'html') {
21342150
$html = $this->document->saveHTML();
21352151
} else {
21362152
$html = $this->document->saveXML();
21372153
}
21382154
}
21392155
} else {
2140-
if ($this->isHTML) {
2156+
if ($this->documentType == 'html') {
21412157
$html = $this->document->saveHTML();
21422158
} else {
21432159
$html = $this->document->saveXML();

0 commit comments

Comments
 (0)