在ajax处理程序上从php获取{%page%}对象


Get {% page %} object from php on ajax handler

我正在使用OctoperCms,并试图从ajax请求返回页面内容。例如,当单击某个内部链接时,我想从ajax获得类似于树枝{% page %}的页面对象。

public function onInternalLink(){
 $href =  post('href');
 return [
        'title'=>'', //here i want {{ page.title }}
        'content' => '', //and here {% page %} like this variable in layout.
             ];
     }
 }

我的js代码是

  $.request('onInternalLink', {
       data: {href: u}, // var u is the requested url to return
        success: function() {
            console.log('Almost october');
        }
    })
 }

我尝试创建新的CmsObject,并尝试使用parseMarkup()方法,尝试pageCycle(),但没有成功。我没有找到从php脚本中获取{% page %}对象的方法,有这样的方法吗?

在您的PHP中,您可以使用$this->page访问当前页面。因此,您的php将变成:

public function onInternalLink() {
    $href =  post('href');
    return [
        'title'=> $this->page->title,
        'content' => $this->getContentsFromFile($this->page->baseFileName),
    ];
}

在此之后,您只需要编写从页面的baseFileName中获取html内容的逻辑(在上面的示例中,我将其包装为$this->getContentsFromFile())。

$this-> page提供了更多变量-请在此处阅读-https://octobercms.com/docs/cms/pages#page-变量