The anatomy of a snippet call

Posted on 3-Apr-08 at 1:35 pm by Zaigham Rana

Recently I came across to a nice wiki article "Snippet call anatomy" written by our Testing Team member, dev_cw. Be sure to check it out, especially the new users.

This one will also be helpful, Common snippet error messages and their meaning.

Meta: RSS feed Permalink

Comments:

1
A nice followup would be... by foniksonik 29-Apr-08 09:51 AM
It would be nice to see an article/how-to on writing php for snippets. Sure there are plenty of examples, but they can get a little confusing. The two basic questions to answer:

How do I set up a snippet parameter for a snippet call?

How do I get the output to return?
2
by dev_cw 09-May-08 06:49 PM
There is a wiki article <a href"http://wiki.modxcms.com/index.php/Creating_Snippets">"Creating Snippets"</a> that has some good information for creating snippets.

To answer your questions: The snippet parameters &param=`value` are reflected as variables to be used within the snippet. So if you have a parameter named &data this is available as a variable in your snippet as $data and can then be used by the snippet code.

The output can be generated basically the same way as with any php function. I use the return command, but others use echo.

Here is a basic example putting the two concepts together. Take this snippet call for example:
[ [mySnippet? &width=`100` &height=`300` ] ]

And here is the snippet:
<?php
$output = "The width is ".$width.", and the height is ".$height.".";
return $output;
?>

This will output: "The width is 100, and the height is 300."
3
Placeholders by Aikdo 10-Jun-08 02:30 PM
Though it is nicer for the future if even with a small script like this you use placeholders.
4
Placeholders by Aikdo 10-Jun-08 03:02 PM
Though it is nicer for the future if even with a small script like this you use placeholders.
5
test by DevilOpa 24-Jun-08 11:08 AM
test

You must be logged into the forums to comment. Please login