October/Winter CMS Static Pages的调用

October/Winter CMS Static Pages的调用

function onStart()
{
    $this['pages'] = \Rainlab\Pages\Classes\Page::sortBy('title')->lists('viewBag');

}

在模板中使用{% for page in pages %}来读取

    {% for page in pages if page.parent is empty %}
    <p>
 
        <h1>{{ page.title }}</h1>
        <h3>{{ page.url }}</h3>
    </p>
    {% for subpage in page.children %}
    <li class="c-overview__item">
        <a href="{{ subpage.url | app }}">
            <figure style="background-image:url({{ subpage.conver_image }})"></figure>
            <span class="c-overview__item-title">1.{{ subpage.title }}</span>
        </a>
    </li>
    {% endfor %}
    {% endfor %}

使用PageList来调用

    $staticPages = new \RainLab\Pages\Classes\PageList('myThemeName');
    $items = $staticPages->listPages();
    dd($items);
    
    foreach ($items as $item) {
        $pageUrl = $item->viewBag['url']; // url страницы
        $pageTitle = $item->viewBag['title']; // Заголовок страницы
        $pageMurkup = $item->attributes['markup']; // Контент страницы
    }

或者

$theme = \Cms\Classes\Theme::getActiveTheme();
$pages = \RainLab\Pages\Classes\Page::listInTheme($theme, true);

实战案例

根据URL获取该页面下面的所有子页面(包含二级页面),并且调用viewBag中的自定义字段的值

    $theme = \Cms\Classes\Theme::getActiveTheme();
    $router =new \RainLab\Pages\Classes\Router($theme);
    $this->pageObject = $router->findByUrl($this->page->url);
    $childs = $this->pageObject->getChildren();
    
    $pageChilds = [];
    
    foreach ($childs as $child)
    {
        $items = array();
        if(count($child->getChildren())){
            foreach($child->getChildren() as $item){
                $items[] = array(
                    "title" => $item->title,
                    "url" => $item->url,
                    "icon" => isset($item->viewBag['icon']) ? $item->viewBag['icon'] : "",
                    "headline" => isset($item->viewBag['headline']) ? $item->viewBag['headline'] : ""
                    );
            }
        }        
        $pageChilds[] = array(
            "title" => $child->title,
            "url" => $child->url,
            "content" => $child->getProcessedMarkup(),
            "icon" => isset($child->viewBag['icon']) ? $child->viewBag['icon'] : "",
            "headline" => isset($child->viewBag['headline']) ? $child->viewBag['headline'] : "",
            "items" => $items
        );

    }

    $this['childs'] = $pageChilds;

Post Comment