How To Create Your Own Contao Module

Contao is a wonderful tool for creating professional websites. We already know that. But there’s one major flaw which can easily destroy all the fun you had with Contao so far:
That really awful documentation for developers.

If you want to develop a custom module, you’re pretty much left alone. The official ressources (http://www.contao.org/en/developers-guide.html) don’t show the complete creation process, which is highly needed in my opinion.

To help you out a little bit, I created a very simple slideshow module. Its really nothing special, but should show you whats happening behind the scenes.

Feel free to do whatever you want with this piece of code, and if you have any questions, don’t hesitate to drop a comment.

Download

Small Mootools Twitter Class

For a current project, we had to implement a custom Twitter widget. After some time searching the web, I found this article on David Walsh’s Blog. Even if its a bit outdated, it did the trick.

Based on the old class, I present the updated and extendend version!

var TwitterWidget = new Class({

	Implements: [Options, Events],

	options: {
		count: 3,
		insertElement: null,
		actionSeparator: '<span class=\"tweet_actions_separator\">.</span>',
		dateFormat: '%e %b'
	},

	initialize: function(username, options) {
		this.setOptions(options);
		this.username = username;
	},

	getTweets: function() {
		new Request.JSONP({
			url: 'http://api.twitter.com/1/statuses/user_timeline.json',
			data: {
				screen_name: this.username,
				count: this.options.count
			},
			onComplete: function(data) {
				data.each(function(tweet) {
					tweet.text = this.linkify(tweet.text);
				}, this);
				this.fireEvent('fetchComplete', [data]);
				this.insertTweets(data, this.options.insertElement);
			}.bind(this)
		}).send();
		return this;
	},

	linkify: function(tweet) {
		//totally stolen from Jeremy Parrish / David Walsh
		return tweet.replace(/(https?:\/\/\S+)/gi,'<a href="$1">$1</a>')
					.replace(/(^|\s)@(\w+)/g,'$1<a href="https://twitter.com/$2">@$2</a>')
					.replace(/(^|\s)#(\w+)/g,'$1<a href="https://search.twitter.com/search?q=%23$2">#$2</a>');
	},

	insertTweets: function(tweets, element) {
		if (element == null) {return;}
		tweets.each(function(tweet) {
			var tweetDate = Date.parse(tweet.created_at)
			var tweetContainer = new Element('div', {
				html: '<span>' + tweet.text + '</span>',
				'class': 'tweet'
			});
			new Element('div', {
				html: '<a href=\"https://twitter.com/' + this.username + '/status/' + tweet.id_str + '\">' +
					 tweetDate.format(this.options.dateFormat) + '</a>' +
					 this.options.actionSeparator +
					 '<a href=\"https://twitter.com/intent/tweet?in_reply_to=' + tweet.id_str + '\">reply</a>' +
					 this.options.actionSeparator +
					 '<a href=\"https://twitter.com/intent/retweet?tweet_id=' + tweet.id_str + '\">retweet</a>' +
					 this.options.actionSeparator +
					 '<a href=\"https://twitter.com/intent/favorite?tweet_id=' + tweet.id_str + '\">favorite</a>',
				'class': 'tweet_actions'
			}).inject(tweetContainer);
			tweetContainer.inject(element);
		}, this);
		this.fireEvent('insertComplete');
	}

});

Usage

Let’s say you have the following HTML snippet,

<div id="twitter_container">
</div>

and you want your recent 5 tweets in this container, then simply use the following code:

var widget = new TwitterWidget('yourveryownusername', {
    insertElement: $('twitter_container'),
    count: 5
}).getTweets();

But what if you don’t like the date format and want an other separator between reply, retweet and so on? No problem.

var widget = new TwitterWidget('yourveryownusername', {
    insertElement: $('twitter_container'),
    count: 5,
    dateFormat: '%B-%d-%Y',
    actionSeparator: '|'
}).getTweets();

(for the date format options head over to http://mootools.net/docs/more/Types/Date#Date:format)

That’s it! Simple.
But be aware that you need to add the Request.JSONP and the Date module if you are using Contao. These two things are not included in the Mootools-More pack by default.

On this page, you can see how it could look like on your website.

Have fun, and oh, please let me know if this could come in quite handy on any project you’re working on.

New Ebersbach Mediendesign website released!

This is the point where I could write an essay about how cool the new website is, but now that this is a blog about development, we should stick to the boring and nerdy facts.

Overview

If I’d have to describe the page in a few words, I would likely say “sprite & javascript madness”.

Javascript

In the first few drafts, the only scripted element was the slideshow in the header. But while being in the development phase, we came up with some neat (and mostly tricky) ideas to improve the user experience using javascript.
Lets take the logo as an example. In the final layout draft it was just a white logo, nothing more, nothing less. As I accidentally mixed up the logo and the header, a new idea was born. It solved the issue about a missing mouseover and looks pretty awesome (though, even if the repeating image is nowhere near perfect, its close enough).

CSS-Sprites

The sprite stuff is something that nearly drove me insane. At first, every background was a separate image. Ok, thats basically nothing to worry about, but after seeing the performance graph with all these separated requests being made, I realized that there is no way around sprites.
There are some pre-made solutions for that, Sprite Me for example, but either I’m too dumb the get some decent results with them, or they did not fit the situation. So I was forced to combine them myself. As someone with zero experience in image editing software, I felt like a 3-year-old in an art academy.
But after some minutes it wasn’t so bad after all. For example layers. First being a feature straight from hell, they appeared to be pretty helpful in structuring the image.
And so, after hours of testing, resizing, repositioning and exploring the world of graphic designers, the huge sprite was created. It contains 16 single images and so reduces the amount of requests by 15, pretty neat if you ask me.

And where’s the code?

Yep, there is no code yet. If you are interested in how something works on the new page, just launch Firebug and see for yourself. It will be partially explained in the next blogposts though.

Now for some statistics! Everybody loves statistics.

159 lines of javascript
800 lines of css
7 images in total
3 custom templates
3 custom contao modules with 39 files in total

All sizes in uncompressed and unoptimized format.