1
0
Fork 0
mirror of synced 2024-06-29 03:30:34 +12:00

feat(template): added static method for template creation from file

This commit is contained in:
Christy Jacob 2021-07-23 22:59:20 +05:30
parent 47084b2835
commit 2919e70d05
3 changed files with 30 additions and 6 deletions

View file

@ -1515,9 +1515,9 @@ App::post('/v1/account/recovery')
$url['query'] = Template::mergeQuery(((isset($url['query'])) ? $url['query'] : ''), ['userId' => $profile->getId(), 'secret' => $secret, 'expire' => $expire]); $url['query'] = Template::mergeQuery(((isset($url['query'])) ? $url['query'] : ''), ['userId' => $profile->getId(), 'secret' => $secret, 'expire' => $expire]);
$url = Template::unParseURL($url); $url = Template::unParseURL($url);
$body = new Template(__DIR__.'/../../config/locale/templates/email-base.tpl'); $body = Template::fromFile(__DIR__.'/../../config/locale/templates/email-base.tpl');
$content = Template::fromHtmlString($locale->getText('account.emails.recovery.body')); $content = Template::fromHtmlString($locale->getText('account.emails.recovery.body'));
$cta = new Template(__DIR__.'/../../config/locale/templates/email-cta.tpl'); $cta = Template::fromFile(__DIR__.'/../../config/locale/templates/email-cta.tpl');
$body $body
->setParam('{{content}}', $content->render(false)) ->setParam('{{content}}', $content->render(false))
@ -1719,9 +1719,9 @@ App::post('/v1/account/verification')
$url['query'] = Template::mergeQuery(((isset($url['query'])) ? $url['query'] : ''), ['userId' => $user->getId(), 'secret' => $verificationSecret, 'expire' => $expire]); $url['query'] = Template::mergeQuery(((isset($url['query'])) ? $url['query'] : ''), ['userId' => $user->getId(), 'secret' => $verificationSecret, 'expire' => $expire]);
$url = Template::unParseURL($url); $url = Template::unParseURL($url);
$body = new Template(__DIR__.'/../../config/locale/templates/email-base.tpl'); $body = Template::fromFile(__DIR__.'/../../config/locale/templates/email-base.tpl');
$content = Template::fromHtmlString($locale->getText('account.emails.verification.body')); $content = Template::fromHtmlString($locale->getText('account.emails.verification.body'));
$cta = new Template(__DIR__.'/../../config/locale/templates/email-cta.tpl'); $cta = Template::fromFile(__DIR__.'/../../config/locale/templates/email-cta.tpl');
$body $body
->setParam('{{content}}', $content->render(false)) ->setParam('{{content}}', $content->render(false))

View file

@ -419,9 +419,9 @@ App::post('/v1/teams/:teamId/memberships')
$url['query'] = Template::mergeQuery(((isset($url['query'])) ? $url['query'] : ''), ['membershipId' => $membership->getId(), 'teamId' => $team->getId(), 'userId' => $invitee->getId(), 'secret' => $secret, 'teamId' => $teamId]); $url['query'] = Template::mergeQuery(((isset($url['query'])) ? $url['query'] : ''), ['membershipId' => $membership->getId(), 'teamId' => $team->getId(), 'userId' => $invitee->getId(), 'secret' => $secret, 'teamId' => $teamId]);
$url = Template::unParseURL($url); $url = Template::unParseURL($url);
$body = new Template(__DIR__.'/../../config/locale/templates/email-base.tpl'); $body = Template::fromFile(__DIR__.'/../../config/locale/templates/email-base.tpl');
$content = Template::fromHtmlString($locale->getText('account.emails.invitation.body')); $content = Template::fromHtmlString($locale->getText('account.emails.invitation.body'));
$cta = new Template(__DIR__.'/../../config/locale/templates/email-cta.tpl'); $cta = Template::fromFile(__DIR__.'/../../config/locale/templates/email-cta.tpl');
$title = \sprintf($locale->getText('account.emails.invitation.title'), $team->getAttribute('name', '[TEAM-NAME]'), $project->getAttribute('name', ['[APP-NAME]'])); $title = \sprintf($locale->getText('account.emails.invitation.title'), $team->getAttribute('name', '[TEAM-NAME]'), $project->getAttribute('name', ['[APP-NAME]']));
$body $body

View file

@ -5,9 +5,32 @@ namespace Appwrite\Template;
use Exception; use Exception;
use Utopia\View; use Utopia\View;
use function PHPUnit\Framework\isReadable;
class Template extends View class Template extends View
{ {
/**
* 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);
}
/** /**
* @var string * @var string
*/ */
@ -28,6 +51,7 @@ class Template extends View
if (empty($html)) { if (empty($html)) {
throw new Exception('Empty HTML string'); throw new Exception('Empty HTML string');
} }
$template = new Template(); $template = new Template();
return $template->setHtml($html); return $template->setHtml($html);
} }