runSnippet
API Function Definition
Name | runSnippet |
Versions | > 0.9.2 |
Parameters | string $snippetName [, array $params] |
Return success | Output of a given snippet |
Return failure | empty string (or false?) |
Return type | string |
Object hierarchy | DocumentParser |
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
- [getChunk]
- [evalSnippet]
Function source
API 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 on GitHub (Requires GitHub account. Opens a new window/tab).