Understanding the parse order for placeholder tag in a chunk

Environment
MODX Revolution 2.8.5
pdoTools 2.13.3 - this is relevant as the MODX parser is replaced by pdoParser

My Snippet [[get_superhero]] uses a MIGXdb schema to query a custom database. It creates an array of output properties $arr_output_props and calls getChunk which contains relevant placeholder tags. Ultimately it runs phpThumbOf to create a thumbnail image and while the image is generated the the following error is logged

PHP warning: is_readable(): open_basedir restriction in effect. File(/assets/media/[[+img_src]]) is not within the allowed path(s)

[pThumb] Resource: 1 || Image: (none)
File not found: /home/server/webapps/dev/httpdocs/assets/media/[[+img_src]]

At the point of error the placeholder `[[+img_src]] has not been replaced with the actual image file path. I suspect this is caused by the parse order and my nested snippet calls.

However, as noted above, the image is generated despite the error, hence the process appears to run twice.

The relevant code from [[get_superhero]] is

$arr_output_props['img_src']   = $obj_superhero->get('img_src');

// getChunk executes the parser (currently pdoParser) so the $tpl chunk can use output filters
$output .= $modx->getChunk($tpl, $arr_output_props);
return $output;

The relevant code from the $tpl Chunk is

[[generate_grid_picture?
    &src=`[[+img_src]]`

Here you can see the [[+img_src]] is used by another snippet [[generate_grid_picture]]. To debug I wrote the value of $src to screen and it is correct.

The [[generate_grid_picture]] Snippet is 400 lines and not well formatted so I’m not including it at the moment as I’m unsure it’s necessary.

I see @markh noted the following which seems relevant

Placeholders that are not yet available in a $modx->getChunk call will be returned as-is and rendered later. Depending on where the code runs and where [[++city]] comes from, that may or may not be available.
Stop Custom Snippet from Parsing Placeholders - #7 by markh

I can resolve the issue in two ways but neither is a proper solution

  • update the $tpl Chunk to use the :default output modifier
[[generate_grid_picture?
    &src=`[[+img_src:default=``]]`
  • or call [[!generate_grid_picture? ...]] uncached.

Can anyone suggest the next steps to find the proper solution or if it would help for me to post the full code please?

Thanks

Parsing order issues may be caused by over nesting. They can sometimes be fixed by changing which tags are cached, but moving things “up” one or more levels tends to be a more reliable solution. For example, when you have a chunk tag in your template and that tag contains a tag that in turn contains one or more tags, moving the content of the chunk into the template can sometimes help.

Another method, (what I normally do) is to move the problem logic into a custom snippet. In other words, pass the information you have to the snippet as properties, then in the snippet, get the remaining information yourself and call getChunk() with the necessary placeholder values in an associative array as the second argument to getChunk(). It’s best for the snippet to return the final HTML you want displayed on the page, but if necessary, you can also return a tag, that MODX will try to handle when the page is rendered.

Are you sure that the key img_src always exists in the array $arr_output_props (and is set to some value) when $modx->getChunk(...) is called?

It seems to me that somehow, img_src is missing and therefore the placeholder (in $tpl) can’t immediately be replaced with the actual value.

@bobray and @halftrainedharry - the issue is resolved by changing the [[generate_grid_picture? ]] call in the get_superhero_img.tpl chunk to a placeholder [[+picture]] ie.

        <div class="superhero-card">
            [[+picture]]
        </div> [[- /.super-hero-card]]

And updating the [[get_superhero.php]] snippet to run the generate_grid_picture snippet manually and include the generated html in the output properties array ie.

$arr_output_props['picture'] =  $modx->runSnippet("generate_grid_picture", array(
	'src' => $src,
	'src_is_migx' => '1',

Thanks again for pointing me in the right direction.

Yes, when I dumped the array to screen the img_src key existed with the correct value. The [[img_src]] placeholder in my tpl chunk also showed the correct value once rendered on site, it’s just that at some point through the parsing process the placeholder value wasn’t available and triggered an error in the logs.

I’m unsure how it was ultimately replaced with the correct value allowing the image to be generated. At least this will encourage me not to write more complete snippets and not take the easy way out.

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.