<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Guillermo Rauch&#039;s Devthought &#187; tutorial</title>
	<atom:link href="http://www.devthought.com/tag/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.devthought.com</link>
	<description></description>
	<lastBuildDate>Mon, 30 Jan 2012 16:38:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Writing a PHP command line script that takes options</title>
		<link>http://www.devthought.com/2009/03/22/writing-a-simple-php-command-line-script-that-takes-options/</link>
		<comments>http://www.devthought.com/2009/03/22/writing-a-simple-php-command-line-script-that-takes-options/#comments</comments>
		<pubDate>Sun, 22 Mar 2009 15:41:03 +0000</pubDate>
		<dc:creator>Guillermo Rauch</dc:creator>
				<category><![CDATA[Tumble]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://devthought.com/?p=570</guid>
		<description><![CDATA[First of all, we make our file start like this. #!/usr/bin/env php This allows us to run the script without prefixing it with the &#8220;php&#8221; command, and instead we can run it like this: chmod +x myscript # This gives &#8230; <a href="http://www.devthought.com/2009/03/22/writing-a-simple-php-command-line-script-that-takes-options/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>First of all, we make our file start like this.</p>
<pre class='highlight ' lang="php">
#!/usr/bin/env php
</pre>
<p>This allows us to run the script without prefixing it with the &#8220;php&#8221; command, and instead we can run it like this:</p>
<pre class='highlight ' lang="bash">
chmod +x myscript  # This gives execution permissions to the script, do it only once
./myscript --first=option --second --third=option
</pre>
<p>If we want our script to take options like in the example above, we can use this snippet:</p>
<pre class='highlight ' lang="php">
$options = array();

foreach ($argv as $arg){
	preg_match('/\-\-(\w*)\=?(.+)?/', $arg, $value);
	if ($value &#038;&#038; isset($value[1]) &#038;&#038; $value[1])
		$options[$value[1]] = isset($value[2]) ? $value[2] : null;
}
</pre>
<p>The $options array will hold the supplied options. You can then use them like this:</p>
<pre class='highlight ' lang="php">
if (!isset($options['somevalue']))
	// show an error

if (isset($options['dosomething']))
	// do something
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.devthought.com/2009/03/22/writing-a-simple-php-command-line-script-that-takes-options/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>CSS+Javascript power. Fancy menu</title>
		<link>http://www.devthought.com/2007/01/29/cssjavascript-true-power-fancy-menu/</link>
		<comments>http://www.devthought.com/2007/01/29/cssjavascript-true-power-fancy-menu/#comments</comments>
		<pubDate>Mon, 29 Jan 2007 18:46:24 +0000</pubDate>
		<dc:creator>Guillermo Rauch</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[mootools]]></category>
		<category><![CDATA[mootools plugin]]></category>
		<category><![CDATA[release]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://devthought.com/?p=5</guid>
		<description><![CDATA[Let me introduce you to Fancy Menu: When it comes to creating the navigation part of your Website, the first thing you might think of is an unordered list that you style as tabs. Lately, such navbars are everywhere, as &#8230; <a href="http://www.devthought.com/2007/01/29/cssjavascript-true-power-fancy-menu/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Let me introduce you to Fancy Menu:</p>
<p><img src="http://devthought.com/wp-content/uploads/2007/01/fancymenu.png" alt="fancymenu" title="fancymenu" width="458" height="72" class="alignnone size-full wp-image-231" /></p>
<p>When it comes to creating the navigation part of your Website, the first thing you might think of is an unordered list that you <a href="http://unraveled.com/projects/css_tabs/">style</a> as <a href="http://www.alistapart.com/articles/slidingdoors/">tabs</a>. Lately, such navbars are everywhere, as many people believe they&#8217;ll make their site more Web 2.0-compatible. Personally, I just think they&#8217;re semantically better and accessible.</p>
<p>In this article I&#8217;ll go through the creation of a custom navigation bar with some cute Javascript effects that will certainly impress your friends. Thanks to the great <a href="http://www.mootools.net">Mootools</a> library, this beauty is contained in 1.5kb. Not only that, but it&#8217;s also cross browser (tested on Internet Explorer 6/7, Firefox and Safari) and accessible!</p>
<p><span id="more-44"></span></p>
<h3>Introduction</h3>
<p>Every time that I know I&#8217;m going to use Javascript to alter the behavior or look of something, I try to come up with a simple markup, and make sure it renders perfectly with Javascript turned off. To illustrate this point, imagine that you want to make an element wider on rollover. The property Javascript would change is <em>width:</em>, so I make sure first that my style works when I modify the width randomly.</p>
<p>For this menu, as we&#8217;ll be having a movable element that acts as the background, we should first make sure that just by using css, we can freely move it and that it won&#8217;t affect the display of the menu. If you didn&#8217;t do this, when you&#8217;re coding the JS and face a bug, you&#8217;ll find yourself wondering if it is caused by the CSS, the Javascript, the browser?</p>
<h3>Mark it up</h3>
<p>Just like any other navigation, we&#8217;re going to use an unordered list with some anchors:</p>
<pre class='highlight ' lang="html4strict" toolbar="0">
<div id="fancymenu">
<ul>
<li class="current" id="menu_home"><a href="#">Home</a></li>
<li id="menu_plantatree"><a href="#">Plant a tree</a></li>
<li id="menu_travel"><a href="#">Travel</a></li>
<li id="menu_rideanelephant"><a href="#">Ride an elephant</a></li>
</ul>
</div>
</pre>
<p>This is the foundation of a semantically correct, degradable navigation structure.</p>
<h3>The CSS styling</h3>
<p>As I said before, it&#8217;s paramount that we create flawless, cross browser CSS code. Let&#8217;s get to it</p>
<p>The first problem we face is that it&#8217;s impossible to use the background property for the rounded box that follows your mouse, with the current CSS specs shared by most browsers. That forces us to add a new LI item that will act as the moving background.</p>
<p>We&#8217;re going to set position: relative to the unordered list, and position: absolute to the moving item, so that it&#8217;s easy to move it between the menu boundaries from Javascript. If you don&#8217;t quite understand how this works, I encourage you to quickly read <a href="http://www.barelyfitz.com/screencast/html-training/css/positioning/">this article about CSS positioning</a>. You&#8217;ll understand that if we simply set position: absolute to it, we&#8217;d have to do some hard, useless calculations Javascript side to positionate it correctly.</p>
<p>Then, this is the code we have so far:</p>
<pre class='highlight ' lang="css" line="1">
#fancymenu {
  position: relative;
  height: 29px;
  width: 421px;
  background: url('images/bg.gif') no-repeat top;
  padding: 15px;
  margin: 10px 0;
  overflow: hidden;
}

#fancymenu ul {
  padding: 0;
  margin: 0;
}

/* Don't apply padding here (offsetWidth will differ in IE)
If you need padding add it to the child anchor */
#fancymenu ul li {
  float: left;
  list-style: none;
}

#fancymenu ul li a {
  text-indent: -500em;
  z-index: 10;
  display: block;
  float: left;
  height: 30px;
  position: relative;
  overflow: hidden;
}
</pre>
<p>So far it&#8217;s quite easy, and I included some comments for the tricky parts. The text-indent property is used to hide the text without adding extra markup, and keeping it accesible.</p</p>
<p>Now, we have to add the background images for each link:</p>
<pre class='highlight ' lang="css" line="1">
#menu_home a {
  width: 59px;
  background: url('images/menu_home.png') no-repeat center !important;
  background: url('images/menu_home.gif') no-repeat center; // ie!
}

#menu_plantatree a {
  width: 119px;
  background: url('images/menu_plantatree.png') no-repeat center !important;
  background: url('images/menu_plantatree.gif') no-repeat center;
}

#menu_travel a {
  width: 70px;
  background: url('images/menu_travel.png') no-repeat center !important;
  background: url('images/menu_travel.gif') no-repeat center;
}

#menu_rideanelephant a {
  width: 142px;
  background: url('images/menu_rideanelephant.png') no-repeat center !important;
  background: url('images/menu_rideanelephant.gif') no-repeat center;
}
</pre>
<p>In the following section you&#8217;ll see why we use .gif images for Internet Explorer by using the !important hack.</p>
<h3>The moving background</h3>
<p>As we discussed, there&#8217;s a LI that moves in a lower layer and stretches to take the shape of each element. Because of its structure, we&#8217;re going to implement something similar to the <a href="http://alistapart.com/articles/slidingdoors">Sliding Doors technique</a>, but without text.</p>
<p>Its markup would be the following:</p>
<pre class='highlight ' lang="html4strict">
<li class="background">
<div class="left">&nbsp;</div>
</li>
</pre>
<p>As it doesn&#8217;t have any semantic role in the unordered list, we&#8217;re going to include it from Javascript. Of course, for testing, you can include it first manually and then remove it. This is the style for it:</p>
<pre class='highlight ' lang="css" line="1">
#fancymenu li.background {
  background: url('images/bg_menu_right.png') no-repeat top right !important;
  background: url('images/bg_menu_right.gif') no-repeat top right;
  z-index: 8;
  position: absolute;
  visibility: hidden;
}

#fancymenu .background .left {
  background: url('images/bg_menu.png') no-repeat top left !important;
  background: url('images/bg_menu.gif') no-repeat top left;
  height: 30px;
  margin-right: 9px; /* 7px is the width of the rounded shape */
}
</pre>
<p>The use of this technique is one of the main reasons why we don&#8217;t use filters to display the PNGs in Internet Explorer. You can&#8217;t decide the position of the background with them, which would make the right corner side display above the left part. Read this article about the <a href="http://www.themaninblue.com/writing/perspective/2004/06/18/">png hack limitations</a> to find out more. Another reason is that Microsoft is updating users to IE7 automatically, which supports png perfectly.</p>
<p>Keep in mind, as well, that when you export the .gifs you&#8217;ll have to set the Matte to match the background color, otherwise everything will look really bad. This picture illustrates what your images should look like:</p>
<p><img src="/wp-content/articles/fancy-menu/images/comparison.gif" alt="PNG and GIF comparison" /></p>
<h3>Scripting it</h3>
<p>Thanks to our smart CSS code, our Javascript is very short and simple. Its job is limited to adding the extra background markup, and of course the effects for shrinking and moving it.</p>
<p>We&#8217;re just going to need Mootools&#8217; <strong>Fx.Style.js</strong>, <strong>Dom.js</strong>, and of course their dependencies. For this article&#8217;s example, I also used a custom transition found in the <strong>Fx.Transitions</strong> package (remember that transitions are what make the movement of the background vary). It&#8217;s coded in the form of a Class, so that it&#8217;s possible to initialize several menus on the same page.</p>
<pre class='highlight ' lang="javascript" line="1">
var SlideList = new Class({
	initialize: function(menu, options) {
		this.setOptions(this.getOptions(), options);

		this.menu = $(menu), this.current = this.menu.getElement('li.current');

		this.menu.getElements('li').each(function(item){
			item.addEvent('mouseover', function(){ this.moveBg(item); }.bind(this));
			item.addEvent('mouseout', function(){ this.moveBg(this.current); }.bind(this));
			item.addEvent('click', function(event){ this.clickItem(event, item); }.bind(this));
		}.bind(this));

		this.back = new Element('li').addClass('background').adopt(new Element('div').addClass('left')).injectInside(this.menu);
		this.back.fx = this.back.effects(this.options);
		if(this.current) this.setCurrent(this.current);
	},

	setCurrent: function(el, effect){
		this.back.setStyles({left: (el.offsetLeft)+'px', width: (el.offsetWidth)+'px'});
		(effect) ? this.back.effect('opacity').set(0).start(1) : this.back.setOpacity(1);
		this.current = el;
	},

	getOptions: function(){
		return {
			transition: Fx.Transitions.sineInOut,
			duration: 500, wait: false,
			onClick: Class.empty
		};
	},

	clickItem: function(event, item) {
		if(!this.current) this.setCurrent(item, true);
		this.current = item;
		this.options.onClick(new Event(event), item);
	},

	moveBg: function(to) {
		if(!this.current) return;
		this.back.fx.custom({
			left: [this.back.offsetLeft, to.offsetLeft],
			width: [this.back.offsetWidth, to.offsetWidth]
		});
	}
});

SlideList.implement(new Options);
</pre>
<p>Finally, it&#8217;s time to start it. Just create the object, by passing the id and desired options. The following example shows how to do it when the page DOM tree is loaded.</p>
<pre class='highlight ' lang="javascript">
window.addEvent('domready', function() {
	new SlideList($E('ul', 'fancymenu'), {transition: Fx.Transitions.backOut, duration: 700, onClick: function(ev, item) { ev.stop(); }});
});
</pre>
<p>The script first looks for the element that has the current class. If it finds it, it positions the background behind it. If it doesn&#8217;t, it waits till the user first click on some item to set the &#8216;current&#8217; class. This comes in very handy for menus meant for user selection, like the example below, instead of menus with links to actual URLs.</p>
<p>There&#8217;s an onClick option, which calls a function with an <a href="http://docs.mootools.net/files/Native/Event-js.html">Event</a> object, and the clicked element object reference as parameters. You can also change the effect duration, transition, etc.</p>
<h3>Extend it</h3>
<p>If you&#8217;ve made it this far, you must&#8217;ve noticed that it hasn&#8217;t been dead easy. In fact, the tutorial is not aimed solely to teach you how to create a menu, but for you to understand the possibilities you have using CSS and Javascript to make something stand out, and at the same time provide some tips to get you started if you want to create your own.</p>
<p>Here&#8217;s another example, using the very same Javascript class!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devthought.com/2007/01/29/cssjavascript-true-power-fancy-menu/feed/</wfw:commentRss>
		<slash:comments>690</slash:comments>
		</item>
	</channel>
</rss>

