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

Category:Library::Markup | Category:Library::External

[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.

Previously, the use of [url=http://jimandlissa.com/project/textilephp]TextilePHP[/url] was the recommended way to get Textile for use with CI. However, TextilePHP now seems to be defunct. You might think that downloading Textile from the [url=http://textile.thresholdstate.com/]Textile homepage[/url] would seem like a good option, but the Textile version 2.0 available for download from the Textile homepage is outdated and contains at least one bug relating to outputting lists.

For the most up to date Textile, download the latest [url=http://textpattern.com/download]Textpattern[/url], extract the files and grab the classTextile.php file. Even though this file is also designated as version 2.0 it is in fact newer than the one that you get from the Textile homepage and works better.

Integrating Textile in to CodeIgniter 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]classTextile.php[/b] file, rename it to [b]Textile.php[/b] and drop it in to your [b]application/libraries[/b] folder.

Add 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