Adding Layout support like RubyOnRails into CodeIgniter :: By Hooking

Recently I came across an interesting feature in RoR which is known as layout yielding. To understand what it is just imagine that you have a particular template file (layout) specific to your controller, and in that layout file you have a special block called yield. After that whenever there is some output from any method in your controller will be yielded exactly to that block in that layout. Which means all output from your controller will be first replaced into that “yield” block inside that layout, and then whole data will be displayed right away.

So what is the benefit of using that. Simple, it will help to design your website more flexibly. You have a layout, and inside that layout at a particular position you can output all data. This will just minimize the need of splitting a single template into smaller pieces like “header”, “body”, “footer” and so forth, and finally turning them into a single piece manually before rendering the final output.

Let’s have a look at the following image to understand the fact more clearly.

layout.gif

This is a typical website payout where some of its sections are marked. In this layout which sections you think changes most frequently? Definitely the section marked as p3. Now what usually happens when you design a site.

1. Either you write the whole file as template and change the parts you need
2. Or you split them into smaller components and merge those parts while delivering the final output.

Now, with this yield feature all we do is we create a single html file exactly as the design keeping the div of P3 blank. But inside that P3 container div, we will just write some text “{yield}”. Now because of this yield feature, whenever our controller generate some output using any view, CodeIgniter will automatically load that layout file first, then take the output from the view and merge them together.

To enable this excellent yield feature into CodeIgniter we need to write some hook. First, open the file named application/config/hooks.php and add the following line in it.

$hook['display_override'][] = array(
'class' => 'Yielder',
'function' => 'yield',
'filename' => 'Yielder.php',
'filepath' => 'hooks'
);

After that, inside application/hooks folder create a file named “Yielder.php” and inside that file, write the following code.

<? if (!defined('BASEPATH')) exit('No direct script access allowed');
class Yielder
{
function yield()
{
$ci= & get_instance();
$current_output = $ci->output->get_output();
$controller = $ci->uri->segment(1);
$layout = "system/application/layouts/{$controller}.php";
if (file_exists($layout)){
$output = $ci->load->file($layout,true);
$output = str_replace("{yield}",$current_output,$output);
}
else
$output = $current_output;
echo $output;
}
}
?>

And that’s it!! You are done.

Now all you have to do is create a folder inside your application folder named “layouts” and place the layout file named same as controller. For example if your controller name is “abcd” then your layout should be “abcd.php” – But remember to keep a section named {yield} inside that layout. Lets take a look at the complete example below

Controller: controllers/abcd.php

<?
class abcd extends Controller
{
function help()
{
$title = "";
for($i=0; $i";
}
$this->load->view("myviews",array("title"=>$title));
}

}
?>

View : views/myview.php

<?
echo $title;
?>

Layout : layouts/abcd.php

<h1>
Hi there
</h1>

{yield}

<h1>
Happy Ending
</h1>

And the final Output is

/****************************

Hi there

0
1
2
3
4
5
6
7
8
9

Happy Ending

****************************/

back after a long time

after almost a month, I started blogging again. these days I was extremely busy with developing a localized blog and some internal deliveries with pageflakes. yesterday i found one cool service which tells you what happened any day in history. i developed a flakes yesterday by clipping those information.

day by day I am falling into love with code igniter.

yesterday i started learning ruby on rails again.

আমার আমি

বিআইটিতে পড়ার সময় আর স্কুলের সময়টা আমার সবচেয়ে ভাল কেটেছিল। স্কুলে পড়ার সময় ছিলাম পুরোপুরি ভোদাই একটা। বাসা টু স্কুল আর স্কুল টু বাসা ছাড়া আর কোন কাজ ছিল না। আমার সবচেয়ে বড় বন্ধু ছিল আমাদের বাসার কাজের ছেলে ফরহাদ। ফরহাদ আমাদের বাসায় এসেছিল যখন আমি ক্লাস টুতে পড়তাম, আর চলে গিয়েছিল ইন্টার পাশ করার পরে, সুতরাং সব কিছুর মাঝে ও ছিল আমার খেলাধুলার সঙ্গী।

বিআইটিতে পড়ার সময় সুমির সাথে পার করেছি সারাদিনের বেশীর ভাগ সময়। কি ছিল সেই সব দিন, আহ। ভোরেঊঠে ক্লাসে দৌড়, অ্যাসাইনমেন্ট, পরীক্ষা আর ফাঁক পেলেই সুমির সাথে বেড়িয়ে পড়া। দুজন মিলে প্রতিদিন বিকেলে বসে থাকতাম পদ্মার পাড়ে, জায়গাটা মনে হত অনেক নিজের, অনেক কাছের। প্রতিদিন বিকেলে বিসমিল্লাহ হোটেলের মোগলাই আর চা দিয়ে হত নাস্তা। হলে ফেরার সময় আবার দুজনের মন খারাপ, কতক্ষন আবার দেখা হবে না। মাঝে মাঝে যেতাম রাজশাহী ইউনিভার্সিটির পাবলিক লাইব্রেরির সামনে মানিক মামার দোকানে কলিজার সিংগাড়া খাওয়ার জন্য। কখনো কাজলায় চায়ের টংএ চলে যেত ঘন্টার পর ঘন্টা।

আজকে দুজনে মিলে গিয়েছিলাম বুমারস এ, দুপুরের খাবার খেতে। অনেক দিন পরে একটু সময় করতে পারলাম। সবকিছু সেইসব দিনের মত, শুধু পার্থক্য একটাই, আজ আমাদের মাঝখানে আফিফ নামে একজন দেবদূত থাকে সবসময়।

রিকশায় করে ফেরার সময় সুমি আমাকে নামিয়ে দিল পেজফ্লেক্স অফিসের সামনে। নামিয়ে দিয়ে চলে যাওয়ার সময় হঠাৎ করে মনে পড়ল ও আমাকে এভাবে রাজশাহীতে হলের সামনে নামিয়ে দিয়ে চলে যেত ওর হলে… নিজের অজান্তেই নস্টালজিক হলাম

খারাপ হয়না মাঝে মাঝে কাজ ফেলে সেই আগেকার দিনগুলোর মত বেড়িয়ে পড়তে পারলে।

Javascript For Living?? Oh hell yeah….

I just came across this article in Ajaxaian where Dion Almaer rant what you need to do when you code JS for living!!! Some says that pure JS is nothing, some says that just JS cant take you anywhere. I agree partially with Dion’s thought and wants to add something by myself.

Well, you can do pure JS for living as I am doing right now. Beside lots other tasks in pageflakes, I write huge amount of JS everyweek to develop flakes as a development engineer. Beside that I also maintain the community, study JS in the user submitted flakes and support them to write efficient JS. If you neglect JS as a language, you are doing a big mistake. JS is extremely powerful and you can do things beyond your imagination with it.

I want to add some tips beside dion’s suggestions. You also need to do the following if you really want to live by coding JS.

1. DOM Scripting is a Must, but not all, simple Node operations will do.
2. Interacting with CSS is also a must.
3. Manipulating JSON is more important than XML becoz its much more flexible and lightweight to transfer your data as JSON object.
4. Choose some popular libraries and stick to them. Personally I suggest Prototype (Its a must), jQuery, mooTools and Scriptaculous. I dont like Dojo becoz it seems too heavier to me. (I dont agree with Dino in this issue, I suggest DRY, Dont Repeat Yourself. Why do you have to reinvent the wheel?)
5. AJAX is also a must.
6. Being able to write JS in Object Notation format (he he he, its JSON)
7. keep yourself uptodate by visiting sites which inform you what’s actually going on in this sector.
8. Be aware of browser compatibility issues (Its a must)

And Finally

9. Love JS

My Life!! A trueman show?

Hasin, get up, it is 7AM
Its 7:25, office car will arrive soon, please take something for breakfast
Howdy everyone, Its 8 AM, Office Time
&$!&$&$@^%$^%!$#%$#!^%$^%$@^%@ – Are you Insane??
Its 1 PM – Time for lunch
Want some drinks? Candy floss is there at 1:35
Hey – Its 2PM – ^(@*&@^*^@*#^*@#*&@^#*_)(_)(*(*&*&^
Bye Bye Bye – Its 6PM, Everyones Leaving
Hey Still at office? Its 7
Feeling hungry? Well, take some meal getting back to home, Its 8PM
Taxi, wanna goto Home?
Click-Clock-Door Opened, OHHH sweet home……Whats the time??? 9?
Only 10? why not write some pages before you goto bed?
Its 1AM?? OMG, Time to goto bed….i am gonna miss office tomorrow….
GRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
Hasin, Its 7AM………..

Just PHP will give you nothing… unless you upgrade yourself

I have been working with PHP for more than 3yrs (I believe still I am beginner in this category) – I was present in several interview board. Which things disappointed me most is the “lack of eagerness” to learn what comes new. Sometime developers thinks that learning Only PHP will help them to get lucrative jobs!! OMG

Specially in BD most of the time PHP developers plays multiple roles in the companies, they are developer, they are template designers, they are HTML coders, they are DBA, they are PMs….what the heck. Only few companies have different people for these roles.

How far you can go just learning PHP (RAW code, in ancient style, that means PHP+HTML together, yak!!)? You have to have knowledge in CSS, JS, Frameworks, Multiple DBS

When it comes the question of CSS, you should maintain a list of websites where from you can get updates. Dont learn the CSS from book, but goto websites and see what is happening… If you are still using Tables to design (more…)

Installing subversion server

Most of subversion users are familier with Tortoise SVN which removes the need of using svn command line tool. Tortoise is a great tool. It helps a lot as a visual replacement of svn client. What if you want to setup a subversion server in your machine so that developers can work remotely? follow these steps

Before that, download the SVNService from http://gda.utp.edu.co/pub/svn/

1. Create the subversion working directoty by svnadmin tool
Example : svnadmin create “c:\projects\myproject”

2. add authentication setings in your project
open your working directory, such as “c:\projects\myproject” and open the conf/svnserve.conf file. Add the following lines

[general]
anon-access = read
auth-access = write
password-db = passwd

for the settings above, anonymous users will get the read-only access to the repository. If you want authenticated read access, make it as shown below.

[general]
auth-access = read
auth-access = write
password-db = passwd

3. add credentials
open conf/passwd file with notepad and add the username and password in following format

[users]
user1 = pass1
user2 = pass2

4. Install the subversion service and run it
svnservice -install -d -r “c:\projects\myproject”

OR

4. run the server simply
svnserve -d -r “c:\projects\myproject”

Now you can access your subversion repository remotely as “svn://your_server_ip/”

Thats it.

@Full Throttle

Finally I got my PHP team in somewherein. Morshed and Lavlu joins the team this 15th. Rashed an Ryan are doing exciting artworks for overall look-n-feel. Fariha and Jana are maintaining the policies. We are running at full throttle.

PF members are leaving for KL tomorrow. Hope I can join them pretty soon.

We are running at full throttle for this Blog. Yahoooooo

You've got a friend

Searching for more than a year, Got Stacey Kents title “You’ve got a friend”

When you’re down and troubled
And you need a helping hand
And nothing is going right
Close your eyes and think for me
And soon I will be there
To brighten up even your darkest night
You just call out my name
And you know wherever I am
I’ll come running
To see you again

Winter, spring, summer or fall
All you got to do is call
And I’ll be there
Yes, I will
You’ve a friend

If the sky above you
Should turn dark and full of clouds
And that old north wind should begin to blow
Keep your head together
And call my name out loud
Soon, you’ll hear me knocking upon your door

You just call out my name
And you know wherever I am
I’ll come running
To see you again

Winter, spring, summer or fall
All you got to do is call
And I’ll be there
Yes, I will

Ain’t it good to know
You’ve got a friend
When people can be so cold
They’ll hurt you
And desert you
And they’ll take your soul if you let them
But don’t you let them
You just call out my name
And you know wherever I am
I’ll come running
To see you again

Winter, spring, summer or fall
All you got to do is call
And I’ll be there, yes, I will
You’ve a friend