Implemented For Developers: add function to Controller - isAjax

grisha2217

Active member
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>
 
Upvote 0
This suggestion has been implemented. Votes are no longer accepted.
Back
Top Bottom