<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/rss2full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Meio Código</title>
	
	<link>http://www.meiocodigo.com</link>
	<description>A peça que faltava para seu código.</description>
	<pubDate>Fri, 19 Dec 2008 15:11:39 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.5</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/meiocodigo" type="application/rss+xml" /><item>
		<title>Builtin template tags and filters in Django</title>
		<link>http://feeds.feedburner.com/~r/meiocodigo/~3/489735145/</link>
		<comments>http://www.meiocodigo.com/2008/12/19/builtin-template-tags-and-filters-in-django/#comments</comments>
		<pubDate>Fri, 19 Dec 2008 15:11:39 +0000</pubDate>
		<dc:creator>vbmendes</dc:creator>
		
		<category><![CDATA[Django]]></category>

		<category><![CDATA[Python]]></category>

		<category><![CDATA[builtins]]></category>

		<category><![CDATA[template filters]]></category>

		<category><![CDATA[template tags]]></category>

		<guid isPermaLink="false">http://www.meiocodigo.com/?p=264</guid>
		<description><![CDATA[A good feature of Django Framework is the template tags and template filters. But it sucks when you have to load the filters in each template like this:

{% load mytemplatetags %}

It was much better if you just use the filters, without the need to load it. The reason you have to load is to achieve [...]]]></description>
			<content:encoded><![CDATA[<p>A good feature of <a href="http://www.djangoproject.com">Django Framework</a> is the template tags and template filters. But it sucks when you have to load the filters in each template like this:</p>
<pre class="syntax-highlight:python">
{% load mytemplatetags %}
</pre>
<p>It was much better if you just use the filters, without the need to load it. The reason you have to load is to achieve a better performance. But there&#8217;s a function of the template module called add_to_builtins that solves this problem. You can easily define a template tag or filter as builtin and use it like the builtin django template tags and filters. Just put this code in a file that is loaded ever, like the __init__.py of your project.</p>
<pre class="syntax-highlight:python">
from django.template import add_to_builtins
add_to_builtins(&#039;path.to.templatetags.file&#039;)
</pre>
<p>Where &#8216;path.to.templatetags.file&#8217; is the path of the file containing the template tags. For example, I have an app, inside my project, named mytagsapp. Inside this app I have the module templatetags with a file named mytags.py with my custom tags. So I will have to call add_builtins(&#8217;mytagsapp.templatetags.mytags&#8217;). My project folder should look like this:</p>
<div id="attachment_265" class="wp-caption alignleft" style="width: 230px"><a href="http://www.meiocodigo.com/wp-content/uploads/2008/12/blog-folders.png"><img src="http://www.meiocodigo.com/wp-content/uploads/2008/12/blog-folders.png" alt="Folders structure" title="blog-folders" width="220" height="300" class="size-full wp-image-265" /></a><p class="wp-caption-text">Folders structure</p></div>
<p>Make sure your app is in the INSTALED_APPS setting in your settings.py project. It will guarantee that the __init__.py of you app will get called. So just put the code above in this file (red in image).</p>
<p>Remember to not use this feature with all your template filters as it will mean loss of performance.</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.meiocodigo.com%2F2008%2F12%2F19%2Fbuiltin-template-tags-and-filters-in-django%2F';
  addthis_title  = 'Builtin+template+tags+and+filters+in+Django';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.meiocodigo.com/2008/12/19/builtin-template-tags-and-filters-in-django/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.meiocodigo.com/2008/12/19/builtin-template-tags-and-filters-in-django/</feedburner:origLink></item>
		<item>
		<title>Removing Empty Labels from forms.ChoiceField in ModelFormSets</title>
		<link>http://feeds.feedburner.com/~r/meiocodigo/~3/486090786/</link>
		<comments>http://www.meiocodigo.com/2008/12/15/removing-empty-labels-from-formschoicefield/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 00:52:00 +0000</pubDate>
		<dc:creator>vbmendes</dc:creator>
		
		<category><![CDATA[Django]]></category>

		<category><![CDATA[Python]]></category>

		<category><![CDATA[formset]]></category>

		<guid isPermaLink="false">http://www.meiocodigo.com/?p=260</guid>
		<description><![CDATA[When you use a Model Choice Field in Django, it automatically creates an empty option in the generated select widget. But in some special cases, you don&#8217;t want this empty option to be there. So I decided to find the way to remove it. It&#8217;s much more simple then you think. Just create your model [...]]]></description>
			<content:encoded><![CDATA[<p>When you use a Model Choice Field in <a href="http://www.djangoproject.com">Django</a>, it automatically creates an empty option in the generated select widget. But in some special cases, you don&#8217;t want this empty option to be there. So I decided to find the way to remove it. It&#8217;s much more simple then you think. Just create your model declaring the model choice field like this:</p>
<pre class="syntax-highlight:python">
fieldname = forms.ModelChoiceField(queryset=RelationModel.objects,empty_label=None)
</pre>
<p>It&#8217;s simple like that. Just set the empty_label argument to None. But whe using ModelFormSets, there is a little more work to do. You have to create a new BaseModelFormSet like this:</p>
<pre class="syntax-highlight:python">
from django.forms.models import BaseModelFormSet

class MyBaseModelFormSet(BaseModelFormSet):
    def add_fields(self, form, index):
        super(MyBaseModelFormSet,self).add_fields(form,index)
        form.fields[&#039;fieldname&#039;] = forms.ModelChoiceField(queryset=RelationModel.objects,empty_label=None)
</pre>
<p>Pay atention that you have to override the add_fields method and change your field in the form fields array. Finally you have to pass this BaseModelFormSet as argument to the modelformset_factory:</p>
<pre class="syntax-highlight:python">
from django.forms.models import modelformset_factory

MyModelFormset = modelformset_factory(MyModel,formset=MyBaseModelFormSet)
</pre>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.meiocodigo.com%2F2008%2F12%2F15%2Fremoving-empty-labels-from-formschoicefield%2F';
  addthis_title  = 'Removing+Empty+Labels+from+forms.ChoiceField+in+ModelFormSets';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.meiocodigo.com/2008/12/15/removing-empty-labels-from-formschoicefield/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.meiocodigo.com/2008/12/15/removing-empty-labels-from-formschoicefield/</feedburner:origLink></item>
		<item>
		<title>meioMask 1.0.4 version released!</title>
		<link>http://feeds.feedburner.com/~r/meiocodigo/~3/478027006/</link>
		<comments>http://www.meiocodigo.com/2008/12/07/meiomask-104-version-released/#comments</comments>
		<pubDate>Mon, 08 Dec 2008 02:51:14 +0000</pubDate>
		<dc:creator>fabiomcosta</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.meiocodigo.com/?p=251</guid>
		<description><![CDATA[I present you meioMask 1.0.4 - a jQuery plugin for masking inputs.
meioMask for Mootools!
meioMask’s page at jquery.com
meioMask’s SVN project page
meioMask’s SVN
Features

Accepts paste event;
Haves fixed and reverse mask types ( allow number mask );
You can still use your hot keys and others (ex: ctrl+t, ctrl+f5, TAB …);
Supports metadata plugin;
Works with iPhone;
Allow default values;
Haves callbacks for invalid [...]]]></description>
			<content:encoded><![CDATA[<p>I present you meioMask 1.0.4 - a jQuery plugin for masking inputs.</p>
<p><a title="meioMask for Mootools!" href="/projects/moomeiomask">meioMask for Mootools!</a><br />
<a title="meioMask's page at jquery.com" href="http://plugins.jquery.com/project/meiomask">meioMask’s page at jquery.com</a><br />
<a title="meioMask's SVN project page" href="http://www.assembla.com/spaces/meiomask/stream">meioMask’s SVN project page</a><br />
<a title="meioMask's SVN" href="http://svn.assembla.com/svn/meiomask">meioMask’s SVN</a></p>
<h3>Features</h3>
<ul>
<li>Accepts paste event;</li>
<li>Haves fixed and reverse mask types ( allow number mask );</li>
<li>You can still use your hot keys and others (ex: ctrl+t, ctrl+f5, TAB …);</li>
<li>Supports <a href="http://docs.jquery.com/Plugins/Metadata/metadata" target="_blank">metadata</a> plugin;</li>
<li>Works with iPhone;</li>
<li>Allow default values;</li>
<li>Haves callbacks for invalid inputs, valid and overflow;</li>
<li>Haves function to mask strings;</li>
<li>Support for positive and negative numbers on reverse masks.</li>
</ul>
<h3>Changelog</h3>
<p>v1.0.4</p>
<ul>
<li>New mask type &#8216;infinite&#8217;, it allows infinite values at masks. See demos for a better understanding;</li>
<li>Added new function &#8216;unmaskVal&#8217; that returns the input value without the mask. See demos for a better understanding;</li>
<li>Removed a serious bug at IE that was fixing the caret at the end of the input;</li>
</ul>
<p>v1.0.3</p>
<ul>
<li>Callback functions now receive the char number as it second parameter, so it is possible to detect exactly which key has been pressed;</li>
<li>added the signed masks. It only works with the reverse mask. See demos for more details;</li>
<li>iPhone support improved. iPhone now works 100% better than the 1.0.2 version. I&#8217;ts logic have been changed, and now it&#8217;s working with full features. <img src='http://www.meiocodigo.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </li>
</ul>
<p>v1.0.2</p>
<ul>
<li>added input callbacks: onInvalid, onValid and onOverflow;</li>
<li>added support for default values;</li>
<li>can now be used like $().setMask({});</li>
<li>added the function $.mask.string(string,mask) that will mask a string (see demos);</li>
<li>now the value of the input is masked at the time the mask is applyed to it.</li>
</ul>
<p>v1.0.1</p>
<ul>
<li>added support for iphone;</li>
<li>removed a bug where a fixed char would be replaced by an inputed one.</li>
</ul>
<p>v1.0</p>
<ul>
<li>initial release.</li>
</ul>
<p>You can see the plugin page <a title="meioMask" href="http://meiocodigo.com/projects/meiomask/">here</a>. It contains documentation and examples. Please tell me any bug, new feature, english errors on documentation…. anything! I’ll be glad to hear your feedback and make the fixes. Hope it helps you!</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.meiocodigo.com%2F2008%2F12%2F07%2Fmeiomask-104-version-released%2F';
  addthis_title  = 'meioMask+1.0.4+version+released%21';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.meiocodigo.com/2008/12/07/meiomask-104-version-released/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.meiocodigo.com/2008/12/07/meiomask-104-version-released/</feedburner:origLink></item>
		<item>
		<title>meioMask 1.0.3 version released!</title>
		<link>http://feeds.feedburner.com/~r/meiocodigo/~3/439148752/</link>
		<comments>http://www.meiocodigo.com/2008/11/01/meiomask-103-version-released/#comments</comments>
		<pubDate>Sat, 01 Nov 2008 15:49:51 +0000</pubDate>
		<dc:creator>fabiomcosta</dc:creator>
		
		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[jQuery]]></category>

		<category><![CDATA[form]]></category>

		<category><![CDATA[input]]></category>

		<category><![CDATA[iphone]]></category>

		<category><![CDATA[mask]]></category>

		<category><![CDATA[paste]]></category>

		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://www.meiocodigo.com/?p=222</guid>
		<description><![CDATA[I present you meioMask 1.0.3 - a jQuery plugin for masking inputs.
Plugin page
jQuery meioMask plugin page
I&#8217;m going to centralize all the comments at the page of the project, so i&#8217;m not allowing comments here but you are free and motivated to comment at the meioMask&#8217;s project page
Features

Accepts paste event;
Haves fixed and reverse mask types ( [...]]]></description>
			<content:encoded><![CDATA[<p>I present you meioMask 1.0.3 - a jQuery plugin for masking inputs.</p>
<p><a title="meioMask" href="http://meiocodigo.com/meiomask/">Plugin page</a><br />
<a title="jQuery meioMask plugin page" href="http://plugins.jquery.com/project/meioMask">jQuery meioMask plugin page</a></p>
<p>I&#8217;m going to centralize all the comments at the page of the project, so i&#8217;m not allowing comments here but you are free and motivated to comment at the <a title="meioMask's project page" href="http://meiocodigo.com/projects/meiomask/">meioMask&#8217;s project page</a></p>
<h3>Features</h3>
<ul>
<li>Accepts paste event;</li>
<li>Haves fixed and reverse mask types ( allow number mask );</li>
<li>You can still use your hot keys and others (ex: ctrl+t, ctrl+f5, TAB …);</li>
<li>Supports <a href="http://docs.jquery.com/Plugins/Metadata/metadata" target="_blank">metadata</a> plugin;</li>
<li>Works with iPhone;</li>
<li>Allow default values;</li>
<li>Haves callbacks for invalid inputs, valid and overflow;</li>
<li>Haves function to mask strings;</li>
<li>Support for positive and negative numbers on reverse masks.</li>
</ul>
<h3>Changelog</h3>
<p>v1.0.3</p>
<ul>
<li>Callback functions now receive the char number as it second parameter, so it is possible to detect exactly which key has been pressed;</li>
<li>added the signed masks. It only works with the reverse mask. See demos for more details;</li>
<li>iPhone support improved. iPhone now works 100% better than the 1.0.2 version. I&#8217;ts logic have been changed, and now it&#8217;s working with full features. <img src='http://www.meiocodigo.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </li>
</ul>
<p>v1.0.2</p>
<ul>
<li>added input callbacks: onInvalid, onValid and onOverflow;</li>
<li>added support for default values;</li>
<li>can now be used like $().setMask({});</li>
<li>added the function $.mask.string(string,mask) that will mask a string (see demos);</li>
<li>now the value of the input is masked at the time the mask is applyed to it.</li>
</ul>
<p>v1.0.1</p>
<ul>
<li>added support for iphone;</li>
<li>removed a bug where a fixed char would be replaced by an inputed one.</li>
</ul>
<p>v1.0</p>
<ul>
<li>initial release.</li>
</ul>
<p>TODO</p>
<ul>
<li>Add auto-tab options, so the focus will change to the next input of the form when the user input size is equal to the size of the mask;</li>
<li>Your suggestion here.</li>
</ul>
<p>You can see the plugin page <a title="meioMask" href="http://meiocodigo.com/projects/meiomask/">here</a>. It contains documentation and examples. Please tell me any bug, new feature, english errors on documentation…. anything! I’ll be glad to hear your feedback and make the fixes. Hope it helps you!</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.meiocodigo.com%2F2008%2F11%2F01%2Fmeiomask-103-version-released%2F';
  addthis_title  = 'meioMask+1.0.3+version+released%21';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.meiocodigo.com/2008/11/01/meiomask-103-version-released/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.meiocodigo.com/2008/11/01/meiomask-103-version-released/</feedburner:origLink></item>
		<item>
		<title>Unique validation rule in CakePHP 1.2</title>
		<link>http://feeds.feedburner.com/~r/meiocodigo/~3/424544959/</link>
		<comments>http://www.meiocodigo.com/2008/10/18/unique-validation-rule-in-cakephp-12/#comments</comments>
		<pubDate>Sat, 18 Oct 2008 11:46:49 +0000</pubDate>
		<dc:creator>vbmendes</dc:creator>
		
		<category><![CDATA[CakePHP]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[unique]]></category>

		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://www.meiocodigo.com/?p=192</guid>
		<description><![CDATA[In this post I will show an example of how apply the unique validation rule in CakePHP 1.2. It&#8217;s very important to do in some cases, like saving users. You must make sure that the username is unique in your system. Lets see how it works:

var $validate = array(
	&#039;username&#039; =&#62; array(
		&#039;rule&#039; =&#62; array(&#039;isUnique&#039;),
		&#039;message&#039; =&#62; &#039;Username [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I will show an example of how apply the unique validation rule in CakePHP 1.2. It&#8217;s very important to do in some cases, like saving users. You must make sure that the username is unique in your system. Lets see how it works:</p>
<pre class="syntax-highlight:php">
var $validate = array(
	&#039;username&#039; =&gt; array(
		&#039;rule&#039; =&gt; array(&#039;isUnique&#039;),
		&#039;message&#039; =&gt; &#039;Username already in use.&#039;
	)
);
</pre>
<p>I wrote a custom function to validate it, but thanks to <a href="http://www.amillet.net">Dia</a> now I know the built-in validation rule.</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.meiocodigo.com%2F2008%2F10%2F18%2Funique-validation-rule-in-cakephp-12%2F';
  addthis_title  = 'Unique+validation+rule+in+CakePHP+1.2';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.meiocodigo.com/2008/10/18/unique-validation-rule-in-cakephp-12/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.meiocodigo.com/2008/10/18/unique-validation-rule-in-cakephp-12/</feedburner:origLink></item>
		<item>
		<title>meioMask 1.0.2 version released!</title>
		<link>http://feeds.feedburner.com/~r/meiocodigo/~3/422290309/</link>
		<comments>http://www.meiocodigo.com/2008/10/16/meiomask-jquery-form-mask-input-plugin/#comments</comments>
		<pubDate>Thu, 16 Oct 2008 04:11:02 +0000</pubDate>
		<dc:creator>fabiomcosta</dc:creator>
		
		<category><![CDATA[Ajax]]></category>

		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[jQuery]]></category>

		<category><![CDATA[form]]></category>

		<category><![CDATA[input]]></category>

		<category><![CDATA[iphone]]></category>

		<category><![CDATA[mask]]></category>

		<category><![CDATA[paste]]></category>

		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://www.meiocodigo.com/?p=99</guid>
		<description><![CDATA[I present you meioMask 1.0.2 - a jQuery plugin for masking inputs.
Plugin page
I’m going to centralize all the comments at the page of the project, so i’m not allowing comments here but you are free and motivated to comment at the meioMask’s project page.
Plugin official page
Features

Accepts paste event;
Haves fixed and reverse mask types ( allow [...]]]></description>
			<content:encoded><![CDATA[<p>I present you meioMask 1.0.2 - a jQuery plugin for masking inputs.</p>
<p><a title="meioMask" href="http://meiocodigo.com/meiomask/">Plugin page</a></p>
<p>I’m going to centralize all the comments at the page of the project, so i’m not allowing comments here but you are free and motivated to comment at the <a href="http://meiocodigo.com/projects/meiomask">meioMask’s project page</a>.</p>
<p><a title="Plugin Official page" href="http://plugins.jquery.com/project/meioMask">Plugin official page</a></p>
<h3>Features</h3>
<ul>
<li>Accepts paste event;</li>
<li>Haves fixed and reverse mask types ( allow number mask );</li>
<li>You can still use your hot keys and others (ex: ctrl+t, ctrl+f5, TAB …);</li>
<li>Supports <a href="http://docs.jquery.com/Plugins/Metadata/metadata" target="_blank">metadata</a> plugin;</li>
<li>Works with iPhone;</li>
<li>Allow default values;</li>
<li>Haves callbacks for invalid inputs, valid and overflow;</li>
<li>Haves function to mask strings.</li>
</ul>
<h3>Changelog</h3>
<p>v1.0.2</p>
<ul>
<li>added input callbacks: onInvalid, onValid and onOverflow;</li>
<li>added support for default values;</li>
<li>can now be used like $().setMask({});</li>
<li>added the function $.mask.string(string,mask) that will mask a string (see demos);</li>
<li>now the value of the input is masked at the time the mask is applyed to it.</li>
</ul>
<p>v1.0.1</p>
<ul>
<li>added support for iphone;</li>
<li>removed a bug where a fixed char would be replaced by an inputed one.</li>
</ul>
<p>v1.0</p>
<ul>
<li>initial release.</li>
</ul>
<p>Some bugs have been fixed too. I&#8217;m making a TODO list and i&#8217;m really open to add features you guys want.</p>
<p>You can see the plugin page <a title="meioMask" href="http://meiocodigo.com/meiomask/">here</a>. It contains documentation and examples. Please tell me any bug, new feature, english errors on documentation…. anything! I’ll be glad to hear your feedback and make the fixes. Hope it helps you!</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.meiocodigo.com%2F2008%2F10%2F16%2Fmeiomask-jquery-form-mask-input-plugin%2F';
  addthis_title  = 'meioMask+1.0.2+version+released%21';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.meiocodigo.com/2008/10/16/meiomask-jquery-form-mask-input-plugin/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.meiocodigo.com/2008/10/16/meiomask-jquery-form-mask-input-plugin/</feedburner:origLink></item>
		<item>
		<title>Pretty Truncate String Function in PHP (substr replacement)</title>
		<link>http://feeds.feedburner.com/~r/meiocodigo/~3/420788101/</link>
		<comments>http://www.meiocodigo.com/2008/10/14/pretty-truncate-string-function-in-php-substr-replacement/#comments</comments>
		<pubDate>Tue, 14 Oct 2008 18:33:45 +0000</pubDate>
		<dc:creator>vbmendes</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[encoding]]></category>

		<category><![CDATA[limit]]></category>

		<category><![CDATA[string]]></category>

		<category><![CDATA[truncate]]></category>

		<guid isPermaLink="false">http://www.meiocodigo.com/?p=153</guid>
		<description><![CDATA[In this post, I&#8217;ll show you a function to make truncate a string. I could simply use the substr function, but it&#8217;s not that good since I can break the string in the middle of a word. So I decided to write another function to do this job. It takes 4 parameters: the string you [...]]]></description>
			<content:encoded><![CDATA[<p>In this post, I&#8217;ll show you a function to make truncate a string. I could simply use the substr function, but it&#8217;s not that good since I can break the string in the middle of a word. So I decided to write another function to do this job. It takes 4 parameters: the string you want to truncate, the length you want, the end pattern (defaults to &#8216;&#8230;&#8217;) and the string encoding (defaults to UTF-8). I need the encoding because substr may split in the middle of a character, if it is multi-byte, so I use the <a href="http://br.php.net/manual/en/function.mb-substr.php">mb_substr function</a>.</p>
<h2>Examples</h2>
<pre class="syntax-highlight:php">
echo limit(&#039;this is just a test&#039;,10);// will print &#039;this is...&#039;
echo limit(&#039;this is just a test&#039;,30);// will print &#039;this is just a test&#039;
echo limit(&#039;thisIsJustABigWord&#039;,10);// will print &#039;thisIsJust...&#039;
</pre>
<p>Notice that in the first case, the string will be trucated in the middle of a word(&#8217;just&#8217;), so this word was removed also. In the second case, the string is smaller then the max size I specified, so it&#8217;s returned as it is. I the third case, the string is just a big word, bigger then the max size, so the word is breaked because this is the only solution i have.</p>
<h2>Code</h2>
<pre class="syntax-highlight:php">
function limit($string,$length,$end=&#039;...&#039;,$encoding=null){
	if(!$encoding) $encoding = &#039;UTF-8&#039;;
	$string = trim($string);
	$len = mb_strlen($string,$encoding);
	if($len &lt;= $length) return $string;
	else {
		$return = mb_substr($string,0,$length,$encoding);
		return (preg_match(&#039;/^(.*[^\s])\s+[^\s]*$/&#039;, $return, $matches) ? $matches[1] : $return).$end;
	}
}
</pre>
<p>In the code, I firstly remove the spaces in the start and end of the string, then I get the lenght of my string, if it&#8217;s lower then what I want, return the string itself, otherwise, return the truncated with the end pattern.</p>
<p>Hope it helps.</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.meiocodigo.com%2F2008%2F10%2F14%2Fpretty-truncate-string-function-in-php-substr-replacement%2F';
  addthis_title  = 'Pretty+Truncate+String+Function+in+PHP+%28substr+replacement%29';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.meiocodigo.com/2008/10/14/pretty-truncate-string-function-in-php-substr-replacement/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.meiocodigo.com/2008/10/14/pretty-truncate-string-function-in-php-substr-replacement/</feedburner:origLink></item>
		<item>
		<title>New features at HTML 5</title>
		<link>http://feeds.feedburner.com/~r/meiocodigo/~3/419119261/</link>
		<comments>http://www.meiocodigo.com/2008/10/13/new-features-at-html-5/#comments</comments>
		<pubDate>Mon, 13 Oct 2008 03:23:00 +0000</pubDate>
		<dc:creator>fabiomcosta</dc:creator>
		
		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[5]]></category>

		<category><![CDATA[Ajax]]></category>

		<category><![CDATA[features]]></category>

		<category><![CDATA[HTML]]></category>

		<category><![CDATA[html features]]></category>

		<category><![CDATA[html5]]></category>

		<category><![CDATA[onhashchange]]></category>

		<guid isPermaLink="false">http://www.meiocodigo.com/?p=148</guid>
		<description><![CDATA[I&#8217;ve found this video at youtube and i think thats a really great one. It shows the new features that are on the newest browsers (most of them on theirs nightly builds).
some of them are:

onhashchange event, just on IE8 (thats a start right). With this it will be possible to make a robust and responsive, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve found this video at youtube and i think thats a really great one. It shows the new features that are on the newest browsers (most of them on theirs nightly builds).</p>
<p>some of them are:</p>
<ol>
<li>onhashchange event, just on IE8 (thats a start right). With this it will be possible to make a robust and responsive, ajax application;</li>
<li>input=&#8221;date&#8221;, just opera haves this at the moment (awsome feature!);</li>
<li>input=&#8221;range&#8221;, webkit and opera&#8230;;</li>
<li>no more doctypes! yes, no jokes!;</li>
<li>no more type=&#8221;text/javascript&#8221; for the script tag;</li>
<li>more!!!</li>
</ol>
<div>see the video it&#8217;s a good html and javascript learning.</div>
<p><a href="http://www.youtube.com/watch?v=xIxDJof7xxQ">HTML 5: Features you want desperately but still can\&#8217;t use</a></p>
<p>***Ive&#8217; got some great improvements at the meioMask plugin, im just updating the docs localy and trying to add some more features to release the 1.0.3 version :P.</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.meiocodigo.com%2F2008%2F10%2F13%2Fnew-features-at-html-5%2F';
  addthis_title  = 'New+features+at+HTML+5';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.meiocodigo.com/2008/10/13/new-features-at-html-5/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.meiocodigo.com/2008/10/13/new-features-at-html-5/</feedburner:origLink></item>
		<item>
		<title>MeioUpload Behavior 1.0.1 released!</title>
		<link>http://feeds.feedburner.com/~r/meiocodigo/~3/418940943/</link>
		<comments>http://www.meiocodigo.com/2008/10/12/meioupload-behavior-10-released/#comments</comments>
		<pubDate>Sun, 12 Oct 2008 22:40:46 +0000</pubDate>
		<dc:creator>vbmendes</dc:creator>
		
		<category><![CDATA[CakePHP]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Behavior]]></category>

		<category><![CDATA[upload]]></category>

		<guid isPermaLink="false">http://www.meiocodigo.com/?p=135</guid>
		<description><![CDATA[I am glad to show you the MeioUpload Behavior 1.0.1. An improved Upload Behavior for CakePHP 1.2. It makes file uploads as simple as defining a variable.
See the docs.
Features

Accepts custom directory for files to be uploaded;
Validates the file extension and mime-type due to the behavior configuration;
Validates the max file size;
Allow custom validation rules;
Allow as many [...]]]></description>
			<content:encoded><![CDATA[<p>I am glad to show you the <a href="http://www.meiocodigo.com/projects/meioupload">MeioUpload Behavior 1.0.1</a>. An improved Upload Behavior for CakePHP 1.2. It makes file uploads as simple as defining a variable.</p>
<p><a href="http://www.meiocodigo.com/projects/meioupload">See the docs.</a></p>
<h3>Features</h3>
<ul>
<li>Accepts custom directory for files to be uploaded;</li>
<li>Validates the file extension and mime-type due to the behavior configuration;</li>
<li>Validates the max file size;</li>
<li>Allow custom validation rules;</li>
<li>Allow as many thumbnails formats as you want;</li>
<li>Allow more then one field to be uploadable, with custom options per field;</li>
<li>Stores the directory, filesize, and mime-type in the database if the table has these fields;</li>
<li>Allow the use of default files and deleting files without deleting the entire record;</li>
<li>Delete files when the record is deleted or updated with a new file;</li>
<li>Also works in the $model->saveAll method.</li>
</ul>
<h3>Changelog</h3>
<h4>v1.0.1</h4>
<ul>
<li>Fixed a bug in the create folder method;</li>
<li>Now you can use the $validate var of the model to apply the changes to default validation rules;</li>
<li>Changed the my_array_merge function, now it&#8217;s part of the behavior, name arrayMerge;</li>
<li>Allow use of {DS}, {model} and {field} constants in directory name and fields names;</li>
<li>Fixed a bug with the replacement of the default names.</li>
</ul>
<h4>v1.0</h4>
<ul>
<li>Initial release</li>
</ul>
<p>Any bugs, features suggestions, and english errors, please tell me.</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.meiocodigo.com%2F2008%2F10%2F12%2Fmeioupload-behavior-10-released%2F';
  addthis_title  = 'MeioUpload+Behavior+1.0.1+released%21';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.meiocodigo.com/2008/10/12/meioupload-behavior-10-released/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.meiocodigo.com/2008/10/12/meioupload-behavior-10-released/</feedburner:origLink></item>
		<item>
		<title>Setar flag no lugar de excluir registro do banco com CakePHP</title>
		<link>http://feeds.feedburner.com/~r/meiocodigo/~3/407697567/</link>
		<comments>http://www.meiocodigo.com/2008/09/30/setar-flag-no-lugar-de-excluir-registro-do-banco-com-cakephp/#comments</comments>
		<pubDate>Tue, 30 Sep 2008 22:59:52 +0000</pubDate>
		<dc:creator>vbmendes</dc:creator>
		
		<category><![CDATA[CakePHP]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Behavior]]></category>

		<guid isPermaLink="false">http://www.meiocodigo.com/?p=101</guid>
		<description><![CDATA[Uma boa prática de programação é setar uma flag indicando que um registro foi excluido, e não simplesmente excluir o registro do banco. Por exemplo, se eu tenho um produto, que está relacionado em vários pedidos, e eu simplesmente excluo o produto, todos os pedidos que referenciam ele serão inconsistentes. Se em troca de excluir [...]]]></description>
			<content:encoded><![CDATA[<p>Uma boa prática de programação é setar uma flag indicando que um registro foi excluido, e não simplesmente excluir o registro do banco. Por exemplo, se eu tenho um produto, que está relacionado em vários pedidos, e eu simplesmente excluo o produto, todos os pedidos que referenciam ele serão inconsistentes. Se em troca de excluir o registro do produto, eu simplesmente setar uma flag no registro indicando que ele foi excluído, eu não perderei mais essa consistência. Para excluir o produto de id 5, seria algo como:</p>
<pre class="syntax-highlight:php">mysql_query("UPDATE produtos SET deleted = true WHERE id = 5");</pre>
<p>E para listar os produtos, poderia ser algo como:</p>
<pre class="syntax-highlight:php">mysql_query("SELECT * FROM produtos WHERE NOT deleted;");</pre>
<p>Percebam que para transferir isso para o CakePHP, basta alterar dois métodos, o index, adicionando a condição, e o delete, substituindo o $this->Produto->del() por um save. Mas o <a href="http://www.marianoiglesias.com.ar/">Mariano Iglesias</a> do <a href="http://cake-syrup.sourceforge.net/">Cake Syrup</a> desenvolveu um behavior que nos economiza muito trabalho. É o <a href="http://cake-syrup.sourceforge.net/ingredients/soft-deletable-behavior/">SoftDeletable</a>. Com ele, a tarefa se resume a uma simples configuração no model no qual se deseja aplicar o uso da flag.</p>
<pre class="syntax-highlight:php">var $actsAs = array('SoftDeletable');</pre>
<p>Para isso, basta criar o campo deleted na sua tabela e adicionar o arquivo do behavior, disponível no site citado, na pasta de behaviors do seu projeto.</p>
<p>O único problema que eu encontrei nesse behavior é que ao chamar a função $this->Model->del($id), o beforeDelete do behavior é acionado, atualizando o campo da flag deleted para true e retornando false, o que impede que o registro seja realmente excluído do banco. Com isso a tentativa de salvar retorna falso, indicando erroneamente que não obteve sucesso.</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.meiocodigo.com%2F2008%2F09%2F30%2Fsetar-flag-no-lugar-de-excluir-registro-do-banco-com-cakephp%2F';
  addthis_title  = 'Setar+flag+no+lugar+de+excluir+registro+do+banco+com+CakePHP';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.meiocodigo.com/2008/09/30/setar-flag-no-lugar-de-excluir-registro-do-banco-com-cakephp/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.meiocodigo.com/2008/09/30/setar-flag-no-lugar-de-excluir-registro-do-banco-com-cakephp/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic Page Served (once) in 0.676 seconds --><!-- Cached page generated by WP-Super-Cache on 2009-01-04 19:47:47 -->
