Skip to content

Instantly share code, notes, and snippets.

@ryanneufeld
Last active December 17, 2015 02:19
Show Gist options
  • Select an option

  • Save ryanneufeld/5534369 to your computer and use it in GitHub Desktop.

Select an option

Save ryanneufeld/5534369 to your computer and use it in GitHub Desktop.
Place this file in the application/core directory of your CodeIgniter project and you will find comments in your HTML at the start and end of every view.
<?php defined('BASEPATH') || exit('No direct script access allowed');
/*
Override CI loader to add comments indicating the location and count of views as they are loaded.
Copyright (C) 2012 Ryan Neufeld
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* Custom extension of the CI_Loader class to provide convenience methods for developers.
*
* @author Ryan Neufeld <ryan@neucode.org>
*
*/
class MY_Loader extends CI_Loader
{
/**
* List of views and a counter for every time one is loaded.
*
* @var array
*/
public $views = array();
/**
* Override CI's view loader.
*
* @param string $view The view name to load.
* @param array $vars View variables.
* @param boolean $return Return a string (true) or just load the view.
*
* @return mixed
*/
public function view( $view, $vars = array(), $return = FALSE )
{
// Skip adding comments to views loaded outside development environment.
if (ENVIRONMENT !== "development")
{
return parent::view( $view, $vars, $return );
}
// Keep track of number of times a view is loaded.
if (!isset($this->views[$view]))
{
$this->views[$view] = 0;
}
$this->views[$view]++;
if ($return)
{
$result = parent::view( $view, $vars, $return );
return "<!-- start " . APPPATH . "views/$view ({$this->views[$view]}) -->\n{$result}<!-- end " . APPPATH . "views/$view ({$this->views[$view]}) -->\n";
}
echo "<!-- start " . APPPATH . "views/$view ({$this->views[$view]}) -->\n";
echo parent::view( $view, $vars, TRUE);
echo "\n<!-- end " . APPPATH . "views/$view ({$this->views[$view]}) -->\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment