mirror of
https://github.com/appwrite/appwrite.git
synced 2026-05-26 13:51:13 +00:00
Merge branch 'master' into 0.9.x
This commit is contained in:
@@ -5,8 +5,56 @@ namespace Appwrite\Template;
|
||||
use Exception;
|
||||
use Utopia\View;
|
||||
|
||||
|
||||
class Template extends View
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected string $content = '';
|
||||
|
||||
/**
|
||||
* fromFile
|
||||
*
|
||||
* Creates a new Template() from the file at $path
|
||||
*
|
||||
* @param string $path
|
||||
*
|
||||
* @return self
|
||||
*
|
||||
*/
|
||||
public static function fromFile(string $path): self
|
||||
{
|
||||
if (!\is_readable($path)) {
|
||||
throw new Exception("$path view template is not readable.");
|
||||
}
|
||||
|
||||
$template = new Template();
|
||||
return $template->setPath($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* fromString
|
||||
*
|
||||
* Creates a new Template() using a raw string
|
||||
*
|
||||
* @param string $content
|
||||
*
|
||||
* @return self
|
||||
*
|
||||
*/
|
||||
public static function fromString(string $content): self
|
||||
{
|
||||
if (empty($content)) {
|
||||
throw new Exception('Empty string');
|
||||
}
|
||||
|
||||
$template = new Template();
|
||||
$template->content = $content;
|
||||
return $template;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render.
|
||||
*
|
||||
@@ -25,10 +73,14 @@ class Template extends View
|
||||
|
||||
if (\is_readable($this->path)) {
|
||||
$template = \file_get_contents($this->path); // Include template file
|
||||
} else if (!empty($this->content)) {
|
||||
$template = $this->print($this->content, self::FILTER_NL2P);
|
||||
} else {
|
||||
throw new Exception('"'.$this->path.'" template is not readable or not found');
|
||||
}
|
||||
|
||||
// First replace the variables inside the params. Then replace the variables in the template
|
||||
$this->params = array_merge($this->params, \str_replace(\array_keys($this->params), \array_values($this->params), $this->params));
|
||||
$template = \str_replace(\array_keys($this->params), \array_values($this->params), $template);
|
||||
|
||||
return $template;
|
||||
@@ -125,4 +177,5 @@ class Template extends View
|
||||
{
|
||||
return \str_replace([' ', '_'], '-', \strtolower(\preg_replace('/([a-zA-Z])(?=[A-Z])/', '$1-', $input)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user