revolution icon indicating copy to clipboard operation
revolution copied to clipboard

Enhance debugging of chunks

Open pepebe opened this issue 6 years ago • 19 comments

Summary

Knowing what placeholders are available inside a chunk is more or less a gamble. You can read the documentation or study the code. Both methods are not very reliable and can be quite time-consuming.

I propose to add a generic placeholder to getChunk ([[+chunk.properties]]). It will contain all properties given to getChunk and can be output inside the chunk:

pdoMenu / wayfinder chunk:

<!-- [[+chunk.properties]] -->
<li>[[+menutitle]]<li>

Solution

Add a new placeholder to getChunk: #1872 - https://github.com/modxcms/revolution/blob/4beca05219ea5e3b4011ff54268dc54e8623163a/core/model/modx/modx.class.php

    public function getChunk($chunkName, array $properties= array ()) {
        // add debug placeholder here
       $properties['chunk']['properties'] = print_r($properties,true);
        //
        $output= '';
        if ($this->getParser()) {
            $chunk= $this->parser->getElement('modChunk', $chunkName);
            if ($chunk instanceof modChunk) {
                $chunk->setCacheable(false);
                $output= $chunk->process($properties);
            }
        }
        return $output;

Questions:

  • I'm not sure about the placeholder name. Do you have a better one?
  • Can this possibly break something?

Regards, pepebe

Edit: I guess this should also be added to parseChunk()

pepebe avatar Nov 28 '18 12:11 pepebe

Please also add chunk.name because it can take a hell of a time to find out which chunk created an output. Being able to put the name of a chunk into a comment line would help a lot – especially when a chunk gets renamed or is being duplicated.

mindeffects avatar Nov 28 '18 13:11 mindeffects

Nice idea, but this placeholder generation should only be enabled with a system setting.

Jako avatar Nov 28 '18 13:11 Jako

@Jako Opt-in is always a good thing!

@mindeffects And chunk.id would also be a nice thing. I have written an issue about this: https://github.com/modxcms/revolution/issues/12510

pepebe avatar Nov 28 '18 13:11 pepebe

And got a +10 from me! ;-)

mindeffects avatar Nov 28 '18 15:11 mindeffects

@mindeffects Zeros are cheap, but surprisingly effective ;)

pepebe avatar Nov 28 '18 16:11 pepebe

I'd use placeholders instead of properties for the naming - just to keep things accurate - but I can't really argue against the idea.

Though I do wonder.. how come @mindeffects get to have 10 votes?! [/random]

Mark-H avatar Nov 28 '18 21:11 Mark-H

Mayby, if you think „binary“, it is not so much anymore. 😜

mindeffects avatar Nov 28 '18 21:11 mindeffects

@Mark-H Good point!

Picking up on something Mark mentioned in the other thread ( https://github.com/modxcms/revolution/issues/12510 ), a generic prefix could also be a good idea:

[[+_self.placeholders]], [[+_self.id]], [[+_self.name]]

pepebe avatar Nov 29 '18 13:11 pepebe

I suggest that [[+_self.placeholders]] should contain the names of the passed properties only since the values can be called the "regular" way.

JoshuaLuckers avatar Nov 30 '18 20:11 JoshuaLuckers

Excellent idea, but I don't like variant with an underscore. [[+self.placeholders]] looks more readable.

alroniks avatar Nov 30 '18 22:11 alroniks

With underscore there is a lower risk of overwriting a property set by a user/developer.

JoshuaLuckers avatar Nov 30 '18 22:11 JoshuaLuckers

It doesn't matter because I can use custom placeholder with _ too. Better to log a warning when such placeholder used but keep these things simple. Any special symbol increases the complexity of MODX because you need to remember about it.

alroniks avatar Nov 30 '18 22:11 alroniks

Then I suggest naming it this. People who work with objects are already familiar with $this.

JoshuaLuckers avatar Nov 30 '18 22:11 JoshuaLuckers

My first idea was the same, too: Why use „self“ and not „this“? Let‘s use „this“!

mindeffects avatar Dec 01 '18 17:12 mindeffects

Yes, [[+this.placeholders]] is much more consistent with our experience and expectations.

I'll write the necessary changes and try my luck with a PR.

pepebe avatar Dec 03 '18 17:12 pepebe

I wonder how we could "protect" "this." from being used by other extensions. Is anyone aware of such a placeholder?

pepebe avatar Dec 03 '18 17:12 pepebe

I wonder how we could "protect" "this." from being used by other extensions. Is anyone aware of such a placeholder?

Log warn or error into the log.

alroniks avatar Dec 03 '18 22:12 alroniks

Nice idea. But I would do it through a new modifier "print" to output arrays. Sometimes it can be useful for snippet's output when it returns an array.

[[+this:print]]   // It prints an array of the modChunk object (the placeholder "this" is obtained from the modChunk::toArray() method).
[[+this.placeholders:print]]

sergant210 avatar Mar 19 '21 07:03 sergant210

It would be great if this would be a thing in 3.1.

patrickatwsrn avatar May 12 '22 16:05 patrickatwsrn