Skip to content

Instantly share code, notes, and snippets.

@Abban
Last active December 11, 2015 22:58
Show Gist options
  • Save Abban/4673002 to your computer and use it in GitHub Desktop.
Save Abban/4673002 to your computer and use it in GitHub Desktop.
ExpressionEngine plugin for checking if a request is an ajax one in a template.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Is Ajax Class
*
* @package ExpressionEngine
* @category Plugin
* @author Abban Dunne
* @copyright Copyright (c) 2012, Webtogether
* @license MIT
* @link http://www.webtogether.ie/
*/
$plugin_info = array(
'pi_name' => 'Is Ajax',
'pi_version' => '1.0',
'pi_author' => 'Abban Dunne',
'pi_author_url' => 'http://www.webtogether.ie/',
'pi_description' => 'Returns true or false depending on if the current request is an ajax one.',
'pi_usage' => Is_ajax::usage()
);
class Is_ajax{
public $return_data = '';
public function __construct(){
$this->EE =& get_instance();
$tagdata = $this->EE->TMPL->tagdata;
$conds['ajax'] = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest');
$tagdata = $this->EE->functions->prep_conditionals($tagdata, $conds);
$tagdata = $this->EE->TMPL->swap_var_single("ajax", $conds['ajax'], $tagdata);
$this->return_data = $tagdata;
}
/**
* Usage
*
* This function describes how the plugin is used.
*
* @access public
* @return string
*/
public static function usage(){
ob_start(); ?>
You use the plugin like this:
{exp:is_ajax}
{if ajax}
Do stuff
{if:else}
Do other stuff
{/if}
{/exp:is_ajax}
<?php $buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
}
/* End of file pi.is_ajax.php */
/* Location: ./system/expressionengine/third_party/is_ajax/pi.is_ajax.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment