How to Use the BacklitArticle Class

You create an instance of the BacklitArticle class like this:

1
$article = new BacklitArticle($article_id);

The article_id parameter corresponds to the article_id field in the bl_articles table in your database.

If you want to use the GeSHi code highlighter, you can optionally pass in the enable_geshi parameter:

1
$article = new BacklitArticle($article_id, true);

To get the article's title you use the getTitle method:

1
echo $article->getTitle();

To only show the title if the article's show_title property is set, use the getShowTitle method:

1
if ($article->getShowTitle()) echo '<h1 class="page_title">' . $article->getTitle() . '</h1>';

To get the article's content, use the getContent method:

1
echo $article->getContent();

To get the article's publish date, use the getPublishDate method:

1
$date = $article->getPublishDate();

To format the publish date use PHP's date function:

1
echo date('M j, Y', strtotime($article->getPublishDate()));

To generate the HTML breadcrumbs for an article use the getBreadcrumbTrail method:

1
echo '<div id="breadcrumbs">' . $article->getBreadcrumbTrail() . '</div>';

To generate a HTML list of links to the article's children, use the getChildrenAsList method:

1
echo $article->getChildrenAsList();

You can optionally pass in order_by, direction, limit, offset, prefix_publish_date, and date_format parameters.

The order_by parameter is a string and must be either 'title' or 'publish_date'. The direction parameter is a string and must be either 'ASC' or 'DESC'. The limit parameter allows you to limit the number of children returned. The offset parameter will offset the records returned by the database. The prefix_publish_data will prefix the article's publish date to the title in the list. The date_format parameter determines how the date prefix is formatted and must be a valid format for the PHP date function.

As an example, if you had an article with a title of Press Releases and an article_id of 2, and you filed all your press releases as children of that article, you could generate a HTML list of the last 6 press releases using the following code:

1
echo $article->getChildrenAsList('publish_date', 'DESC', 6, 0, true);

You could then use CSS to style the list however you liked, including removing the bullet points.

To generate a HTML list of links to the article's grandchildren, use the getGrandchildrenAsList method:

1
echo $article->getGrandchildrenAsList();

The getGrandchildrenAsList method has the same optional parameters as getChildrenAsList.

As an example, if you had a sitemap like the following:

  • Home
    • Press Releases
      • 2008 Press Releases
        • Press Release on 12/15/2008
        • Press Release on 12/27/2008
      • 2009 Press Releases
        • Press Release on 1/9/2009
        • Press Release on 1/22/2009

You could generate a HTML list of links to the last three press releases using the following code:

1
echo $article->getGrandchildrenAsList('publish_date', 'DESC', 3);

This would create the following list of links:

  • Press Release on 1/22/2009
  • Press Release on 1/9/2009
  • Press Release on 12/27/2008

To generate a site map for an article, use the getDescendantsAsList method:

1
echo $article->getDescendantsAsList();

Unlike the getChildrenAsList and getGrandchildrenAsList methods, the getDescdantsAsList method does not take any parameters.

If the getChildrenAsList or getGrandchildrenAsList methods do not suit your needs, you can use the getChildren or getGrandchildren methods, which return an array of objects with the properties: article_id, title, and publish_date. You could then loop through the array and display links in any configuration you liked.

To determine if the article's content contains GeSHi highlighted code, use the getHasGeshi method:

1
$has_geshi = $article->getHasGeshi();

You could then include the GeSHi stylesheet like this:

1
 <?php if ($has_geshi) echo '<link href="/css/geshi.css" rel="stylesheet" type="text/css" />'; ?>

If the enable_geshi parameter is set to true in the constructor, GeSHi will highlight code that is enclosed by the <pre> tag and has a language or lang attribute. For example the following code would be highlighted by GeSHi:

1
2
3
4
5
<pre lang="php">
<?php
echo "Hello World!";
?>
</pre>

This would produce the output:

1
2
3
<?php
echo "Hello World!";
?>

GeSHi will place line numbers if you specify class="start1". You can also start the line numbers at some number other than 1. For no line numbers, leave off the class attribute.

You can provide your code with a title by using the title attribute. For example, the following would generate GeSHi highlighted code with a title of HelloWorld.php, and with line numbers starting at 1

1
2
3
4
5
<pre class="start1" lang="php" title="HelloWorld.php">
<?php
echo "Hello World!";
?>
</pre>

This would produce the output:

HelloWorld.php
1
2
3
<?php
echo "Hello World!";
?>

GeSHi requires a special stylesheet to highlight code. CuriousFind keeps an up-to-date copy of GeSHi and you can generate a stylesheet to download for your site by clicking here.

Internet Explorer 6 and 7 may require some CSS tweaks to get GeSHi (and indeed other parts of your website) to display correctly. You can add rules to the fix_ie6.css and fix_ie7.css files. For example, to get the code on the CuriousFind website to display correctly in Internet Explorer, the following had to be added to both fix_ie6.css and fix_ie7.css:

1
2
3
4
5
6
div.geshi_wrapper {
    overflow: auto;
    overflow-X: scroll;
    width: 920px;
    position: relative;
}