Side
Side

Reputation: 1751

CodeIgniter templates

Hello first sorry if its a stupid question, but I amm a beginner, and im a bit lost.

For practice I decided to code a really small social network just for practice.

I worked on small CodeIgniter projects in the past, and the truth is I dont really like loading the header and footer in every controller.

So on YouTube I saw a video about creating basic templating.

But I can't really decide whether it is a good idea.

So how would it look like?

In my view folder I created 3 subfolders

- include 
- template
- user 

In the templates I have the main_template.php and that looks like this:

 <!DOCTYPE html>
 <html>
 <head>
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="<?php base_url();?>themes/default/style.css">
    <title><?php echo $title; ?></title>
 </head>
 <body>

</body>
</html>

The controllers would contain different variables for the title and other stuff if needed for each view.

My question is: is it a bad idea not create header and footer files, and creating all views with including the html, head, and body tag like this?

Upvotes: 1

Views: 1032

Answers (2)

Abilon Dias
Abilon Dias

Reputation: 11

Don't know if I get you right. But, I will put some examples how to use the Template library.

It isn't included, you can take It here : http://gist.github.com/1519385.

Put the Template.php file in /application/libraries.

Make a directory on the views, name It "templates" and inside a "main.php". This "main.php" file, the name describes it well, let's say that It is your Main template.

Example : http://pastie.org/3071074

There you can see the $content variable, It is defined on your controller.

Example : http://pastie.org/3071097

Thats a very basic example, you can extend It more as you need. Hope It helps.

Upvotes: 1

Ayman Safadi
Ayman Safadi

Reputation: 11552

I would recommend using a "header" and "footer". Otherwise it's going to get really messy when you'r trying to customize each template. I get passing down a few variables to the views; actually, that will come in really handy when setting the page's title, meta description, JS/CSS includes, etc. However if you keep your header/footer together, you will have to either pass the entire body in a variable or pass in the name of the specific view you want; in which case you would have to load the specific view inside this main view.

Upvotes: 2

Related Questions