[trick] Memanfaatkan Assets Helper Untuk Managment File Script

Bismillah…

Tulisan kali ini, lagi lagi sekedar pengalaman gw aja, pas ngembangin aplikasi ditempat kerja gw. studi kasusnya kaya gini.

Wah, script js/css plugin gw bentrok neh, padahal kan gw pake cuman di halaman ini doank!

Yah, script js/ css yang wajib di load biasanya kan JQuery tapi misal kita punya plugin yang cuman halaman login doank atau sebuah fungsi yang digunakan sesekali doank, ya kali mesti kita load plugin js misalkan, gak perlu, makanya kita butuh yang namanya management assets, khususnya di framework codeigniter.

Gw cuman sekedar pemakai, ada 2 assets manager yang biasa gw pake ini diantaranya:

  1. https://github.com/sekati/codeigniter-asset-helper
  2. http://www.thephpcode.com/blog/view/css-and-javascript-asset-helper-for-codeigniter.html
  3. https://github.com/gpedro/CodeIgniter-Assets-Helper

Kalo gw pake nomer 2 di tempat kerja gw sekarang, kurang lebih sama caranya:

1. Buat file, namanya assets_helper.php simpan di folder helper

berikut scriptnya:

<?php // source: http://www.thephpcode.com/blog/view/css-and-javascript-asset-helper-for-codeigniter.html // modified by Desta function asset_url($asset_name, $asset_type = NULL) { $obj = & get_instance(); // $base_url = $obj->config->item('base_url');
	$base_url = ASSETURL; // ubah sesuai kebutuhan
	$asset_root = 'assets/';
	$asset_location = $base_url;

	if (is_array($asset_name))
	{
		$cachename = md5(serialize($asset_name));
		$asset_location .= 'cache/' . $cachename . '.' . $asset_type;
		if(!is_file($asset_root . 'cache/' . $cachename . '.' . $asset_type))
		{
			$out = fopen($asset_root . 'cache/' . $cachename . '.' . $asset_type, "w");
			foreach($asset_name as $file)
			{
				$file_content = file_get_contents($asset_root . $asset_type . '/' . $file);
				fwrite($out, $file_content);
			}
			fclose($out);
		}
	}
	else
	{
		$asset_location .= $asset_type . '/' . $asset_name;
	}
	return $asset_location;
}

function css_asset($asset_name,$attributes = array()) {
	$attribute_str = _parse_asset_html($attributes);
	// return '<link href="' . asset_url($asset_name,'css') . '" rel="stylesheet" type="text/css" />';
	return '<link href="' . asset_url($asset_name,'css') . '" rel="stylesheet" type="text/css"' . $attribute_str . ' />';
}

function js_asset($asset_name) {
	return '<script type="text/javascript" src="' . asset_url($asset_name,'js') . '"></script>';
}

function image_asset($asset_name, $module_name = '', $attributes = array()) {
	$attribute_str = _parse_asset_html($attributes);
	return '<img src="' . asset_url($asset_name,'img') . '"' . $attribute_str . ' />';
}

function _parse_asset_html($attributes = NULL) 
{
	if (is_array($attributes)) {
		// empty string
		$attribute_str = '';
		foreach ($attributes as $key => $value) {
			$attribute_str .= '' . $key . '="' . $value . '"';
		}
		return $attribute_str;
	}else{
		return '';
	}

	// 	$attribute_str = '';
	// foreach ($attributes as $key => $value)
	// 	$attribute_str .= '' . $key . '="' . $value . '"';
	// endforeach;
	// 	return $attribute_str;
	// endif;
	// return '';
}

kurang lebih struktur folder standard dah, kayak gini

struktur foler

tinggal panggil fungsi helper di template/ fungsi yang dikehendaki dah..

echo css_asset('mystyle.css');
echo image_asset('myimage.css');
echo js_asset('myscript.js');

Have a nice day

Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: