-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dashboard object variables for template (#690)
* dashboard object variables for template * changelog ref * add merge
- Loading branch information
Showing
3 changed files
with
88 additions
and
2 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
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 |
---|---|---|
|
@@ -4,13 +4,18 @@ | |
|
||
use Yii; | ||
use luya\admin\base\DashboardObjectInterface; | ||
use luya\helpers\StringHelper; | ||
use yii\base\BaseObject; | ||
|
||
/** | ||
* Base Implementation of an Dashboard Object. | ||
* | ||
* This provides the setters and getters from the {{luya\admin\base\DashboardObjectInterface}}. | ||
* | ||
* @property string $template | ||
* @property string $outerTemplateContent | ||
* @property string $dataApiUrl | ||
* @property string $title | ||
* @author Basil Suter <[email protected]> | ||
* @since 1.0.0 | ||
*/ | ||
|
@@ -25,6 +30,28 @@ abstract class BaseDashboardObject extends BaseObject implements DashboardObject | |
* @return string Returns the outer template string which can contain the {{template}} variable, but don't have to. | ||
*/ | ||
abstract public function getOuterTemplateContent(); | ||
|
||
/** | ||
* Option content parser varaibles | ||
* | ||
* Pass additional variables into the template. | ||
* | ||
* ``` | ||
* 'variables' => [ | ||
* 'foo' => 'bar', | ||
* 'time' => function() { | ||
* return time(); | ||
* }, | ||
* 'title' => ['Key', 'Value'] // equals to: Yii::t('Key', 'Value') | ||
* ] | ||
* ``` | ||
* | ||
* The variables can be used as {{foo}} and {{time}} in the template. | ||
* | ||
* @var array An array with key and value, where the key is what is available in the template. | ||
* @since 4.2.0 | ||
*/ | ||
public $variables = []; | ||
|
||
private $_template; | ||
|
||
|
@@ -47,14 +74,30 @@ public function getTemplate() | |
} | ||
|
||
/** | ||
* Parse the content will replace {{dataApiUrl}}, {{title}}, {{template}} with the content from the object. | ||
* Parse the content will replace {{dataApiUrl}}, {{title}}, {{template}} variables with the content from the object. | ||
* | ||
* @param string $content The content to parse. | ||
* @return string | ||
*/ | ||
public function contentParser($content) | ||
{ | ||
return str_replace(['{{dataApiUrl}}', '{{title}}', '{{template}}'], [$this->getDataApiUrl(), $this->getTitle(), $this->_template], $content); | ||
$customVars = []; | ||
foreach ($this->variables as $key => $value) { | ||
if (is_array($value)) { | ||
list($category, $message) = $value; | ||
$customVars[$key] = Yii::t($category, $message); | ||
} else { | ||
$customVars[$key] = is_callable($value) ? call_user_func($value, $content, $this) : $value; | ||
} | ||
} | ||
|
||
$vars = [ | ||
'dataApiUrl' => $this->getDataApiUrl(), | ||
'title' => $this->getTitle(), | ||
'template' => StringHelper::template($this->_template, $customVars, false), | ||
]; | ||
|
||
return StringHelper::template($content, array_merge($vars, $customVars), true); | ||
} | ||
|
||
private $_dataApiUrl; | ||
|
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,42 @@ | ||
<?php | ||
|
||
namespace admintests\admin\dashboard; | ||
|
||
use admintests\AdminTestCase; | ||
use luya\admin\dashboard\ListDashboardObject; | ||
use luya\admin\dashboard\TableDashboardObject; | ||
|
||
class ListDashboardObjectTest extends AdminTestCase | ||
{ | ||
public function testListObject() | ||
{ | ||
$o = new ListDashboardObject(); | ||
$o->variables = [ | ||
'foo' => 'bar', | ||
'date' => time(), | ||
'eval' => function() { return time(); }, | ||
'tt' => ['app', 'Value'], | ||
]; | ||
$o->setTemplate('{{foo}} - {{ date }} - {{eval}} - {{tt}} - {{item.user.firstname}}'); | ||
$s = $o->getTemplate(); | ||
|
||
$this->assertStringContainsString('Value - {{item.user.firstname}}', $s); | ||
$this->assertStringContainsString('bar', $s); | ||
} | ||
|
||
public function testTableObject() | ||
{ | ||
$o = new TableDashboardObject(); | ||
$o->variables = [ | ||
'foo' => 'bar', | ||
'date' => time(), | ||
'eval' => function() { return time(); }, | ||
'tt' => ['app', 'Value'], | ||
]; | ||
$o->setTemplate('{{foo}} - {{ date }} - {{eval}} - {{tt}} - {{item.user.firstname}}'); | ||
$s = $o->getTemplate(); | ||
|
||
$this->assertStringContainsString('Value - {{item.user.firstname}}', $s); | ||
$this->assertStringContainsString('bar', $s); | ||
} | ||
} |