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 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: