drd94483 2018-02-26 10:02
浏览 30
已采纳

在代码中访问OctoberCMS页面?

I have a Static Page in OctoberCMS named General that has a bunch of site-wide settings including phone number and address. Is it possible to access this page in code to read these settings from its ViewBag?

UPDATE: a plugin was created with the following, where properties like twitter_username for example can now be accessed in templates with {{ general('twitter_username') }}:

use System\Classes\PluginBase;
use RainLab\Pages\Classes\Page;
use Cms\Classes\Theme;

class Plugin extends PluginBase
{
    private static $generalViewBag = null;

    public function registerMarkupTags()
    {
        return [
            'functions' => [
                'general' => function($var) {
                    if (self::$generalViewBag === null) {
                        self::$generalViewBag = Page::load(Theme::getActiveTheme(), 'general')
                            ->getViewBag();
                    }

                    return self::$generalViewBag->$var;
                },
            ],
        ];
    }
}

The twitter_username form field was added to the General page in the backend using a separate plugin:

use System\Classes\PluginBase;
use Event;

class Plugin extends PluginBase
{
    public function boot()
    {
        Event::listen('backend.form.extendFields', function($widget) {
            if (! $widget->getController() instanceof \RainLab\Pages\Controllers\Index) {
                return;
            }
            if (! $widget->model instanceof \RainLab\Pages\Classes\Page) {
                return;
            }

            switch ($widget->model->fileName) {
                case 'general.htm':
                    $widget->addFields([
                        'viewBag[twitter_username]' => [
                            'label' => 'Twitter username',
                            'type' => 'text',
                            'tab' => 'Social Media',
                        ],
                    ], 'primary');
                    break;
            }
        });
    }
}
  • 写回答

1条回答 默认 最新

  • doudi2431 2018-02-26 12:42
    关注

    yes you can do it actually you need to use this code in page life-cycle method

    In page code block you can use something like this OR anywhere else

    use RainLab\Pages\Classes\Page as StaticPage;
    
    function onStart() {    
        $pageName = 'static-test';
        $staticPage = StaticPage::load($this->controller->getTheme(), $pageName);
        dd($staticPage->viewBag);    
    }
    

    let me know if it you find any issues

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 综合布线实例设计,就好看好看不恐怖可好滤镜好聚
  • ¥15 使用moviepy库视频合并时出错
  • ¥30 FLUENT液固传质UDF
  • ¥15 怎么看梯度直方图以,怎么判断梯度消失/爆炸,怎么解决
  • ¥15 aspnetdll文件访问拒绝
  • ¥15 wpf中在模版中寻找元素
  • ¥15 MFC平台生成指定圆
  • ¥15 jmeter出现403
  • ¥500 求华为P30PRO手机硬盘数据恢复
  • ¥15 关于#vscode#的问题:ESP32开发板对接MQTT实现小灯泡的开关