8 Commits
3.0 ... 3.1.1

Author SHA1 Message Date
User
8e62519dfd 20250112#1 2025-01-12 22:44:11 +03:00
User
ed5b931e59 20240429#1 2024-04-29 19:47:40 +03:00
User
d09bd2836a 20240410#12 2024-04-10 03:49:52 +03:00
User
e96bc71eff 20240410#11 2024-04-10 03:46:37 +03:00
User
3b4836bb5e 20240410#10 2024-04-10 03:42:03 +03:00
User
94ada77e89 20240410#9 2024-04-10 03:39:40 +03:00
User
bbaf319dc8 20240410#7 2024-04-10 03:27:21 +03:00
User
763228a883 20240410#6 2024-04-10 03:21:30 +03:00
2 changed files with 46 additions and 8 deletions

View File

@@ -5,6 +5,7 @@ namespace Rmphp\Content;
use Rmphp\Foundation\Exceptions\AppError;
use Rmphp\Foundation\TemplateInterface;
#[\AllowDynamicProperties]
class Content implements TemplateInterface {
/**
@@ -27,7 +28,7 @@ class Content implements TemplateInterface {
/** @inheritDoc */
public function setSubtemplatePath(string $subtemplatePath = "") : TemplateInterface {
ContentData::$subtemplatePath = ContentData::$basePath.$subtemplatePath;
ContentData::$subtemplatePath = ContentData::$basePath.rtrim($subtemplatePath, '/');
return $this;
}
@@ -36,12 +37,48 @@ class Content implements TemplateInterface {
return ContentData::$subtemplatePath;
}
/**
* @param array $aliases
* @return TemplateInterface
*/
public function setSubtemplatePathAlias(array $aliases = []) : TemplateInterface {
foreach($aliases as $alias => $subtemplatePath){
ContentData::$subtemplatePathAlias[$alias] = ContentData::$basePath.rtrim($subtemplatePath, '/');
}
return $this;
}
/**
* @return array
*/
public function getSubtemplatePathAlias() : array {
return ContentData::$subtemplatePathAlias;
}
/**
* @param string $subtemplate
* @return string
*/
public function getFullSubtemplatePath(string $subtemplate) : string {
if (preg_match("'^[%@#](\w+?)(/.+)'", $subtemplate, $match)){
if(empty(ContentData::$subtemplatePathAlias)) throw new AppError("SubtemplatePathAliases is not defined");
if(empty(ContentData::$subtemplatePathAlias[$match[1]])) throw new AppError("Aliase '$match[1]' is not defined");
if(!file_exists(ContentData::$subtemplatePathAlias[$match[1]].$match[2])) throw new AppError("Subtemplate ".ContentData::$subtemplatePathAlias[$match[1]].$match[2]. " is not found");
return ContentData::$subtemplatePathAlias[$match[1]].$match[2];
} else {
if (empty(ContentData::$subtemplatePath)) throw new AppError("SubtemplatePath is not defined");
if (!file_exists(ContentData::$subtemplatePath.$subtemplate)) throw new AppError("Subtemplate ".ContentData::$subtemplatePath.$subtemplate. " is not found");
return ContentData::$subtemplatePath.$subtemplate;
}
}
/**
* @inheritDoc
*/
public function addValue(string $point, string $string) : void {
if (empty($point)) throw new AppError("Empty point");
if (empty(ContentData::$subtemplatePath))throw new AppError("SubtemplatePath is not defined");
ContentData::$content[$point][] = $string;
}
@@ -57,13 +94,13 @@ class Content implements TemplateInterface {
* @inheritDoc
*/
public function addSubtemplate(string $point, string $subtemplate, array $resource = []) : void {
if (empty(ContentData::$subtemplatePath))throw new AppError("SubtemplatePath is not defined");
if (empty($point)) throw new AppError("Empty point");
if (empty($subtemplate) || !file_exists(ContentData::$subtemplatePath.$subtemplate)) throw new AppError(ContentData::$subtemplatePath.$subtemplate. " is not found");
if (empty($subtemplate)) throw new AppError("Subtemplate is empty");
$inc = $this->getFullSubtemplatePath($subtemplate);
foreach ($resource as $resKey => $resVal){
$this->{$resKey} = $resVal;
}
ob_start(); include ContentData::$subtemplatePath.$subtemplate; ContentData::$content[$point][] = ob_get_contents(); ob_end_clean();
ob_start(); include $inc; ContentData::$content[$point][] = ob_get_contents(); ob_end_clean();
}
/**
@@ -78,11 +115,11 @@ class Content implements TemplateInterface {
* @inheritDoc
*/
public function inc(string $incFile, array $resource = []) : string {
$inc = $this->getFullSubtemplatePath($incFile);
foreach ($resource as $resKey => $resVal){
$this->{$resKey} = $resVal;
}
if(empty($incFile) || !file_exists(ContentData::$subtemplatePath.$incFile)) throw new AppError("Empty inc file");
ob_start(); include ContentData::$subtemplatePath.$incFile; $out = ob_get_contents(); ob_end_clean();
ob_start(); include $inc; $out = ob_get_contents(); ob_end_clean();
return $out;
}
@@ -98,4 +135,4 @@ class Content implements TemplateInterface {
ob_start(); include ContentData::$template; $out = ob_get_contents(); ob_end_clean();
return $out;
}
}
}

View File

@@ -8,6 +8,7 @@ class ContentData {
public static string $basePath = "";
public static string $template;
public static string $subtemplatePath;
public static array $subtemplatePathAlias = [];
public static array $content = [];
}