Welcome Guest, Not a member yet? Register   Sign In
is there a way to make Codeigniter use a template for the pages ?
#1

[eluser]R_Nelson[/eluser]
I was introduced to Rails 3 by a friend and i like it but the syntax drives me nuts. I was told to look into Codeigniter and it seems to have every thing rails has except that the code is NOT DRY (Don't Repeat Yourself. what i'm trying to ask here is in ruby they have a file called application.html.erb witch is basically the the template for your website with a tag in it to display the action from the controller so in other words i don't have to copy code for every page (example <html><title><body> ect..) tags. Is there a way to make this work or could the developers add this into a release?
#2

[eluser]Derek Allard[/eluser]
Welcome to CI R_Nelson.

There are a few different ways to do this. Here's a couple examples:

1) From your controller:
Code:
$this->load->view('html_open'); // this is common

$this->load->view('page', $vars); // This is unique to this controller

$this->load->view('html_close'); // this is common

2) From your views
Code:
<?php $this->load->view('html_open');?>

<h1>Page heading</h1>

<p>Other normal view stuff here</p>

&lt;?php $this->load->view('html_close');?&gt;

3) View libraries
There are a series of user contributed view libraries available. I've not used any of them, but one I can think of off the top of my head would be Ocular.
#3

[eluser]R_Nelson[/eluser]
not really what i'm looking for let me show you what the ruby file looks like
Code:
<!DOCTYPE html>
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;&lt;/title>
  
&lt;/head&gt;
&lt;body&gt;

<%= yield %>

&lt;/body&gt;
&lt;/html&gt;

the <%= yield %> is what lets the action be displayed or in Codeigniter Function i hope that clears up what i want to do!
#4

[eluser]Derek Allard[/eluser]
This looks to me like a simple view file. I see this is your second post, so forgive me for making assumptions about what you might or might not have had a chance to do.

Here's a basic CI controller
Code:
&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Yield extends Controller {

    function index()
    {
        $vars['yield'] = '14.5%';
        $this->load->view('yield', $vars);
    }
}

/* End of file yield.php */

And the resulting view
Code:
<!DOCTYPE html>
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;&lt;/title>
  
&lt;/head&gt;
&lt;body&gt;

&lt;?= $yield ?&gt;

(or you may prefer)

&lt;?php echo $yield;?&gt;

&lt;/body&gt;
&lt;/html&gt;

Are we on the same page here?
#5

[eluser]ramm[/eluser]
I'm using Smarty3 with CI

http://ilikekillnerds.com/2010/11/using-...i-library/

With the blocks from Smarty, i do something like this:

Master Template
Code:
<!DOCTYPE html>
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;{$title}&lt;/title&gt;
    &lt;link rel="stylesheet" href="css/style.css" type="text/css" /&gt;
&lt;/head&gt;
&lt;body&gt;
    <div id="wrap">
        <div id="head">
            <h1>Title</h1>
        </div>

        <div id="main">
            {block name=content}{/block}
        </div>

        <div id="foot">
            <p>Terms, Copyright, etc</p>
        </div>

    </div>
&lt;/body&gt;
&lt;/html&gt;

Views
Code:
{extends file="includes/master.php"}

        {block name=content}
        <div id="cont">
                    <p>Data here</p>            
        </div>
        {/block}

Which is basicly what you are asking for. It can as deep as you want and also you get in the viw all the Smarty functionality like conditionals, loops, cycles, variables with defaul values, etc.
#6

[eluser]R_Nelson[/eluser]
sorry still not on the same page lets say index.php is
Code:
<p>Hello World!
about.php is
Code:
<p>About

what ruby dose is uses the page in my example as a template and the
Code:
<p>Hello World!
would be place in where yield is! then when it loads about page it would show
Code:
<p>About
in other words im not retyping the html tags for each page its loading the template then yielding the page!

That is the best i can enplane it with out giving you a rails 3 lesson i hope that gets us on the same page!
#7

[eluser]R_Nelson[/eluser]
I'm not sure till i try it but ramm may have my answer!
#8

[eluser]ramm[/eluser]
Yep, same thing.

My about page will be:
Code:
{extends file="includes/master.php"}
{block name=content}
  <div id="cont">
    <h2>About page</h2>
    <p>Lorem ipsum about page...</p>            
  </div>
{/block}


Contact Page
Code:
{extends file="includes/master.php"}
{block name=content}
  <div id="cont">
    <h2>Contact page</h2>
    <p>Lorem ipsum contact page...</p>
    &lt;form&gt;
      contact form...
      //And i love being able to do this:
      {form_input('name', set_value(name))}
      {form_error(name)}
    &lt;/form&gt;          
  </div>
{/block}


All of them "extending" my master template.
#9

[eluser]Armchair Samurai[/eluser]
You can easily do this with CI. Set up a template page (template.php) in the views folder:

Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Foo&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php $this->load->view($content);?&gt;
&lt;/body&gt;
&lt;/html&gt;

Then when you call a view from your controller:

Code:
$this->load->vars(array(
    'content'    => 'page_to_load'
));
$this->load->view('template');
#10

[eluser]kilishan[/eluser]
[quote author="R_Nelson" date="1299042042"]not really what i'm looking for let me show you what the ruby file looks like
Code:
<!DOCTYPE html>
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;&lt;/title>
  
&lt;/head&gt;
&lt;body&gt;

<%= yield %>

&lt;/body&gt;
&lt;/html&gt;

the <%= yield %> is what lets the action be displayed or in Codeigniter Function i hope that clears up what i want to do![/quote]

This is exactly how Ocular works (Thanks for the mention, Derek!). There are a number of other template libraries out there, also, and I think that most of them operate under similar principles.




Theme © iAndrew 2016 - Forum software by © MyBB