Skip to content

armgit / Vvveb

Public Imported from GitHub
0

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

Support for comments in json files when rendering json templates

givanz7dfd67124495
diff --git a/system/component/component.php b/system/component/component.php
index 6887963..60f6d7f 100644
--- a/system/component/component.php
+++ b/system/component/component.php
@@ -23,6 +23,7 @@
namespace Vvveb\System\Component;
use function Vvveb\dashesToCamelCase;
+use function Vvveb\removeJsonComments;
use Vvveb\System\Cache;
use Vvveb\System\Core\View;
@@ -333,19 +334,22 @@ class Component {
$extension = strtolower(trim(substr($template, -4), '.'));
$this->documentType = $extension;
- if (strpos($template, 'plugins/') === 0) {
- $template = str_replace('plugins/', '', $template);
- $p = strpos($template, '/');
- $pluginName = substr($template, 0, $p);
- $nameSpace = substr($template, $p + 1);
- $app = '';
+ if ($template[0] == '/') {
+ } else {
+ if (strpos($template, 'plugins/') === 0) {
+ $template = str_replace('plugins/', '', $template);
+ $p = strpos($template, '/');
+ $pluginName = substr($template, 0, $p);
+ $nameSpace = substr($template, $p + 1);
+ $app = '';
- if (APP != 'app') {
- $app = APP . DS;
+ if (APP != 'app') {
+ $app = APP . DS;
+ }
+ $template = DIR_PLUGINS . $pluginName . DS . 'public' . DS . $app . $nameSpace;
+ } else {
+ $template = $view->getTemplatePath() . $template;
}
- $template = DIR_PLUGINS . $pluginName . DS . 'public' . DS . $app . $nameSpace;
- } else {
- $template = $view->getTemplatePath() . $template;
}
if ($this->documentType == 'html') {
@@ -353,8 +357,10 @@ class Component {
LIBXML_NOWARNING | LIBXML_NOERROR);
} else {
$content = file_get_contents($template);
-
+
if ($this->documentType == 'json') {
+ //remove json comments from line start
+ $content = removeJsonComments($content);
$json = json_decode($content, true);
$json = \Vvveb\prepareJson($json);
$xml = \Vvveb\array2xml($json);
diff --git a/system/core/frontcontroller.php b/system/core/frontcontroller.php
index 3cf4103..6c8d773 100755
--- a/system/core/frontcontroller.php
+++ b/system/core/frontcontroller.php
@@ -139,7 +139,7 @@ class FrontController {
*/
//header(' ', true, $statusCode);
$response = Response::getInstance();
- $view->setType($response->getType('json'));
+ $view->setType($response->getType());
//return $response->output();
PageCache::getInstance()->cleanUp();
@@ -216,11 +216,20 @@ class FrontController {
$template = str_replace('/', DS, strtolower(self :: $moduleName));
$theme = $controller->view->getTheme();
$path = DIR_THEME . $theme . DS;
+ $pluginName = false;
if ($actionName && $actionName != 'index') {
$html = $path . $template . DS . strtolower($actionName) . '.html';
- if (file_exists($html)) {
+ if (is_file($html)) {
+ if ($pluginName) {
+ $template .= 'plugins' . DS . $nameSpace . DS . strtolower($actionName);
+ } else {
+ $template .= DS . strtolower($actionName);
+ }
+ }
+
+ if ($pluginName) {
$template .= DS . strtolower($actionName);
}
}
@@ -246,7 +255,7 @@ class FrontController {
//return $controller->view->render();
$response = Response::getInstance();
- $controller->view->setType($response->getType('json'));
+ $controller->view->setType($response->getType());
$return = $response->output();
self :: closeConnections();
diff --git a/system/vtpl/vtpl.php b/system/vtpl/vtpl.php
index 1b65eac..8248fa6 100755
--- a/system/vtpl/vtpl.php
+++ b/system/vtpl/vtpl.php
@@ -309,8 +309,9 @@ class Vtpl {
preg_match_all('/(?<!["\'\[])(\$.+)/', $this->template, $variables);
//$variables[0] = array_values($variables[0]);
-
- $variables[1] = array_unique($variables[1]);
+ $patternsVariables = [];
+ $placeholdersVariables = [];
+ $variables[1] = array_unique($variables[1]);
//usort($variables[1],fn($a,$b) => strlen($b) <=> strlen($a));
usort($variables[1],function ($a,$b) {return strlen($b) <=> strlen($a); });
@@ -864,6 +865,7 @@ class Vtpl {
}
}
}
+
//if ($class && preg_match_all('@filter_([^ :$]+)(:\'[^\']+\'|:[^ $]+)*@', $class, $matches, PREG_SET_ORDER) > 0)
if ($filters) {
$chain = '_$variable';
@@ -1790,6 +1792,7 @@ class Vtpl {
return false;
}
+
$this->htmlSourceFile = $filename;
$this->debug->log('LOAD', $filename);
@@ -1830,6 +1833,8 @@ class Vtpl {
} else {
//convert json
if ($extension == 'json') {
+ //remove json comments from line start
+ $html = Vvveb\removeJsonComments($html);
$json = json_decode($html, true);
$json = Vvveb\prepareJson($json);
$xml = Vvveb\array2xml($json);
@@ -1959,9 +1964,12 @@ class Vtpl {
$text = \Vvveb\stripExtraSpaces($text);
$trimmed = trim($text);
- if (strlen($trimmed) < 2 ||
- (substr_compare($trimmed, '{$', 0, 2) == 0) ||
- (substr_compare($trimmed, 'http', 0, 4) == 0) ||
+ //skip untranslatable text
+ if (strlen($trimmed) < 2 || //too small
+ (substr_compare($trimmed, '{$', 0, 2) == 0) || //starts with variable
+ (substr_compare($trimmed, 'http', 0, 4) == 0) || //is url
+ (strpos($trimmed, '{$') !== false) || //string has variable
+ (strpos($trimmed, '<?php') !== false) || //string has php code
(is_numeric($trimmed[0]))
) {
continue;