Welcome Guest, Not a member yet? Register   Sign In
Contentful Templates for CodeIgniter
#1

[eluser]Unknown[/eluser]
Github Repository: https://github.com/shawndellysse/codeigniter-contentful

From the much-better-formatted README(https://github.com/shawndellysse/codeign...E.markdown):

What is Contentful?

Heavily inspired by Rails 3's template system, Contentful is a templating library for CodeIgniter (tested on 2.0.1 Reactor) and PHP 5. It allows you to easily keep your layouts and views compeletely separate from your controllers. It also lets the individual view apply changes to your layouts.

Installation

1. Download either the tarball(https://github.com/sdellysse/codeigniter...all/master) or the zipball(https://github.com/sdellysse/codeigniter...all/master), depending upon your poison of choice, and extract it.
2. Copy the contents of the extracted directory to your CodeIgniter project root.
3. That's it. It comes with an example controller/layout/view combination for testing purposes. Assuming your project url is http://localhost/ci, point your web browser to http://localhost/ci/index.php/contentfultest.

Usage

In your controller:
Code:
class ContentfulTest extends CI_Controller {
  public function index() {
    $this->load->library('contentful');
    $this->name = 'Shawn Dellysse';
    $this->contentful->load();
  }
}

This will load the library, set a variable in the view, and then load your layouts and views. Since you didn't specify otherwise, it will look for your layout in
Code:
application/views/layout/default.html.php
and will look for your view in
Code:
application/views/contentfultest/index.html.php
.

In your layout:

Code:
<html>
  <head>
    <title><?php echo contents_of('title') ?></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js" type="text/javascript"></script>
    <?php echo contents_of('head') ?>
  </head>
  <body class="<?php echo contents_of('body-class', 'trim')?>">
    <h1>Layout</h1>
    <hr/>
    <h1>View:</h1>
    <div style="border: 3px coral solid">
      &lt;?php echo contents_of('main_area') ?&gt;
    </div>
  &lt;/body&gt;
&lt;/html&gt;

The
Code:
contents_of($section)
will return the content inside the
Code:
content_for
block written in the view. The first argument to
Code:
contents_of
is always the name of the section; it can also take an arbitrary number of strings after the section that are the names of functions that will format the output. In this case, for the
Code:
body-class
section, that contents_of call is equivalent to
Code:
&lt;?php echo trim(contents_of('body-class')) ?&gt;
. In general,
Code:
contents_of('some-section', 'a', 'b', 'c')
will be equivalent to
Code:
a(b(c(contents_of('some-section'))))
.

In your view:

Code:
&lt;?php content_for('title') ?&gt;
  Greetings &lt;?php echo $name ?&gt;!
&lt;?php end_content_for() ?&gt;

&lt;?php content_for('body-class') ?&gt;
  greetings-page
&lt;?php end_content_for() ?&gt;

&lt;?php content_for('head') ?&gt;
  &lt;script type="text/javascript"&gt;
    $(document).ready(function() {
      //...
    });
  &lt;/script&gt;
&lt;?php end_content_for() ?&gt;

Hello, &lt;?php echo $name ?&gt;!

Each
Code:
content_for($section)
should map to a
Code:
contents_of($section)
in your layout. Please note that, although they are style and indented as such,
Code:
content_for
blocks are *not* control structures. All code inside a block will always be evaluated.

When run with the above layout, view, and controller (with a little bit of tidying):

Code:
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;  Greetings Shawn Dellysse!  &lt;/title&gt;
    &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js" type="text/javascript"&gt;&lt;/script&gt;
      &lt;script type="text/javascript"&gt;
        $(document).ready(function() {
          //...
        });
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body class="greetings-page"&gt;
    <h1>Layout</h1>
    <hr/>
    <h1>View:</h1>
    <div style="border: 3px coral solid">
      Hello, Shawn Dellysse!
    </div>
  &lt;/body&gt;
&lt;/html&gt;

Configuration
-------------

Edit
Code:
application/config/contentful.php
and
Code:
application/config/contentfulmanager.php
.

Edit: Due to feedback, 'yield' became 'contents_of'. It is more fitting with the name, and more readable. Thanks _druu.




Theme © iAndrew 2016 - Forum software by © MyBB