Retrieving the raw source code of MODX Revolution Elements

Retrieving the raw source code of MODX Revolution Elements

Simple Snippet to display raw source code

Posted on February 22, 2012 at 22:55:00
Tags: Chunks, snippet, sourcecode
Words: 510
Level: Advanced
Dependencies: None

Far too often it can prove beneficial to display the source code of a MODX Revolution Chunk or Snippet in a Resource for discussion or blog type articles.

Here is a simple method to do just that:

Displaying Snippet source

 Snippet Call: [[getElementSourceCode?name=`getElementSourceCode`]]

The Snippet Contents

<?php

/* File         getElementSourceCode.php (MODx snippet)
 * Created on	Feb 21, 2012
 * Project	shawn_wilkerson
 * @package	MODx Revolution Scripts
 * @version	3
 * @category	snippet
 * @author	W. Shawn Wilkerson
 * @link	http://www.shawnwilkerson.com
 * @copyright   Copyright (c) 2009, W. Shawn Wilkerson.  All rights reserved.
 * @license     GNU General Public License 2
 *
 ************************************************
 * Purpose: to utilize MODx Revolution to access the raw source of an Element for display in a Resource.
 *
 * Requirements: user must supply chunk/plugin/snippet/template name in snippet call
 *
 * Example:
 * <pre class="brush: php; html-script: true">
 *	 [[showSnippet?name=`randomImages`]]
 * </pre>
 *
 *
 * Dependencies: MODx Revolution 2.x +
 *
 *
 * Variable lexicon:
 * 	Via Snippet call:
 * 		$name (string)	actual name of the MODX Revolution Element
 * 		$type (string)	MODX Revolution Object type: Snippet / Chunk
 *
 * Snippet internal vars:
 * 		$content	(string) source code from snippet
 * 		$obj     	(object) MODX Object of the specified type
 *
 ************************************************/
$content = '';
$elementName = $modx->getOption('name', $scriptProperties, '');
$elementType = ucfirst(strtolower($modx->getOption('type', $scriptProperties, 'Snippet')));
$wsw = false;

$allowedTypes= array('Chunk', 'Plugin', 'Resource', 'Snippet', 'Template');
if (in_array($elementType, $allowedTypes)) {

if($elementType == 'Resource'){
$obj = $modx->getObject('mod' . $elementType, array(
        'id' => $elementName
    ));
}else {
    $obj = $modx->getObject('mod' . $elementType, array(
        'name' => $elementName
    ));
}
    if (is_object($obj)) {
        if (($elementType == 'Snippet') || ($elementType == 'Plugin')) {
            /* inject a beginning tag for cosmetic reasons - seperated to prevent PHP errors */
            $content .= '<' . '?' . 'php' . "\n\n";
        }
        /* Retrieve the raw source */
        $content .= $obj->getContent();

        /* Sanitize the source */
        $codeEntities = array("<", ">", "[", "[", "{", "}");
        $replaceHtml = array("<", ">", "[", "]", "{", "}");
        $wsw = str_replace($codeEntities, $replaceHtml, trim($content));
    }
}
return $wsw;

The Site Footer

 <footer class="container-fluid">
	<div class="row well">
		[[*FooterContent]]
	</div>
</footer>
<div class="container copyright">
	<div class="row">
		<div class="col-lg-5">
			[[getCurrentYear:toPlaceholder=`currentYear`]] © [[*createdon:strtotime:date=`%Y`:lt=`[[+currentYear]]`:then=`[[*createdon:strtotime:date=`%Y`]] - `]]  [[+currentYear]] [[++site_name]]. All Rights Reserved.
		</div>
		<div class="col-lg-7">
			<div class="pull-right">
				Powered By: <a href="//sanityllc.com" target="_blank" rel="nofollow">Sanity LLC</a>, <a href="//www.xpdo.org" target="_blank" rel="nofollow">xPDO</a>,
				<a href="//www.modx.com" target="_blank" rel="nofollow">MODX Revolution [[++settings_version]]</a>, <a href="//getbootstrap.com/" target="_blank" rel="nofollow">Bootstrap 3</a>, <a href="//www.jquery.org" target="_blank" rel="nofollow">jQuery</a>
			</div>
		</div>
	</div>
</div>
MODX:
Website development platform combining security, power, and innovation.