• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Implemented For Developers: add function to Controller - isAjax

#1
Hello, i often use XenForo.ajax function to load a content from server. There are situations, when you don't need get all content from template, you don't want create new template, i suggest simple way:

Create isAjax function in XenForo_Controller class:
PHP:
public function isAjax()
    {
        if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
        {
            return true;
        }
      
        return false;
    }

we will use this function in actions:
1. Load or no load content if is ajax request
PHP:
public function actionTest()
    {
        if (!$this->isAjax())
        {
            // load something
        }
        else
        {
            // do another
        }
      
        $viewParams = array(
            // others params
            'isAjax' => $this->isAjax()
        );
    }
2. Display or no display content in template
Code:
<xen:if is="!{$isAjax}">
// display content only for non ajax requests
</xen:if>