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 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题