A better way to use jQuery in Wordpress Themes

.........................................................

Since a lot of people come to my blog looking for a way to add jQuery into their wordpress themes, it turns out that there is a better way to do it than the way that I previously blogged. You can include jQuery from the current Wordpress install by calling:

wp_enqueue_script("jquery");

Followed by:

wp_head();

You can even use it to include the jQuery library that you are adding with your theme (so that it works after wordpress updates their library):

//First you have to get rid of the one that wordpress includes:
wp_deregister_script('jquery');
//Then you have to replace it with your own:
wp_register_script('jquery', (bloginfo("template_url")."/js/jquery.min-1.3.2.js"), false, '1.3.2');
//or you could use the one that google so kindly provides:
wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"), false, '1.3.2'); 
//Then enqueue it
wp_enqueue_script('jquery');
Share and Enjoy:
  • Twitter
  • DZone
  • del.icio.us
  • Google Bookmarks
  • RSS
  • StumbleUpon
  • Digg
  • Yahoo! Buzz
  • Facebook
  • Print this article!
  • Turn this article into a PDF!
  • E-mail this story to a friend!

Continue Reading

On naming functions sanely

.........................................................

When naming your functions, there are certain function prefix/postfixes that should only return specific types. They help the programmers who follow in your footsteps to grasp what it is you were trying to do. It also reduces the number of comments you need because the naming conventions are self explanatory. Here is a short list of some of those function modifiers and the expected return values.

The following should only return boolean values, they should not set anything in the class. They are idempotent. If they are not, you have probably done something wrong or your function is misnamed. Rename it quick before anyone else mistakes it for something that it is not and causes a long bug search.

is_something();
something_exists();

The following should probably not return a value (but if they do it should be true or false based on their success).

set_something();
unset_something();
import_something();
read_something();
calculate_something();
something_calculation();
//this one obviously shouldn't return a value... it could throw an exception though
$something->var = $something_else;

The following should only return values not set them or change anything prior to returning them.

get_something();
retrieve_something();
$something_else = $something->var;
something_value();

The following should only return an integer (long, double…).

count_something();
something_count();

The following are similar, they should also return a number of some sort (could be a float, an int, a double).

total_something();
something_total();
sum_something();

Another thing, don’t use this naming scheme:

get_something();
get_somethings();

There is not enough difference when you are glancing at them quickly or trying to debug something, or you are scrolling through them in an auto-complete pop-up, or documentation. Rather use this one, it is easier to differentiate.

get_something();
get_all_somethings();

Remember:

Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live.

- C2.com via Coding Horror

Share and Enjoy:
  • Twitter
  • DZone
  • del.icio.us
  • Google Bookmarks
  • RSS
  • StumbleUpon
  • Digg
  • Yahoo! Buzz
  • Facebook
  • Print this article!
  • Turn this article into a PDF!
  • E-mail this story to a friend!

Continue Reading

Smarty Best Practices 1

.........................................................

I know I have slagged a lot on smarty in the past, both on my blog and on twitter… It is not because I do not understand it, it is because I see it being misused (at least as far as I am concerned, I may be wrong about it) every day in old projects and all over the internet. So, to make up for the slagging that it has received from me, I will now attempt to show you some best practices (or How to use Smarty the smart way). So, without further ado…

Rule 1: Don’t Manipulate the data

First of all, before you even start using smarty, you have to realise that this is a templating language. You are not supposed to process the data, create new data, delete data, get new data (and so on)… When you are ready to display your template you should already have the data retrieved/created, and ready to go. If you are considering doing this in your smarty template, stop and take a look at your backend code, you probably did something wrong back there. I guess I should do these as some kind of list, so…

Sure it is tempting to do:

{assign var='newVar' value=$oldVar.varThatINeed}

But don’t. It means that people have to go looking for not only $newVar in the php code that displayed the template, they also have to go looking through the smarty code for the assignment. If you need to access that variable, do this instead:

{$oldVar.varThatINeed}

If that variable is supposed to get something from an array, do this:

{$arrayVar[$oldVar.varThatINeed]}

Sure it takes a second more to write it out each time, but it is easier to find in the smarty code.

Breaking Rule 1

There are times when you might need to break rule one. If you find yourself doing anything more complicated than:

{math equation="x + y" x=$height y=$width}

in your template, you should take a step back and look at your code again, why are you doing it this way? Should you be doing it this way? Isn’t there a better way?
I think that even this example provided by the smarty documentation is probably a bit much for the template, especially if the equation is likely to change:

{* Calculate something *}
{math equation="(( x + y ) / z )" x=$vars.var1 y=$vars.var2 z=$vars.var3}

It should probably be something more like:

function calculateSomething($vars){
...
}
//notice I don't rename the function so that you can find it easily later on
//if you didn't want your function named this way, name it something else in your php code...
$smarty->register_modifier('calculateSomething', 'calculateSomething');
$smarty->display('my_template.tpl');
{vars|@caclulateSomething}

Notice the @ symbol, this means pass the whole array as one variable. This is important because smarty apply the modifier to each of the variables in the array if you do not (unless that is what you wanted to happen). While in some cases you may need to break the first rule, if you do it this way, it is managable, reusable. An even better way would be to do this before it gets to the template, but it is understandable to no want to go through the data twice, so this seems like a fair trade off as you are still doing the manipulation in php, and displaying the results of the manipulation in smarty.

Share and Enjoy:
  • Twitter
  • DZone
  • del.icio.us
  • Google Bookmarks
  • RSS
  • StumbleUpon
  • Digg
  • Yahoo! Buzz
  • Facebook
  • Print this article!
  • Turn this article into a PDF!
  • E-mail this story to a friend!

Continue Reading

Smarty… again

.........................................................

Smarty again.To those who think that smarty prevents you in any way from writting your business logic into a template… watch this…

 
{php}
 
if (!$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')) {
    echo 'Could not connect to mysql';
    exit;
}
 
if (!mysql_select_db('mysql_dbname', $link)) {
    echo 'Could not select database';
    exit;
}
 
$sql    = '"INSERT INTO unsafe_tabel (unsafe_var) VALUES('".$_GET['unsafe']."')";
 
$result = mysql_query($sql, $link);
 
if (!$result) {
    echo "DB Error, could not query the database\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}
 
{/php}

I think I might have just put stuff that actually belongs in the model into the view… and unescaped too… oh dear… (that would be template for you smarty folks). So, no, smarty does not disuade you from doing it, infact it seems that they encourage it a bit by providing you with the option to do so via the {php} {/php} tags, instead of forcing you to use {rdelim} {ldelim} all throughout your code instead (which would surely discourage most people). If I can do that, the people who’s site’s you’ve written can surely do that too… and mess up the nice templates that you have made for them (and their databases too). So, please think of another reason to use Smarty, and if you cannot, then why are you using it?

Share and Enjoy:
  • Twitter
  • DZone
  • del.icio.us
  • Google Bookmarks
  • RSS
  • StumbleUpon
  • Digg
  • Yahoo! Buzz
  • Facebook
  • Print this article!
  • Turn this article into a PDF!
  • E-mail this story to a friend!

Continue Reading

Why I use NetBeans

.........................................................

I am sure you know by now that IBM was set to aquire Sun Microsystems this week. This would have been bad seeing as most of their products directly compete/overlap with eachother. Fortunately they backed off at the last minute (or at least they did not offer enough money for them). While this might be a barganing tactic I surely hope that this is not the case. If they had bought them, it seems to me that it would have meant the death of many of the great Sun products.

NetBeans is one that would have been dead really quickly, sure it would have survived as a fork of the code (since Open Source projects never really die) but it would have been making much slower progress than it does now. This is because of Eclipse which is a good product in its own right… but as someone put it:

NetBeans is the Mac of the IDE world… it just works

If you want to use php in eclipse you have to work at setting it up, if you want to develop in php, download the php package, ruby? There is one  for that too. Since they are all professionally developed as part of the package there is no need to work to get it going no need to route aroud the net trying to find which plugins you need, which ones are still supported (which ones are supported well), which ones do not conflict with eachother… You want to use SVN? Which one do you use? Subclipse? Subversive? Does it work with your version of eclipse? Your repository? Will it corrupt it? Does it work with the latest version of subversion? Can it use the command line version if it doesn’t? (On a side note, subclipse probably works fine since it is supported by tigris).

So, if you have a choice at work, try it, if not, try it at home. It even has an eclipse project importer. You have nothing to lose, it is free after all.

Share and Enjoy:
  • Twitter
  • DZone
  • del.icio.us
  • Google Bookmarks
  • RSS
  • StumbleUpon
  • Digg
  • Yahoo! Buzz
  • Facebook
  • Print this article!
  • Turn this article into a PDF!
  • E-mail this story to a friend!

Continue Reading

The Blogroll

Search this Site


[]