Skip to content

Commit

Permalink
Render the page template only for HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Oct 28, 2022
1 parent 05ffc99 commit e30ff43
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
18 changes: 11 additions & 7 deletions src/controllers/SendoutsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,19 @@ public function actionGetPendingRecipientCount(): Response
*/
public function actionGetHtmlBody(): Response
{
$sendoutId = $this->request->getRequiredParam('sendoutId');
$sendout = Campaign::$plugin->sendouts->getSendoutById($sendoutId);
$sendout = $this->_getSendoutFromParamId();
$this->response->content = $sendout->getHtmlBody();

if ($sendout === null) {
throw new NotFoundHttpException(Craft::t('campaign', 'Sendout not found.'));
}
return $this->response;
}

// Prep the response
$this->response->content = $sendout->getHtmlBody();
/**
* Returns the plaintext body.
*/
public function actionGetPlaintextBody(): Response
{
$sendout = $this->_getSendoutFromParamId();
$this->response->content = $sendout->getPlaintextBody();

return $this->response;
}
Expand Down
31 changes: 17 additions & 14 deletions src/elements/CampaignElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,16 @@ private function _getBody(string $templateType = null, ContactElement $contact =

// Get body from rendered template with variables
$template = $templateType == 'html' ? $this->getCampaignType()->htmlTemplate : $this->getCampaignType()->plaintextTemplate;
$variables = [
'campaign' => $this,
'browserVersionUrl' => $this->url,
'contact' => $contact,
'sendout' => $sendout,
'mailingList' => $mailingList,
'unsubscribeUrl' => $contact->getUnsubscribeUrl($sendout),
'isWebRequest' => false,
];
$templateMode = View::TEMPLATE_MODE_SITE;

// Set the current site from the campaign's site ID
Craft::$app->getSites()->setCurrentSite($this->siteId);
Expand All @@ -965,20 +975,13 @@ private function _getBody(string $templateType = null, ContactElement $contact =
Craft::$app->language = $this->_getLanguage();

try {
// Render the page template to prevent Yii block tags being left behind
$body = Craft::$app->getView()->renderPageTemplate(
$template,
[
'campaign' => $this,
'browserVersionUrl' => $this->url,
'contact' => $contact,
'sendout' => $sendout,
'mailingList' => $mailingList,
'unsubscribeUrl' => $contact->getUnsubscribeUrl($sendout),
'isWebRequest' => false,
],
View::TEMPLATE_MODE_SITE,
);
// Render the page template only for HTML, to prevent Yii block tags being left behind
if ($templateType == 'html') {
$body = Craft::$app->getView()->renderPageTemplate($template, $variables, $templateMode);
}
else {
$body = Craft::$app->getView()->renderTemplate($template, $variables, $templateMode);
}
}
catch (Error $exception) {
Campaign::$plugin->log($exception->getMessage());
Expand Down

0 comments on commit e30ff43

Please sign in to comment.