Etomite Community Forums: runSnippet() - Etomite Community Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

runSnippet() Runs a snippet from within a snippet

#1 User is offline   PaulD 

  • Likes Etomite Forums!
  • PipPip
  • Group: Member
  • Posts: 333
  • Joined: 06-February 06
  • Gender:Male
  • Location:Lutterworth, Leicestershire

Posted 17 February 2009 - 04:31 AM

This function allows you to run any snippet from within another snippet.

From Documentation

Quote

runSnippet($snippetName, $params=array())
// returns the processed results of a snippet to the caller
// $snippetName = name of the snippet to process
// $params = array of $key=>$value parameter pairs passed to the snippet


The example below is querying the ListMenu snippett and sending a few variables to it using the runSnippet function.

// set the variables I am sending to snippett
$LM_node=28;
$LM_levelsDeep=-1;

// make them into an array
$params=compact('LM_node','LM_levelsDeep');

// run the snippett ListMenu and return processed contents to $output.
$output .= $etomite->runSnippet('ListMenu', $params);

// return output
return $output;


There is no error checking in this example. If the called snippet fails I don't know if this will return false or just fail completely. When I called a script that didn't exist I believe the function returned false.

Paul.

PS I would hate to test it, but what happens if runsnippet is used to call the snippet containing runsnippet. Presuming a logical loop was used so it wasnt an infinite loop, how many levels deep could it deal with?

This post has been edited by PaulD: 17 February 2009 - 04:33 AM


#2 User is offline   Cris D. 

  • Loves Etomite Forums!
  • PipPipPipPip
  • Group: Member
  • Posts: 1,076
  • Joined: 10-August 06
  • Gender:Not Telling

Posted 17 February 2009 - 10:04 AM

Snippets in snippets are evaluated according to how many snippet parses there are when each page loads. This used to be taken care of in the index.php, but Ralph kindly added this setting to the manager config settings as of etomite v1.0+. The default is 5 parses (which means that a runSnippet will stil be evaluated 5 levels deep....in theory. In practice, if a uncached snippet is more that 2 levels deep it will not be evaluted. This is due to a bug in the code base logic. This can be enhanced by [[caching]] snippets, in which case, each runSnippet will be evaluated up to the last uncached snippet. This can cause problems for those requiring uncached snippets.

A resolution was added to this problem in the bug tracker which is yet to be impemented. You can make the patch change yourself if you like. So...

If you have multiple snippets in snippets and the results are not being returned, cache the first few snippets or pages (if they are able to be cached and still functions as expected) and the additional snippets will be evaluated.

This post has been edited by Cris D.: 08 July 2009 - 09:10 PM


#3 User is offline   Cris D. 

  • Loves Etomite Forums!
  • PipPipPipPip
  • Group: Member
  • Posts: 1,076
  • Joined: 10-August 06
  • Gender:Not Telling

Posted 31 October 2009 - 04:18 AM

Another good use of runSnippet() is to load other snippets that contain functions or classes that you want to load into memory to use for the existing snippet:

An example of this is the siteErrorHandler snippet which as the documentation says, the error handling snippet can be called into the header of site template as normal [!snippet!]. However, if you only want to use it for part of the site, you can load it just before you need it at the beginning of another snippet like this:

$inc = $etomite->runSnippet('siteErrorHandler');

Note that I have passed the value of the snippet to a variable ($inc), while this is not strictly required for just functions, I find it easier to handle any errors with the snippet if they are contained to a variable and in some instances it is a requirement for loading a class (depending on how the original class is step up and instantiated). It's actually possible to call another snippet that loads just functions on demand like this:

$etomite->runSnippet('siteErrorHandler');

Anyway, my point is that runSnippet() lets you do much more than just run another snippet, it can be used as an include() or require().

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users