Skip to content

Commit d90f001

Browse files
author
igor-chepurnoi
committed
update getContent function for parsing widgets in the cms pages
1 parent ddb6514 commit d90f001

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

README.md

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,43 @@ return [
112112
* Create the widget and add the static function. For example:
113113

114114
```php
115-
public static function insertList() {
116-
return self::widget([
117-
//your params
118-
]);
115+
namespace app\widgets;
116+
117+
use yii\base\Widget;
118+
119+
/**
120+
* Class MyWidget
121+
* @package app\widgets
122+
*/
123+
class MyWidget extends Widget
124+
{
125+
/**
126+
* @inheritdoc
127+
*/
128+
public function run()
129+
{
130+
parent::run();
131+
132+
echo 'Text from widget';
133+
}
134+
135+
/**
136+
* This function used for render the widget in the cms pages
137+
*
138+
* @return string
139+
*/
140+
public static function show()
141+
{
142+
return self::widget([
143+
// additional params
144+
]);
145+
}
119146
}
120147
```
121-
* When you create the page via admin panel add the following code to the content:
148+
* When you create the page via admin panel add the following code to the page content:
122149

123150
```
124-
\app\widgets\MyWidget:list() // call the function insertList placed in MyWidget class
151+
[[\app\widgets\MyWidget:show]]
125152
```
126153
2. You can use parameters in your page content, for example: {siteName}, {homeUrl}. For parsing this parameters you can use the `baseTemplateParams` property:
127154

@@ -142,4 +169,4 @@ return [
142169
],
143170
];
144171
}
145-
```
172+
```

models/CmsModel.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ public function findPage($url)
109109
/**
110110
* Returns content and replace widgets short codes
111111
*
112+
* Widget short code example: [[\app\widgets\SomeWidget:method]]
113+
*
112114
* @return string
113115
*/
114116
public function getContent()
@@ -127,7 +129,7 @@ public function getContent()
127129
private function replace($data)
128130
{
129131
$widget = explode(':', $data[1]);
130-
if (class_exists($class = $widget[0]) && method_exists($class, $method = 'insert' . ucfirst($widget[1]))) {
132+
if (class_exists($class = $widget[0]) && method_exists($class, $method = $widget[1])) {
131133
return call_user_func([$class, $method]);
132134
}
133135

0 commit comments

Comments
 (0)