Skip to content

Instantly share code, notes, and snippets.

@goldsky
Last active July 20, 2016 12:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goldsky/60862e232c050feaca39 to your computer and use it in GitHub Desktop.
Save goldsky/60862e232c050feaca39 to your computer and use it in GitHub Desktop.
includeFile snippet is to include any file in MODX's page, either in resource, template, or chunk
<?php
/**
* includeFile snippet is to include any file in MODX's page, either in resource, template, or chunk
*
* @author goldsky <goldsky@virtudraft.com>
* @copyright Copyright (c) 2015, goldsky
* @example [[!includeFile? &file=`[[++core_path]]statics/chunks/mychunk.chunk.tpl`]]
* [[!includeFile? &file=`[[++core_path]]statics/snippets/mysnippet.snippet.php`]]
*
* Adding "&toPlaceholder" parameter ...
* [[!includeFile?
* &file=`[[++core_path]]statics/chunks/mychunk.chunk.tpl`
* &toPlaceholder=`my_placeholder`
* ]]
* ... creates [[+my_placeholder]] for multiple calls in the page
*/
if (!isset($file) || $file == "") {
$modx->log(modX::LOG_LEVEL_ERROR, "[includeFile] File was not defined!");
return;
}
if (!file_exists($file)) {
$modx->log(modX::LOG_LEVEL_ERROR, "[includeFile] could not find file: " . $file);
return;
}
$pathinfo = pathinfo($file);
if ($pathinfo['extension'] === 'php') {
$content = include $file;
} else {
ob_start();
include $file;
$content = ob_get_contents();
ob_end_clean();
}
if ($toPlaceholder) {
$modx->setPlaceholder($toPlaceholder, $content);
return;
}
return $content;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment