Skip to content

armgit / Vvveb

Public Imported from GitHub
0

Powerful and easy to use cms to build websites, blogs or ecommerce stores.

Added Vtpl JSON support for structured data generation.

givanz67d8c8bb8129
diff --git a/system/component/component.php b/system/component/component.php
index 0aecfcb..6887963 100644
--- a/system/component/component.php
+++ b/system/component/component.php
@@ -53,6 +53,8 @@ class Component {
private $view = false;
+ private $documentType = 'html';
+
private $cache = true;
static function getInstance($view = false, $regenerate = false, $content = false) {
@@ -75,7 +77,7 @@ class Component {
$view = View::getInstance();
}
- $this->view = $view;
+ $this->view = $view;
$this->componentsFile = $view->serviceTemplate() . '.component';
$this->content = $content;
@@ -317,12 +319,19 @@ class Component {
$view = view::getInstance();
libxml_use_internal_errors(true);
+ $this->documentType = $view->getDocumentType();
if ($this->content) {
- @$document->loadHTML($this->content);
+ if ($this->documentType == 'html') {
+ @$document->loadHTML($this->content);
+ } else {
+ @$document->loadXML($this->content);
+ }
} else {
- $view = $this->view;
- $template = $view->template();
+ $view = $this->view;
+ $template = $view->template();
+ $extension = strtolower(trim(substr($template, -4), '.'));
+ $this->documentType = $extension;
if (strpos($template, 'plugins/') === 0) {
$template = str_replace('plugins/', '', $template);
@@ -339,16 +348,29 @@ class Component {
$template = $view->getTemplatePath() . $template;
}
- @$document->loadHTMLFile($template,
- LIBXML_NOWARNING | LIBXML_NOERROR);
+ if ($this->documentType == 'html') {
+ @$document->loadHTMLFile($template,
+ LIBXML_NOWARNING | LIBXML_NOERROR);
+ } else {
+ $content = file_get_contents($template);
+
+ if ($this->documentType == 'json') {
+ $json = json_decode($content, true);
+ $json = \Vvveb\prepareJson($json);
+ $xml = \Vvveb\array2xml($json);
+ @$document->loadXML($xml);
+ } else {
+ @$document->loadXML($content,
+ LIBXML_NOWARNING | LIBXML_NOERROR);
+ }
+ }
}
$xpath = new \DOMXpath($document);
+ $i = 0;
//include froms in case any component_ is included
- $elements = $xpath->query('//*[ @data-v-copy-from or @data-v-save-global ]');
-
- if ($elements && $elements->length) {
+ while (($elements = $xpath->query('//*[ @data-v-copy-from or @data-v-save-global ]')) && $elements->length && $i++ < 2) {
$fromDocument = new \DomDocument();
$fromDocument->preserveWhiteSpace = false;
$fromDocument->recover = true;
@@ -360,12 +382,18 @@ class Component {
foreach ($elements as $element) {
$attribute = $element->getAttribute('data-v-copy-from') ?: $element->getAttribute('data-v-save-global');
+ $element->removeAttribute('data-v-copy-from');
+ $element->removeAttribute('data-v-save-global');
if (preg_match('/([^\,]+)\,([^$,]+)/', $attribute , $from)) {
$file = html_entity_decode(trim($from[1]));
$selector = html_entity_decode(trim($from[2]));
- $fromDocument->loadHTMLFile($view->getTemplatePath() . $file);
+ if ($this->documentType == 'html') {
+ $fromDocument->loadHTMLFile($view->getTemplatePath() . $file);
+ } else {
+ $fromDocument->loadXML($view->getTemplatePath() . $file);
+ }
$fromXpath = new \DOMXpath($fromDocument);
@@ -392,8 +420,7 @@ class Component {
}
}
- //search for elements that have a class starting with component_
- //$elements = $xpath->query('//*[ contains(@class, "component_") ]');
+ //search for elements that have an attribute starting with data-v-component-
$elements = $xpath->query('//*[@*[starts-with(name(), "data-v-component-")]]');
foreach ($elements as $element) {
diff --git a/system/vtpl/vtpl.php b/system/vtpl/vtpl.php
index 8bf4452..ec00882 100755
--- a/system/vtpl/vtpl.php
+++ b/system/vtpl/vtpl.php
@@ -59,7 +59,7 @@ class Vtpl {
private $removeVattrs = false;
- private $isHTML = true;
+ private $documentType = 'html';
private $checkSyntax = true;
@@ -157,6 +157,10 @@ class Vtpl {
$this->removeVattrs = $flag;
}
+ function getDocumentType() {
+ return $this->documentType;
+ }
+
function addCommand($selector, $command = false) {
if ($selector) {
$this->template .= "\n $selector";
@@ -1099,7 +1103,7 @@ class Vtpl {
$value = $node->getAttribute($matches[1]);
$this->debug->log('VTPL_ATTRIBUTE', '<b>ATTRIB NAME</b> ' . $matches[1]);
//expand shorthand expression (*) to regex ([a-zA-Z_0-9-]+)
- $regex = str_replace('(*)', '([a-zA-Z_0-9-]+)', $matches[1]);
+ $regex = str_replace('(*)', '([a-zA-Z_0-9-\.]+)', $matches[1]);
foreach ($node->attributes as $name => $attrNode) {
if (preg_match("@$regex@", $name, $_match)) {
@@ -1187,7 +1191,7 @@ class Vtpl {
$doc->appendChild($doc->importNode($child, true));
}
- if ($this->isHTML) {
+ if ($this->documentType == 'html') {
return $doc->saveHTML();
} else {
return $doc->saveXML();
@@ -1257,7 +1261,7 @@ class Vtpl {
$node->parentNode->replaceChild($doc->importNode($child, true), $node);
}
- if ($this->isHTML) {
+ if ($this->documentType == 'html') {
return $doc->saveHTML();
} else {
return $doc->saveXML();
@@ -1758,7 +1762,7 @@ class Vtpl {
$document = new DomDocument();
- if ($this->isHTML) {
+ if ($this->documentType == 'html') {
@$document->loadHTML($html);
} else {
@$document->loadXML($html);
@@ -1772,7 +1776,8 @@ class Vtpl {
}
function loadHtmlTemplate($filename) {
- $this->isHTML = (substr($filename, -3) != 'xml');
+ $extension = strtolower(trim(substr($filename, -4), '.'));
+ $this->documentType = $extension;
if (strpos($filename, DS) === false) {
$filename = $this->htmlPath . $filename;
@@ -1819,9 +1824,16 @@ class Vtpl {
$html = str_replace(array_keys($this->replaceConstants),array_values($this->replaceConstants),$html);
}
- if ($this->isHTML) {
+ if ($this->documentType == 'html') {
@$this->document->loadHTML($html, LIBXML_NOERROR);
} else {
+ //convert json
+ if ($extension == 'json') {
+ $json = json_decode($html, true);
+ $json = Vvveb\prepareJson($json);
+ $xml = Vvveb\array2xml($json);
+ $html = $xml;
+ }
@$this->document->loadXML($html, LIBXML_NOERROR);
}
@@ -1844,7 +1856,7 @@ class Vtpl {
$tmpDom = new DomDocument();
- if ($this->isHTML) {
+ if ($this->documentType == 'html') {
@$tmpDom->loadHTML($this->componentContent);
} else {
@$tmpDom->loadXML($this->componentContent);
@@ -1946,7 +1958,11 @@ class Vtpl {
$text = \Vvveb\stripExtraSpaces($text);
$trimmed = trim($text);
- if (strlen($trimmed) < 2) {
+ if (strlen($trimmed) < 2 ||
+ (substr_compare($trimmed, '{$', 0, 2) == 0) ||
+ (substr_compare($trimmed, 'http', 0, 4) == 0) ||
+ (is_numeric($trimmed[0]))
+ ) {
continue;
}
@@ -1966,7 +1982,7 @@ class Vtpl {
$c = $this->document->createCDATASection('echo ' . $this->translationFunction . '(\'' . $trimmed . '\');');
$f = $this->document->createElement('_script');
$f->setAttribute('language', 'php');
- $f->append($c);
+ $f->appendChild($c);
$node = $node->parentNode->replaceChild($f, $node);
}
//$node->parentNode->replaceChild($f, $node);
@@ -2122,7 +2138,7 @@ class Vtpl {
$tmpDom = new DOMDocument();
$tmpDom->appendChild($tmpDom->importNode($componentNode, true));
- if ($this->isHTML) {
+ if ($this->documentType == 'html') {
$html = $tmpDom->saveHTML();
} else {
$html = $tmpDom->saveXML();
@@ -2130,14 +2146,14 @@ class Vtpl {
$html = trim($html);
} else {
- if ($this->isHTML) {
+ if ($this->documentType == 'html') {
$html = $this->document->saveHTML();
} else {
$html = $this->document->saveXML();
}
}
} else {
- if ($this->isHTML) {
+ if ($this->documentType == 'html') {
$html = $this->document->saveHTML();
} else {
$html = $this->document->saveXML();