Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you organize your template structure in CodeIgniter?

I will be building a CMS for my final Web Development course project, and i've been challenged by my teacher creating it in an MVC system, and for now, I'll be using CodeIgniter.

Therefore, I would like to know how do you organize your file / folder structure.

I will be using a simple templating system: - For now I have a templates.php config file that is being autoloaded and says the name of the selected template, and the absolute path to it. - The templates folder is inside the views folder (is this the most correct way to do this?) - Right now I am having some problem to access .css files through the files in the view, and, from what I've read people usually put all that files outside the application folder.

How do you normally do it? And, as seeing what I am building, what advices can you give me? Shouldn't views contain all the stuff from templates? (including CSS inside the folder of templates)

Thanks.

(i'm using the Code Ignitor 2.1.0)

EDIT:

Right now, after structured all the files and folders, I am in a dilemma. I have added the following in constants.php

<!-- language: lang-php -->
$config['selected_template'] = 'oceania';
$config['template_abs_path'] = 'assets/templates/'.$config['selected_template'];

define('IMAGES_PATH', $config['template_abs_path'].'/img/' );
define('CSS_PATH', $config['template_abs_path'].'/css/' );
define('SCRIPTS_PATH', $config['template_abs_path'].'/js/' );

(if I didn't define the selected_template and template_abs_path I could play with them in the constants)

And i'm including them in the HTML like this:

<!-- language: lang-html -->
<link rel="stylesheet" type="text/css" href="<?=CSS_PATH;?>style.css" />

But now, I was trying to get a title stored in config too, but I can't do it, because I need all this variables to be in the other templates.php file that I had created previously, otherwise it will not recognize my variables. (I have templates autoloading)

templates.php

<!-- language: lang-php -->
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config['title'] = 'Title of the website';
$config['selected_template'] = 'oceania';
$config['template_abs_path'] = 'templates/'.$config['selected_template'];

header.php

<div id="logo">
<h1><a href="#"><?=$this->config->item('title');?></a></h1>
</div>

Therefore, do I need to have both config to have it working on my website? It really doesn't make much sense for me...

like image 396
Ivo Pereira Avatar asked Feb 26 '12 23:02

Ivo Pereira


People also ask

How do I use a template in CodeIgniter?

There is a library which allows you to use templates in CodeIgniter in a native style. To load a template/theme just do: $this->load->css (‘path/file.css’); $this->load->js (‘path/file.js’); You can optionally control the way browsers cache CSS & JS files. A Codeigniter template is generally just a PHP file.

How do I set up a new CodeIgniter project?

In this article, I’d like to walk you through my first few steps when setting up a new CodeIgniter Project: – Setting up a way to render pages in a layout – Setting up a way to easily handle assets (CSS & JavaScript files). To start, we are going to make two new files, a layout view, and a localized controller.

What is the application folder in CodeIgniter?

Application directory in CodeIgniter This folder is the core use for developers. This application folder, like its name, is the container of all of the codes that you will do to build your project. Anything you will be doing and any file you will be creating will reside in this folder and its subfolders.

What are MVC patterns used by CodeIgniter?

Models, Views, and Controllers (MVC) patterns are used by CodeIgniter to organized the files. This helps us to maintain the data, the presentation, and flow through the application. To make things more clear we can understand with their basic definition –


2 Answers

A good file/folder structure would be the below:

website_folder/
–––– application/
–––––––– config/
–––––––––––– autoload.php
–––––––––––– config.php
–––––––––––– ...
–––––––– controllers/
–––––––––––– examples.php
–––––––––––– index.html
–––––––––––– welcome.php
–––––––– ...
–––––––– views/
––––––––---- templates/
––––––––-------- backend.php
––––––––-------- frontend.php
–––––––––––– ....
–––––––––––– example.php
–––––––––––– index.html
–––––––––––– welcome_message.php    
–––– assets/
–––––––– css/
–––––––– js/
–––––––– images/
–––––––– templates/
––––––––---- frontend/
––––––––-------- css/
––––––––-------- js/
––––––––-------- images/
––––––––---- backend/   
––––––––-------- css/
––––––––-------- js/
––––––––-------- images/
–––––––– uploads/
–––––––– index.html
–––– system/
–––– user_guide/
–––– index.php
–––– license.txt 

This is just a suggestion. So you will have your template views at views/templates and your css/js files at assets/templates/

like image 159
John Skoumbourdis Avatar answered Nov 15 '22 16:11

John Skoumbourdis


I am totally agreed with web-johnny file folder structure but in my opinion he missed one thing define all your assets path in ./application/config/constants.php file e.g.

define('IMAGES_PATH', your_absolute_path_of_images_folder);
define('CSS_PATH', your_absolute_path_of_css_folder);
define('SCRIPTS_PATH', your_absolute_path_of_scripts_folder);

and so on... use these constants throughout the application so in this way if you ever need to change your file folder structure you just need to change the values of these constants.

like image 30
Code Prank Avatar answered Nov 15 '22 14:11

Code Prank