API:runSnippet
From MODx Wiki
API Function Definition:
runSnippet
- Versions
- > 0.9.2
- Return Values
- Success: Output of a given snippet
- Fail: empty string (or false?)
- Data Type
- string
- Object Hierarchy
- DocumentParser
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.
[edit]
Examples
$dittoOutput = $modx->runSnippet( "Ditto", array( "tpl" => "<post><title>[+title+]</title><summary>[+summary+]</summary></post>" ) );
[edit]
Related functions
[edit]
Function source
| File: | manager/includes/document.parser.class.inc.php |
|---|---|
| Line: | 1689 |
$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); // run snippet return $this->evalSnippet($snippet, $parameters); }
[edit]
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);
