【问题标题】:OctoberCMS - How to access properties of a component in a function inside of the php section?OctoberCMS - 如何在 php 部分的函数中访问组件的属性?
【发布时间】:2019-11-28 15:25:43
【问题描述】:

是否可以访问组件的属性,例如OnStart() 函数中的 blogPosts?如果有,怎么做?

title = "Blog Category"
url = "/blog/category/:slug/:page?"

[blogPosts]
categoryFilter = ":slug"
postsPerPage = 3
==
function onStart()
{
    // Access categoryFilter property
    $catfilter = ???
    ...
}
==

【问题讨论】:

  • 不明白你在问什么。更清楚你的问题。
  • 您会看到 OnStart() 函数。我想在那里读取 categoryFilter 属性的值。此属性是在组件 blogPosts 的属性中的函数之外设置的。简单来说,我正在寻找这样的解决方案 $catfilter = categoryFilter.不幸的是,categoryFilter 不能以这种方式访问​​。
  • 我想我可以按如下方式访问属性:this.page.components['blogPosts'].properties['postsPerPage']。但这仅适用于树枝内的功能之外,例如{{ 转储(this.page.components['blogPosts'].properties['postsPerPage']) }}。我想我应该再喝一两杯啤酒。
  • @creg 回答了你的问题。

标签: php octobercms octobercms-plugins


【解决方案1】:

我要问你为什么需要访问组件属性?

你可以访问参数':slug'$this->param('slug');

这应该适用于您获取属性数组。

function onStart() {
    $this['properties'] = collect($this->page->components['blogPosts'])->last()['postsPerPage'];
}

我正在添加另一种从数组中获取属性的方法。我假设属性可能总是最后的,但如果不是:

    $array = collect($this->page->components['blogPosts'])->toArray();
    $properties = [];
    foreach ( $array as $key => $property) {
        if ( strpos($key, 'properties') !== false ) {
            $properties[$key] = $property;
        }
    }
    $this['properties'] = $properties['postsPerPage'];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-26
    • 2016-09-05
    • 2021-05-21
    • 2013-10-30
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多