<ralphdahlgren.com>


 
Navigation
  • Home
  • Etomite CMS News
  • Code Library
    • Snippets
      • DisplayCode
      • EtoConfig
      • ListMenu
      • PoweredBy
      • RenderTime
  • Tutorials
  • Ralphs Rants
  • Site Map
  • Search Help
  • Login|Logout
 
Credits
 
Valid XHTML 1.1 Valid CSS! Powered By PHP Powered By MySQL
Powered By Etomite
 

DisplayCode
Snippet: DisplayCode <?php
/*
// DisplayCode - Text display of Chunk, Snippet, or Template code
// Requires: Etomite 0.6.1 or greater
// Created By: Ralph A. Dahlgren
// Last Modified: 2007-09-10
// Usage: [ [DisplayCode?type=chunk|file|snippet|template&name=Name] ]
// Note: Remember that this snippet uses resource names instead of id's so name changes will effect existing snippet calls
// Also: Files use absolute paths so use ./path/from/Etomite/root
*/

// Snippet messages (the messages below were used for development purposes)
$usage = "Error -- Usage: [ [DisplayCode?type=chunk|file|snippet|template&name=Name] ]";
$error = "Error -- " . $type ." '" . $name . "' is either an invalid name or is empty.";

// Set fieldset amd legend styles (can be inline style or reference to stylesheet components)
// $fieldsetCSS = "class='contentarea'"; // example using a CSS class
$fieldsetCSS = "style='background-color:#ffffff; border:1px solid #000000;'";
$legendCSS = "style='background-color:#ffffff; border:1px solid #000000;'";

// Define which Etomite tags to be rendered non-functional in the returned code
// $oldtags are listed in their original format
$oldtags = array("[ [","[ !","[ *","[ (","[ ~","{ {","[ ^");
// $newtags are listed with a blank space between tag elements
$newtags = array("[ [","[ !","[ *","[ (","[ ~","{ {","[ ^");

// If Chunk or Template
if(($type == "template") || ($type == "chunk"))
{
  if(
$name == '')
  {
    return 
$usage;
  }
  else
  {
    
// Set the appropriate field to extract from the record
    
$field = ($type == "template") ? "content" : "snippet";
    
// Query the appropriate database table for the desired record
    
if($type == "template")
    {
      
$rs = $etomite->getIntTableRows($field,"site_templates","templatename='$name'");
    }
    elseif(
$type == "chunk")
    {
      
$rs = $etomite->getIntTableRows($field,"site_htmlsnippets","name='$name'");
    }
    
// If a record was returned then get the code
    
if(is_array($rs))
    {
      
$code = str_replace($oldtags, $newtags,$rs[0][$field]);
      
$code = "<div class=\"code\">".nl2br(str_replace(" ","&nbsp;",htmlentities($code)))."</div>";
    }
    else
    {
      return 
$error;
    }
  }
}

// If Snippet
if($type == "snippet")
{
  if(
$name == '')
  {
    return 
$usage;
  }
  else
  {
    
// Cache snippet code into a variable
    
$rs = $etomite->getIntTableRows("snippet","site_snippets","name='$name'");
    
// If snippet code is not found, display message
    
if(!is_array($rs))
    {
      return 
$error;
    }
    
// If snippet code exists, process it for display
    
else
    {
      
// Add PHP Begin and End tags and use highlight_string() to colorize the code
      
$code = highlight_string("<?php\n".chr(13).str_replace($oldtags, $newtags, $rs[0]['snippet'])."?>", true);
      
$code = str_replace("<code>","",$code);
      
$code = str_replace("</code>","",$code);
    }
  }
}

// If File
if($type == "file")
{
  if(
$name == '')
  {
    return 
$usage;
  }
  else
  {
    
// If snippet code is not found, display message
    
$code = file_get_contents($name);
    if(
$code == "")
    {
      return 
$error;
    }
    else
    {
      
// If this is PHP then highlight the code
      
if((substr($name,-3) == "php") || (substr($name,-4) == "phps"))
      {
        
$code = highlight_string(str_replace($oldtags, $newtags, $code), true);
      }
      
// All other code gets processed for display
      
else
      {
        
$code = "<div class=\"code\">".nl2br(str_replace(" ","&nbsp;",htmlentities(str_replace($oldtags, $newtags,$code))))."</div>";
      }
    }
  }
}

// Open a fieldset, insert code, and return
return "<fieldset><legend>".ucfirst($type).": ".$name."</legend>".$code."<br /></fieldset>";
// Alternate output format
//return "<div class=\"contentBox1\"><h4>".ucfirst($type).": ".$name."</h4>".$code."<br /></div>";
?>


Powered by Etomite 1.1 (Prelude).
MySQL: 0.0056 s, 16 request(s), PHP: 0.0245 s, Total: 0.0301 s, document retrieved from database
2008-11-21 04:25 PM