This Week in Web – jQuery Forms, JS Microframeworks, Resig & Dahl Q+A

May 6, 2011 No comments yet

jQuery IdealForms

jQIdealForms is a forms framework for jQuery that assists developers in building attractive forms with minimal Javascript code. Forms are defined in standard HTML so if the user has Javascript disabled, the form still displays. After calling the .idealForms() method on your form, jQIdealForms automatically styles and enhances all inputs. All styling is done with CSS, no images are used. jQIdealForms supports IE7+ and other modern browsers.

MicroJS.com

Microjs.com helps you discover the most compact-but-powerful microframeworks, and makes it easy for you to pick one that’ll work for you.

Micro-frameworks are definitely the pocketknives of the JavaScript library world: short, sweet, to the point. And at 5k and under, micro-frameworks are very very portable. A micro-framework does one thing and one thing only — and does it well. No cruft, no featuritis, no feature creep, no excess anywhere.

jQuery 1.6 Released

The jQuery team have released version 1.6 of the jQuery Javascript library. This release includes an increase in performance for the .attr(), .val() and .data() methods, changes to the way jQuery manages HTML element attribute data, numerous big fixes and the addition of relative movements in the .css() method. Links to the new…

PHP Twig Template Wrapper

May 2, 2011 No comments yet

Requirements

Template Class

The use of the Settings class is self explanatory. Just replaces the instances of Settings with your own configuration system (or just strings). All Twig settings are documented here.

class Template
{

	public $context = array();

	private $template;

	public function __construct($template)
	{		

		$loader = new Twig_Loader_Filesystem(Settings::get('template_directories'));

		$twig = new Twig_Environment($loader, array(

			'cache' => Settings::get('template_cache_directory'),
			'debug' => Settings::get('debug'),

				)

		);

		$this->template = $twig->loadTemplate($template);

	}

	public function setContext(array $context = array())
	{
		$this->context = array_merge($context, $this->context);

	}

	public function render()
	{
		return $this->template->render($this->context);
	}	

	public function __toString()
	{
		return $this->render();
	}

	public function __set($key, $val)
	{
		$this->context[$key] = $val;
	}

	public function __get($key)
	{
		return $this->context[$key];
	}

}

Simple Example

hello.html

Hey there <strong>{{ name }}</strong> welcome to the website.

Code in controller

$t = new Template('hello.html');
$t->name = 'Frank';
echo $t->render();

Output:

Hey there <strong>Frank</strong> welcome to the website.

Example with Inheritance

base.html

<h3>Welcome to the website<h3>
{% block content %}{% endblock %}

hello.html

{% extends 'base.html' %}
{% block content %}
Hey there <strong>{{ name }}</strong> welcome to the website.
{% endblock %}

Code in controller

$t = new Template('hello.html');
$t->name = 'Joe';
echo $t->render();

Output:

This Week in Web – Git Cheatsheet, PHP Profiling, Netbeans, Dojo Layouts

April 22, 2011 No comments yet

Git Cheatsheet

Basic Profiling Techniques for PHP Applications

Eric Hogue has written an article outlining the different profiling techniques a developer can use to test their PHP applications. He starts off by introducing Apache Siege which gives response time and request/sec figures. He then goes on to talk about XDebug, which is used to find bottlenecks in a development environment, and XHProf, which is appropriate to use in a production environment. He explains how to install and configure all 3, as well as view output from XDebug and XHProf in KCacheGrind and XHGui. If you haven’t done any application profiling in PHP before I recommend you read this article as it is a good introduction.

Netbeans 7.0 Released

The Netbeans Project has released version 7.0 of their IDE. Some of the big new features 7.0 introduces are:

  • HTML5 support
  • JDK7 support
  • PHP application refactoring
  • Java GUI designer, GridBag
  • Git 1.7 support

A detailed list of the changes since Netbeans 6 are available here.

Dojo Application Layouts with Dijit

Sam Foster of the Dojo Project has written a tutorial showing how to layout Dojo application using dijit.layout. Layouts are vital in any Dojo…

Tutorial Django Dojo DataGrid

April 18, 2011 No comments yet

Dojo is an open source Javascript toolkit. It provides an easy way to access and modify the DOM as well as a rich user interface widget library. One of the most powerful widgets in the Dojo toolkit is DataGrid. It provides an easy way to represent what is essentially a cross between a spreadsheet and a table. In a recent Django project I needed to output the contents of a model into the DataGrid. Thanks to django-dojoserializer this is very easy.

Setup

We need the Python package django-dojoserializer. This can be installed by downloading the source and running

python setup.py install

or with setuptools using

easy_install django-dojoserializer

or with pip using

pip install django-dojoserializer

Then add django-dojoserializer to the INSTALLED_APPS tuple in settings.py.

Dojo will need to be linked correctly in the Django template. We need to require the dojox.grid.DataGrid and dojo.data.ItemFileReadStore classes.

<script src="/static/libs/dojo/dojo.js" djConfig="parseOnLoad: true">%lt;/script>
<script type="text/javascript">
dojo.require('dojox.grid.DataGrid');
dojo.require('dojo.data.ItemFileReadStore');
</script>

Django Model

PROXY_TYPE_CHOICES = (

	('SOCKS4', 'SOCKS4'),
	('SOCKS5', 'SOCKS5'),
	('HTTP', 'HTTP'),

)

class Proxy(models.Model):
	ip = models.CharField(max_length=50)
	type = models.CharField(max_length=6, choices=PROXY_TYPE_CHOICES)
	alive = models.BooleanField(default=False)
	added = models.DateTimeField(auto_now=False, auto_now_add=True)
	last_checked = models.DateTimeField(auto_now=True, auto_now_add=False)

	def __unicode__(self):
		return self.ip

Django View

The view needs…

This Week In Web – LiveJS, SunlightJS, Python, MySQL INSERT

April 15, 2011 No comments yet

LiveJS

LiveJS is a Javascript library that is designed to speed up development times. It actively monitors the CSS and Javascript files that are in the used in the current page and automatically reloads them whenever a change is made. This saves you from having to refresh the web browser every time you make a change to a stylesheet or script. Check out the demo.

SunlightJS

SunlightJS is an intelligent syntax highlighter for web pages. It supports Internet Explorer 6+ in addition to all Gecko and Webkit powered web browsers. It supports most popular languages out of the box (PHP, Python, Java, Javascript etc..) and has a simple API interface that allows you to create your language bindings. Full documentation and tutorial on how to use SunlightJS is available here.

Advanced Programming Language

Matt Might, Assistant Professor of CS at University of Utah, has written a blog post titled Advanced Programming Languages. Might opens with:

If you’re looking for a job in industry, my reply is to learn whatever is hot right now: C++, Java and C#–and probably Python, Ruby, PHP and Perl too.

If, on the other hand, you’re interested in enlightenment, academic research

Why You Should Be Using a PHP Framework

April 11, 2011 7 comments

All frameworks discussed in this post are full stack web frameworks coded in PHP such as Code Igniter, Symfony, Kohana, Yii and CakePHP. Some (but definitely not all) of the points below may apply to micro frameworks such as FatFreeFramework, Limonade, Glue and Slim.

When should I use a framework?

Frameworks should be used when constructing web applications. Any application that involves a database, forms, sessions, cookies or a remote service (such as Twitter or Facebook) will benefit from being powered by a framework. There is no need to use a framework for a website that has only one or two pages, nor for command line utility scripts.

What features do frameworks provide?

  • Database abstraction. Frameworks can abstract away SQL-vendor specific features. This allows you to change your SQL database without needing to rewrite any code. For example, switch from MySQL to Postgres or MSSQL. Some frameworks can also handle database relations automatically, which saves you having to write JOIN queries.
  • Cache abstraction. Rather than using backend specific caching functions (such as apc_add and apc_fetch), you use a generic caching class which has support for multiple backends such as Memcache, APC and XCache.
  • Form management. Symfony2 and CakePHP allow

This Week in Web – App Engine, Titanium Studio, Useful PHP Snippets

April 8, 2011 No comments yet

App Engine 1.4.3 Released

Python API Changes
Prospective Search API: The experimental Prospective Search API allows Python runtime users to detect and take action on datastore entities that match certain criteria when they are written. For the experimental release, users will be allowed 10,000 subscriptions with the Prospective Search API. Pricing will be announced once the feature is fully launched.

Testbed Unit Test Framework: The Testbed suite for Python provides an easy interface for using App Engine API stubs in integration tests similar to the previously existing Java Testing Framework. You can create tests for your application that do not rely on calling App Engine production services, which speeds up the time your tests take to complete, and eliminates dependencies for your test on external services.

Files API: The new Files API in Python and Java allow you to programatically read and write data using Blobstore. This API can be used to generate reports, export data, or do anything that your heart desires that requires large, binary objects.

full announcement

Titanium Studio Preview

Appcelerator have announced a preview release of Titanium Studio, an IDE built on top of Aptana (Eclipse) for Titanium Mobile development. It includes…

Using Celery with Django

April 4, 2011 No comments yet

Introduction

In a recent Django project I needed to issue some HTTP requests to a website, save the results and then display the information to the user. I could have issued those HTTP requests in a Django view, however the requests could take up to 10 seconds to return, and I didn’t want the page hanging that long for the user. I ended up using Celery, an asynchronous task queue written in Python, to execute the requests.

Installation

Celery
Celery is in pypi so installation is simple.

easy_install celery
pip install celery

Or download the source code, extract it and

python setup.py install

Django + Celery
In a high traffic production environment Celery should be paired with the RabbitMQ backend. However for my low traffic application I went with the django-kombu backend. django-kombu uses the Django database as a message store and requires very little configuration compared to other backends. We also need the djcelery package.

easy_install django-kombu
easy_install django-celery

Configuration

Before we can start using Celery we need configure our Django project. In settings.py make sure you add djkombu and djcelery to the INSTALLED_APPS tuple. Also make sure your database is configured and working.

Also add…

This Week in Web – MongoDB, jQuery, Unladen Swallow

April 1, 2011 No comments yet

The Little MongoDB Book

Karl Seguin, creator of mongly, has released a free and open MongoDB ebook. The Little MongoDB Book is aimed at those new to MongoDB and covers the MongoDB API for reads, writes and updates as well as tips on getting the best performance and durability out of a MongoDB server. The book can be viewed on github or downloaded as a pdf.

Beginning jQuery With A Solid Foundation

Mike Gleason has written a brilliant article titled Beginning jQuery with a solid foundation. The article starts out what the Document Object Model is and how the HTTP protocol works. It then goes into detail explaining how jQuery itself is structured, and how the seemingly magical $() function is created (and how you can do it yourself in Javascript). If you learned jQuery without learning Javascript then I recommend you read this.

PHPThumb

PHP Thumb is a light-weight image manipulation library aimed at thumbnail generation. It features the ability to resize by width, height, and percentage, create custom crops, or square crops from the center, and rotate the image. You can also easily add custom functionality to the library through plugins. It also

NodeJS cURL Tutorial

March 28, 2011 No comments yet

NodeJS is a powerful evented Javascript platform running on top of Google’s V8 javascript engine. Over the last year the popularity of NodeJS has increased greatly, as shown by the number of discussions on programming forums such as Hacker News, Reddit and Stackoverflow. Despite this NodeJS is still young and it falls short to Python and Ruby when it comes to the number and range of libraries and modules available.

The Problem

I wanted to convert one of my web scraping applications I made in PHP to NodeJS. The application interacts with SOCKS4 and SOCKS5 proxies using PHP’s cURL bindings. Unfortunately there are no cURL bindings (or SOCKS protocol wrappers) written for NodeJS. NodeJS does come with an HTTP client module that can issue web requests, however it doesn’t support SOCKS proxies.

The Solution

Although there are no specific NodeJS bindings for cURL, we can still issue cURL requests via the command line interface. NodeJS comes with the child_process module which easily allows us to start processes and read their output. Doing so is fairly straight forward. We just need to import the exec method from the child_process module and call it. The first parameter is the command…