Quote from: fegul at Sep 16, 2007, 02:10 PM
I have a PHP script that echos an array and I would like to specify the location to a specific part of the array in the snippet call
(e.g. {{snippetname &array_name="sample_array" &array_location="2"}} which would print the data located in sample_array[2] which is created in the snippet.
Any ideas as to how to how I could take the snipppet call and integrate it in my currently existing PHP script?
If I’m understanding you correctly, the typical way to do it is to put all your PHP code in the snippet (i.e., your code IS the snippet).
If you add array_name and array_location as a parameters in the snippet, they will be available in the snippet as PHP variables. See some of the existing snippets (e.g. ditto) to see how to set this up and access the parameter values in the snippet. Your snippet code would then create the output that would appear on the screen where the snippet call is placed in the document with the snippet call.
BTW, snippets are called with [!SnippetName!] rather than {{xxx}}
{{}} is for chunks, which usually contain straight HTML, not PHP code.
[!SnippetName!] will call the snippet uncached, which I think you’ll want to do since the snippet will produce dynamic results.
Also, be sure your parameters are enclosed in backticks, not single or double quotes, and put a question mark after the snippet name:
[!snippetname? &array_name=`sample_array` &array_location=`2`!]
The quick and dirty method to do what you want would be to put the code that presents the data inside your snippet.
Later, you may want to make your snippet more of an official MODx snippet by creating placeholders in the snippet and using those placeholders in presentation code that’s contained in the document with the snippet call. That separates the presentation layer from the code behind it. It also makes your snippet much more flexible so that it can be used, unchanged, for different purposes.
Bob