Tobiasz Cudnik

Archive for November, 2008|Monthly archive page

PHP 5.3 introduces closures and inline functions

In The Net on 25.11.2008 at 0:31

I couldn’t believe it in first place, but there are ongoing works to bring closures power to PHP in upcoming 5.3 version! No more create_function() and backslashing backslashed quotes.

There are also some scope inheritance tries (using “use” keyword). Below you have some teasing snippet, after which head to wiki.php.net/rfc/closures for more…

function replace_in_array ($search, $replacement, $array) {
  $map = function ($text) use ($search, $replacement) {
    if (strpos ($text, $search) > 50) {
      return str_replace ($search, $replacement, $text);
    } else {
      return $text;
    }
  };
  return array_map ($map, $array);
}

Pure HTML templates theory

In Ideas on 19.11.2008 at 23:51

In this post i would like to sum up my thought about using pure HTML files as fully functional templates. I’ve examined this pattern for about a year and i have to say it creates real order in application structure and lifecycle.

What means “pure” ?

  • Pure like plain, nothing else than HTML
  • Fully validable
  • Readable in any browser
  • Editable in any HTML editor

Read the rest of this entry »

Keep things from web up-to-date easily

In Web Scraping on 05.11.2008 at 17:11

When some project doesn’t use SVN or any other version-control system (or you can’t use it) you have to download things manually. I don’t have to say that nobody wants to do this, so what can you do to not do it ? You can simulate yourself doing it…

Example below downloads latest release of madwifi branch with new HAL (which i need for my WiFi adapter).

#!/usr/bin/php
find('table tr')->slice(-2, -1)->downloadTo('/target/local/path');
}
?>

Now, to get latest release all i need is to run above script from command line. One missing thing is checking if anything has changed but i leave it to you to resolve ;)

For files which names doesn’t change you can just use wget, like so:

wget 'http://host.net/somefile.zip' -O new-name.zip

jQuery Server Side ports

In The Net on 01.11.2008 at 11:03

jQuery besides achieving such successes as being used by Google or Micro$soft also has ports to other major languages. Most of them are designed to be server-side what opens doors for new uses to the library.

jQuery ports to other languages:

Below some snippets showing each implementation in few lines.

PHP

foreach($doc['ul > li'] as $li) {
  pq($li)->addClass('my-new-class');
      ->filter(':last')
        ->addClass('last-li');
}
$doc['ul > li:last']
  ->addClass('last-li');

Ruby

# load the RedHanded home page
doc = Hpricot(open("http://redhanded.hobix.com/index.html"))
# change the CSS class on links
(doc/"span.entryPermalink").set("class", "newLinks")

Perl

pQuery("http://google.com/search?q=pquery")
  ->find("h2")
    ->each(sub {
      my $i = shift;
        print $i + 1, ") ", pQuery($_)->text, "n";
      });

ActionScript

// add enterFrame event handler
$(stage).enterFrame(function(event:Event):void {
  $("RoundRect").attr("color", function(...args):uint {
    return Math.random() * 0xffffff;
  });
});

List comes from phpQuery wiki page. Do you know any other ports ? Share it in comments.

Update 09.12.08
New jQuery port has been released. This time it’s powered by Python and named PyQuery. Here’s the code:

Python

p = d("#hello")
p.addClass("toto")
p.attr.id = "plop"
p.prependTo(d('#test'))

Follow

Get every new post delivered to your Inbox.