How to test result from a snippet?


A have built a custom snippet which return a calculated value.
I want to test this returned value inside a resource and load a chunk according to the result.

Do you know what is the best way to do this ?

1 Like

I believe a chunk with something like this will work


With whatever value name you have given to the calculated value.

If the snippet runs on the same page then the value is available on that page, I am pretty sure about this. It is possible to be overwritten if the value name is used by some other tool, I mean if the snippet defines ‘output’ and a sidebar for example also defined a value ‘output’ you would lose the first one

Hope that makes sense and will help

Yes it’s working !Thanks for the tip !
In fact it also missing this line on my snippet :


Great! So which name gets the results, ‘value’ or ‘result’? I don’t know coding haha, only css/html

Output chunks are a really great part of Modx, I don’t know if it is Modx specific tbh. You can ‘call’ the chunk directly in your snippet, or even put the html in there as well.

But the best way is to have the html structure chunk, with the placeholders in there. For example, you could output not just the calculated result, but all of the variables involved if you wanted to, or you could even have dynamic things in the output, although usually you would use multiple chunks, but honestly you don’t need to.

You have a number of options.

The fastest, if you’re setting a single value, is to have the snippet return a value (which will always be a string, even if it’s a number). The return value from the snippet will replace the snippet tag on the page.

You can also put one or more placeholders in a chunk, have the snippet get the chunk with $modx->getChunk('chunkName'), use str_replace() to replace the placeholder(s), and return the result, which will again replace the snippet tag on the page. In this case, you’d use the full placeholder tag (e.g., '[[+placeholderName]]') as the needle for str_replace().

In addition, there’s a variation of that where you send an associative array of placeholder names and values as a second argument like this $modx->getChunk('chunkName', $value Array). MODX will replace the placeholders as it gets the chunk. In this case, you use just the placeholder names (no brackets or + sign) for the array keys.

Finally, you can put the placeholder tags in the chunk (or in the resource content field or the template) and use setPlaceholder('placeholderName, $value)’ or $modx->setPlaceholders($valueArray). This lets you put multiple placeholders at various points on the page. Again, no brackets or + sign. It requires that the snippet tag be above any placeholder tags or tags for chunks containing placeholders.

For specific cases where you want to set placeholders based on the fields of another MODX object (for example, a user or user profile object), you can get the object and call its toArray() method, which will create exactly the kind of array that getChunk() or setPlaceholders() will use…

That would look something like this:

$user = $modx->getObject('modUserProfile', array('email', '');
$placeholders = $user->toArray();
return $modx->getChunk('someChunk', $placeholders);
// or $modx->setPlaceholders($placeholders);

You can define it as you want :slight_smile:

Do you have an example because it was what I tried and it didn’t work.
I had to set manualy the placeholder and on my resource code I have to place the snippet call AND the placeholder with brackets and +
If there is a way to have only the snippet call which return the result value, I’m interested :slight_smile:

You need just to return your result at the end.
return $result;


FYI, snippets and plugins should always have a return statement, even if you’re setting placeholders, in which case it’s just return; or return ""; and they should never have a closing PHP tag.

You could also write the error log.

To expand on ilja-web’s answer, you can put this in your code to write to the error log:

$modx->log(modX::LOG_LEVEL_ERROR('Some Message'));

This is particularly handy when developing plugins, which can crash with no error message and often don’t display messages on the screen.

1 Like

the value is not working

1 Like

Why not provide your code? We can help

Pls put all code inside triple single ticks above and below the code, I believe that’s the best way to preserve all of the code, could be wrong. So, below I put the opening three ticks then some code then below more ticks

some code

The bottom line is that whatever is returned from the snippet replaces the snippet tag on the page. If its nothing but HTML tags, though, you may not see it.

Feel free to post the code of your snippet (highlight it and click on the </> icon).

1 Like