Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 15 revisions

Category:Library::Markup

[url=http://textism.com/tools/textile/]Textile[/url] is a lightweight markup language originally developed by Dean Allen and billed as a "humane Web text generator". Textile converts its marked-up text input to valid, well-formed XHTML and also inserts character entity references for apostrophes, opening and closing single and double quotation marks, ellipses and em dashes.

Integrating [url=http://jimandlissa.com/project/textilephp]TextilePHP[/url] it in to Code Igniter is pretty straight forward, and is ideal for content-based websites where it makes sense to use Textile-formatted text for page content - it can be stored in plain text files and loaded as views, or it can be stored in a database.

[h3]Textile as a CI Library[/h3]

To use Textile in Code Igniter is very simple - just take the [b]Textile.php[/b] file from the TextilePHP release and drop it in to your [b]application/libraries[/b] folder. Then, make one modification to supress the "Undefined index" notices in the actual [b]Textile.php[/b] file as shown below.

At around line 186 is the function declaration.

[code]function Textile($options = array()) { $this->options = $options; ... ...[/code]

Insert the following line immediately beneath the function declaration line, and before [b]$this->options[/b] is set:

[code]error_reporting(E_ALL ^ E_NOTICE);[/code]

A final modification you should make is adding the following line immediately after the opening <?php tag:

[code]if (!defined('BASEPATH')) exit('No direct script access allowed');[/code]

Textile is now ready to load into your controllers and models or in [b]config/autoload.php[/b].

[h3]Using Textile - basic example[/h3]

[b]Controller[/b] [code] function index(){ // Load library $this->load->library('Textile'); // Load the contents of a view into a variable. This view contains Textile-formatted text. $textile = $this->load->view('text/example', NULL, True); // Get Textile library to process the Textile-formatted text into HTML $html = $this->textile->process( $textile ); // Set layout title $layout['title'] = 'Textile example'; // Set the body of the page to the formatted page $layout['body'] = $html; // Load final view $this->load->view('layout', $layout); } [/code]

[b]View:[/b] text/example.php

[code]h3. This is a subhead

p{color:red}. This is some text of dubious character. Isn't the use of "quotes" just lazy writing -- and theft of 'intellectual property' besides? I think the time has come to see a block quote.

  • This is a list item
  • So is this
  • And this one, too![/code]

[b]Output[/b]

The resulting page will contain a Size 3 heading, a red-coloured paragraph of text, and an unordered list:

[code]

This is a subhead

This is some text of dubious character. Isn't the use of "quotes" just lazy writing -- and theft of 'intellectual property' besides? I think the time has come to see a block quote.

  • This is a list item
  • So is this
  • And this one, too!
[/code]

[h3]Using Textile - best practice[/h3]

When processing large Textile pages, it may take longer than desired to load a page, as the text has to be processed each and every time it is loaded. Therefore, it is considered best practice to retrieve only the HTML each time for a given page. There are two methods you can do this in Code Igniter.

[b]* Method one - caching[/b]. Use the native Code Igniter caching facility to cache the HTML output (obviously the page has to be loaded atleast once). However, you must consider how you will handle changes to the page.

[b]* Method two - database[/b]. Have a form to handle the editing of pages - take the Textile input and store it [i]and the HTML output[/i] in a database. When the page is loaded - only retrieve the HTML field for the given page; but when editing the page [i]retrieve the Textile source[/i] so that it can be changed and updated.

Consider the following basic example code.

[b]Database table structure[/b]

[code] CREATE TABLE pages ( page_id INT NOT NULL , title VARCHAR( 100 ) NOT NULL , textile LONGTEXT NOT NULL , html LONGTEXT NOT NULL , PRIMARY KEY ( page_id ) ); [/code]

[b]Controller or model function to fetch page title and HTML[/b]

[code]function GetPage($page_id){ $query_str = "SELECT title,html FROM pages WHERE page_id='$page_id' LIMIT 1"; $query = $this->db->query($query_str); if($query->num_rows() == 1){ return $query->row(); } else { return false; } }[/code]

[b]Controller function to show page[/b]

[code] function page($id){ $page = $this->GetPage($id); $layout['title'] = $page->title; $layout['body'] = $page->html; $this->load->view('layout', $layout); }[/code]

[b]Function to handle update from form[/b]

[code] function UpdatePage($id){ if($this->input->post('textile')){ // Form submitted // IMPORTANT: must use stripslashes $data['textile'] = stripslashes($this->input->post('textile')); $data['html'] = $this->textile->process($data['textile']); // Update database $where = " page_id='$id' "; $query_str = $this->db->update_string('pages', $data, $where); $this->db->query($query_str); } }[/code]

Clone this wiki locally