-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
04557fd
commit a97757d
Showing
6 changed files
with
116 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,9 @@ | |
namespace yii1tech\mailer; | ||
|
||
use CFileHelper; | ||
use CMap; | ||
use Yii; | ||
use yii1tech\mailer\widgets\ClipWidget; | ||
|
||
/** | ||
* View is a email template view renderer. | ||
|
@@ -14,6 +16,7 @@ | |
* | ||
* @property string $viewPath the root directory of view files. Defaults to 'views/mail' under the application base path. | ||
* @property \IViewRenderer|\CViewRenderer|array|string|null|false $viewRenderer view renderer or its array configuration. | ||
* @property \CMap $clips The list of clips. | ||
* | ||
* @author Paul Klimov <[email protected]> | ||
* @since 1.0 | ||
|
@@ -38,6 +41,12 @@ class View extends \CBaseController | |
*/ | ||
private $_viewRenderer; | ||
|
||
/** | ||
* @var \CMap|null list of clips. | ||
* @see \CClipWidget | ||
*/ | ||
private $_clips; | ||
|
||
/** | ||
* @return string the root directory of view files. Defaults to 'views/mail' under the application base path. | ||
*/ | ||
|
@@ -178,21 +187,19 @@ public function render(string $view, ?array $data = null, ?string $locale = null | |
$content = $this->renderPartial($this->layout, ['content' => $content], true); | ||
} | ||
} catch (\Throwable $e) { | ||
throw $e; | ||
} finally { | ||
while (ob_get_level() > $obInitialLevel) { | ||
if (!@ob_end_clean()) { | ||
ob_clean(); | ||
} | ||
} | ||
|
||
$this->layout = $originalLayout; | ||
$this->_clips = null; | ||
Yii::app()->setLanguage($originalLocale); | ||
|
||
throw $e; | ||
} | ||
|
||
$this->layout = $originalLayout; | ||
Yii::app()->setLanguage($originalLocale); | ||
|
||
return $content; | ||
} | ||
|
||
|
@@ -220,4 +227,44 @@ public function renderPartial(string $view, ?array $data = null, bool $return = | |
|
||
return $this->renderFile($viewFile, $data, $return); | ||
} | ||
|
||
/** | ||
* Returns the list of clips. | ||
* A clip is a named piece of rendering result that can be inserted at different places. | ||
* | ||
* @see \yii1tech\mailer\widgets\ClipWidget | ||
* | ||
* @return \CMap the list of clips | ||
*/ | ||
public function getClips(): CMap | ||
{ | ||
if ($this->_clips === null) { | ||
$this->_clips = new CMap(); | ||
} | ||
|
||
return $this->_clips; | ||
} | ||
|
||
/** | ||
* Begins recording a clip. | ||
* This method is a shortcut to beginning {@see \yii1tech\mailer\widgets\ClipWidget}. | ||
* | ||
* @param string $id the clip ID. | ||
* @param array $properties initial property values for {@see \yii1tech\mailer\widgets\ClipWidget}. | ||
*/ | ||
public function beginClip($id, $properties = []): void | ||
{ | ||
$properties['id'] = $id; | ||
$properties['view'] = $this; | ||
$this->beginWidget(ClipWidget::class, $properties); | ||
} | ||
|
||
/** | ||
* Ends recording a clip. | ||
* This method is an alias to {@see endWidget()}. | ||
*/ | ||
public function endClip(): void | ||
{ | ||
$this->endWidget(ClipWidget::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace yii1tech\mailer\widgets; | ||
|
||
use CWidget; | ||
|
||
/** | ||
* ClipWidget records its content and makes it available elsewhere. | ||
* | ||
* This is a replacement for standard {@see \CClipWidget}, which can integrate into {@see \yii1tech\mailer\View}. | ||
* | ||
* @see \yii1tech\mailer\View::beginClip() | ||
* | ||
* @author Paul Klimov <[email protected]> | ||
* @since 1.0 | ||
*/ | ||
class ClipWidget extends CWidget | ||
{ | ||
/** | ||
* @var \CBaseController|\yii1tech\mailer\View view, which renders this widget. | ||
*/ | ||
public $view; | ||
|
||
/** | ||
* @var bool whether to render the clip content in place. Defaults to false, | ||
* meaning the captured clip will not be displayed. | ||
*/ | ||
public $renderClip = false; | ||
|
||
/** | ||
* Starts recording a clip. | ||
*/ | ||
public function init() | ||
{ | ||
ob_start(); | ||
ob_implicit_flush(false); | ||
} | ||
|
||
/** | ||
* Ends recording a clip. | ||
* This method stops output buffering and saves the rendering result as a named clip in the controller. | ||
*/ | ||
public function run() | ||
{ | ||
$clip = ob_get_clean(); | ||
if ($this->renderClip) { | ||
echo $clip; | ||
} | ||
|
||
$this->view->getClips()->add($this->getId(), $clip); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
<?php | ||
/** | ||
* @var $this \yii1tech\mailer\View | ||
*/ | ||
?> | ||
<!--Header--> | ||
<?php echo $content; ?> | ||
Clip = <?php echo $this->getClips()->itemAt('test-clip') ?> | ||
<!--Footer--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters