runSnippet

API Function Definition
Variable name: runSnippet
CMS versions: 0.9.x + Evo
Input parameters: string $snippetName [, array $params]
Return if successful: Output of a given snippet
Return on failure: empty string (or false?)
Return type: string
Object parent:

API:runSnippet

Description / Usage

string runSnippet(string $snippetName [, array $params]);

Retrieves a snippet from the database (or cache) given the name of the snippet. Extra parameters may also be sent to the snippet via it's optional second parameter. This function is usually used to run a snippet from within other Snippets or even Modules.

Examples

$dittoOutput = $modx->runSnippet(
"Ditto",
array(
"tpl" => "<post><title>[+title+]</title><summary>[+summary+]</summary></post>"
)
);

Related functions

Notes

The paramaters array passed to this function should be of the form 'paramater' => 'value'; If you prefer a more readable function call, create an array first, such as:

$params['param1'] = 'value1';
$params['param2'] = 'value2';

$html = $modx->runSnippet('mysnippet', $params);

Function source

Source File - manager/includes/document.parser.class.inc.php - 1689

function runSnippet($snippetName, $params= array ()) {
if (isset ($this->snippetCache[$snippetName])) {
$snippet= $this->snippetCache[$snippetName];
$properties= $this->snippetCache[$snippetName . "Props"];
} else { // not in cache so let's check the db
$sql= "SELECT * FROM " . $this->getFullTableName("site_snippets") . " WHERE " . $this->getFullTableName("site_snippets") . ".name='" . mysql_escape_string($snippetName) . "';";
$result= $this->dbQuery($sql);
if ($this->recordCount($result) == 1) {
$row= $this->fetchRow($result);
$snippet= $this->snippetCache[$row['name']]= $row['snippet'];
$properties= $this->snippetCache[$row['name'] . "Props"]= $row['properties'];
} else {
$snippet= $this->snippetCache[$snippetName]= "return false;";
$properties= '';
}
}
// load default params/properties
$parameters= $this->parseProperties($properties);
$parameters= array_merge($parameters, $params);
// run snippet
return $this->evalSnippet($snippet, $parameters);
}

Notes

The paramaters array passed to this function should be of the form 'paramater' => 'value'; If you prefer a more readable function call, create an array first, such as:

$params['param1'] = 'value1';
$params['param2'] = 'value2';
$html = $modx->runSnippet('mysnippet', $params);

Suggest an edit to this page.