Skip to content

Commit 3714ebf

Browse files
author
Luke Towers
committed
Added ChildPages component
1 parent cf6f113 commit 3714ebf

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

Plugin.php

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function pluginDetails()
2727
public function registerComponents()
2828
{
2929
return [
30+
'\RainLab\Pages\Components\ChildPages' => 'childPages',
3031
'\RainLab\Pages\Components\StaticPage' => 'staticPage',
3132
'\RainLab\Pages\Components\StaticMenu' => 'staticMenu',
3233
'\RainLab\Pages\Components\StaticBreadcrumbs' => 'staticBreadcrumbs'

components/ChildPages.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php namespace RainLab\Pages\Components;
2+
3+
use Cms\Classes\ComponentBase;
4+
5+
class ChildPages extends ComponentBase
6+
{
7+
/**
8+
* @var \RainLab\Pages\Components\StaticPage A reference to the static page component
9+
*/
10+
protected $staticPageComponent;
11+
12+
/**
13+
* @var array Array of \RainLab\Pages\Classes\Page references to the child static page objects for the current page
14+
*/
15+
protected $childPages;
16+
17+
/**
18+
* @var array Child pages data
19+
* [
20+
* 'url' => '',
21+
* 'title' => '',
22+
* 'page' => \RainLab\Pages\Classes\Page,
23+
* 'viewBag' => array,
24+
* 'is_hidden' => bool,
25+
* 'navigation_hidden' => bool,
26+
* ]
27+
*/
28+
public $pages = [];
29+
30+
public function componentDetails()
31+
{
32+
return [
33+
'name' => 'rainlab.pages::lang.component.child_pages_name',
34+
'description' => 'rainlab.pages::lang.component.child_pages_description'
35+
];
36+
}
37+
38+
public function onRun()
39+
{
40+
// Check if the staticPage component is attached to the rendering template
41+
$this->staticPageComponent = $this->findComponentByName('staticPage');
42+
if ($this->staticPageComponent->pageObject) {
43+
$this->childPages = $this->staticPageComponent->pageObject->getChildren();
44+
45+
if ($this->childPages) {
46+
foreach ($this->childPages as $childPage) {
47+
$viewBag = $childPage->settings['components']['viewBag'];
48+
$this->pages = array_merge($this->pages, [[
49+
'url' => @$viewBag['url'],
50+
'title' => @$viewBag['title'],
51+
'page' => $childPage,
52+
'viewBag' => $viewBag,
53+
'is_hidden' => @$viewBag['is_hidden'],
54+
'navigation_hidden' => @$viewBag['navigation_hidden'],
55+
]]);
56+
}
57+
}
58+
}
59+
}
60+
}

components/childpages/default.htm

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{% if __SELF__.pages is not empty %}
2+
<ul>
3+
{% for page in __SELF__.pages %}
4+
<li>
5+
<a href="{{ page.url | app }}">{{ page.title }}</a>
6+
</li>
7+
{% endfor %}
8+
</ul>
9+
{% endif %}

docs/component-childpages.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Component: Child Pages (childPages)
2+
3+
## Purpose
4+
Outputs a list of child pages of the current page
5+
6+
## Default output
7+
8+
The default component partial outputs a simple nested unordered list:
9+
10+
```html
11+
<ul>
12+
<li>
13+
<a href="{{ page.url | app }}">{{ page.title }}</a>
14+
</li>
15+
</ul>
16+
```
17+
18+
You might want to render the list with your own code. The `childPages.pages` variable is an array of arrays representing the child pages. Each of the arrays has the following items:
19+
20+
Property | Type | Description
21+
-------- | ---- | -----------
22+
`url` | `string` | The relative URL for the page (use `{{ url | app }}` to get the absolute URL)
23+
`title` | `string` | Page title
24+
`page` | `RainLab\Pages\Classes\Page` | The page object itself
25+
`viewBag` | `array` | Contains all the extra data used by the page
26+
`is_hidden` | `bool` | Whether the page is hidden (only accessible to backend users)
27+
`navigation_hidden` | `bool` | Whether the page is hidden in automaticaly generated contexts (i.e menu)
28+
29+
## Example of custom markup for component
30+
31+
```html
32+
{% for page in childPages.pages %}
33+
<li><a href="{{ page.url | app }}">{{ page.title }}</a></li>
34+
{% endfor %}
35+
```

lang/en/lang.php

+2
Original file line numberDiff line numberDiff line change
@@ -137,5 +137,7 @@
137137
'static_menu_code_description' => 'Specify a code of the menu the component should output.',
138138
'static_breadcrumbs_name' => 'Static breadcrumbs',
139139
'static_breadcrumbs_description' => 'Outputs breadcrumbs for a static page.',
140+
'child_pages_name' => 'Child pages',
141+
'child_pages_description' => 'Displays a list of child pages for the current page',
140142
]
141143
];

0 commit comments

Comments
 (0)