Giter VIP home page Giter VIP logo

codeigniter-contentful's Introduction

Contentful Templates for CodeIgniter

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 or the zipball, depending upon your poison of choice, and extract it.
  2. Copy the following files into your application directory:
    • config/contentful.php
    • helpers/contentfulmanager_helper.php
    • libraries/contentful.php
    • libraries/contentfulmanager.php

Usage

In your controller: application/controllers/contentfultest.php:

class ContentfulTest extends CI_Controller {
  public function index() {
    $this->load->library('contentful');

    $data['name'] = 'Shawn Dellysse';
    $this->contentful->load('contentful_test/index', $data);
  }
}

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 application/views/layouts/default.html.php. Your per-page view will be loaded from application/views/contentful_test/index.html.php.

In your layout application/views/_layouts/default.html.php:

<html>
  <head>
    <title><?php echo contents_of('title') ?></title>
    <?php echo contents_of('head') ?>
  </head>
  <body class="<?php echo trim(contents_of('body-class'))?>">
    <h1>Layout</h1>
    <hr/>
    <h1>View:</h1>
    <div style="border: 3px coral solid">
      <?php echo contents_of_main() ?>
    </div>
  </body>
</html>

The contents_of('some section') will return the content inside the content_for('some section') block written in the view.

In your view application/views/contentful_test/index.html.php:

<?php content_for('title') ?>
  Greetings <?php echo $name ?>!
<?php end_content_for() ?>

<?php content_for('body-class') ?>
  greetings-page
<?php end_content_for() ?>

<?php content_for('head') ?>
  <script type="text/javascript">
    // We got scripts
  </script>
  <style type="text/css">
    /* and we got css too */
  </style>
<?php end_content_for() ?>

Hello, <?php echo $name ?>!

Each content_for($section) should map to a contents_of($section) in your layout. Please note that, although they are style and indented as such, 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):

<html>
  <head>
    <title>  Greetings Shawn Dellysse!  </title>
    <script type="text/javascript">
      // We got scripts
    </script>
    <style type="text/css">
      /* and we got css too */
    </style>
  </head>
  <body class="greetings-page">
    <h1>Layout</h1>
    <hr/>
    <h1>View:</h1>
    <div style="border: 3px coral solid">
      Hello, Shawn Dellysse!
    </div>
  </body>
</html>

Configuration

No initial configuration is necessary; however, all options are in config/contentful.php and are documented there.

codeigniter-contentful's People

Contributors

sdellysse avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

codeigniter-contentful's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.