Part of the EllisLab Network
   
 
Snappy Source - An image, stylesheet and javascript helper
Posted: 05 June 2007 11:18 PM   [ Ignore ]  
Grad Student
Avatar
Rank
Total Posts:  47
Joined  05-29-2007

Snappy Source assists in embedding common source files such as images, stylesheets and javascripts into your CodeIgniter application.

A complete user guide can be found in the download file

Download Snappy Source
Snappy Source v1.0

Visit the Wiki
Snappy Source Wiki

Quick Examples:

Loading Snappy Source

this->load->helper('snappy_source');

Embedding a single image

<?=snappy_image('pic.jpg', 'My Alt Tag', 'CSS Class');?>

Embedding a single stylesheet

<?=snappy_style('style.css', 'screen');?>

Embedding a single javascript

<?=snappy_script('script.js');?>

Embedding multiple images, stylesheets and javascripts can be found in the User Guide found in the download file.

Please note that this is the first helper I’ve written for CI. Any comments or suggestions would be great! smile

 Signature 

Blog | Developing on: XAMPP v1.6.2 | Coding with: PHP v4.4.7, CodeIgniter v1.5.3, Prototype v1.5.1

Profile
 
 
Posted: 06 June 2007 01:28 AM   [ Ignore ]   [ # 1 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1749
Joined  06-23-2006

Nice and elegant!

 Signature 

Mac OS X 10.4.10, Apache 1.3.3, PHP 5.2.3, CodeIgniter 1.5.x., baby!

Profile
 
 
Posted: 06 June 2007 01:33 AM   [ Ignore ]   [ # 2 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1749
Joined  06-23-2006

Hmmm, I didn’t realize that Snappy had pre-defined locations for the “source” files. I, among others, don’t keep the application folder in a web-accessible location, but rather above the webroot for increased security. I could see the Snappy helpers taking a root-relative path (my preference) to a source file, rather than just the name and building the path internally.

Thoughts?

 Signature 

Mac OS X 10.4.10, Apache 1.3.3, PHP 5.2.3, CodeIgniter 1.5.x., baby!

Profile
 
 
Posted: 06 June 2007 01:37 AM   [ Ignore ]   [ # 3 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1749
Joined  06-23-2006

Just an observation. The following:

$htmlStr .= '<link rel="stylesheet" type="text/css" href="'.$styleFile.'" media="';
if(!empty(
$styleArr[1]))
{
    $htmlStr
.= $styleArr[1];
}
else
{
    $htmlStr
.= $mediaType;
}
$htmlStr
.= '" />'."\n";

could be written as:

$htmlStr .= '<link rel="stylesheet" type="text/css" href="'.$styleFile.'" media="';
$htmlStr .= (!empty($styleArr[1]) ? $styleArr[1] : $mediaType).'" />'."\n";

(edited for clarity)

 Signature 

Mac OS X 10.4.10, Apache 1.3.3, PHP 5.2.3, CodeIgniter 1.5.x., baby!

Profile
 
 
Posted: 06 June 2007 01:41 AM   [ Ignore ]   [ # 4 ]  
Grad Student
Avatar
Rank
Total Posts:  64
Joined  12-28-2006

nice, but I think dont need to every time call snappy_basedir
if can write

$newCI =& get_instance();
$baseDir = $newCI->config->slash_item('base_url');
$sourceDir = 'system/application/sources/';
$GLOBALS['base_dir] = $baseDir.$sourceDir.$typeDir;


// Generate Image HTML
function snappy_image($fileName, $imgAlt = '', $imgClass = '
') {
// and in every function use
global $base_dir;

I dont understand why all php coders so scary global variables?

Profile
 
 
Posted: 06 June 2007 01:46 AM   [ Ignore ]   [ # 5 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1749
Joined  06-23-2006
MpaK69 - 06 June 2007 01:41 AM

I dont understand why all php coders so scary global variables?

Globals are fine in a tightly-controlled environment (one programmer managing all the code), but they can get messy when combining code from multiple sources. Object-oriented programming solves that problem by introducing scope (separate namespaces) so variables don’t conflict with each other.

 Signature 

Mac OS X 10.4.10, Apache 1.3.3, PHP 5.2.3, CodeIgniter 1.5.x., baby!

Profile
 
 
Posted: 06 June 2007 01:48 AM   [ Ignore ]   [ # 6 ]  
Grad Student
Avatar
Rank
Total Posts:  64
Joined  12-28-2006

so make global config container
as
$GLOBALS[‘ci_config’] = array();
$ci_config[‘base_dir’] = ‘...’

Profile
 
 
Posted: 06 June 2007 08:54 AM   [ Ignore ]   [ # 7 ]  
Grad Student
Avatar
Rank
Total Posts:  47
Joined  05-29-2007

Thanks for the feedback!


CF:
Snappy does have predefined directories, but you can easily edit this in the helpers PHP source. I’m working on implementing an easier way of doing this with the ability of also trying to create the directories you define if they don’t exist.

Thanks for the thoughts on the code. The reason I’ve done it this way is its easier for me to quickly scan through when coming back for updates and editing. smile


MpaK69:
Thanks for your thoughts about the globals. I realize the benefits, but ever since I started programming in PHP, I’ve learned globals are a no no. I guess I just do it from habit now smile

I’m going to look into that config container idea. Thanks!

 Signature 

Blog | Developing on: XAMPP v1.6.2 | Coding with: PHP v4.4.7, CodeIgniter v1.5.3, Prototype v1.5.1

Profile
 
 
Posted: 06 June 2007 11:48 PM   [ Ignore ]   [ # 8 ]  
Grad Student
Avatar
Rank
Total Posts:  64
Joined  12-28-2006

:agree:

Some ideas, will be nice to remake snappy_img parameters as array

snappy_image($fileName, $imgAlt = '', $imgClass = '')

to

snappy_image($fileName, &$params)

because will be greate to call as

<?=snappy_image('logo.gif', array('alt'=>'LOGO', 'title'=>'SLOGAN', 'class' => 'myclass', 'id' => 'logoID'...) );?>
Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120626 Total Logged-in Users: 68
Total Topics: 126656 Total Anonymous Users: 1
Total Replies: 665795 Total Guests: 511
Total Posts: 792451    
Members ( View Memberlist )