MODX Community

I need advice from Dr. Snippet

Where would be a good place to get a checkup for my custom snippet? It works, but I am not sure if it works well. I tried to make it fairly generic so other might benefit from it.

1 Like

Here is how I am using it.

Contents of resource "plant-catalog-src.xml"

<!--?xml version="1.0"?-->
<CATALOG>
[[!XMLooper?
	&source=`https://www.w3schools.com/xml/plant_catalog.xml`
	&tpl=`tpl.XML.PLANTS.ROW`
	&nodeName=`PLANT`
	&xpath=`//*[LIGHT="Shade"]`
	&group=`SHADE`
]]
[[!XMLooper?
	&source=`https://www.w3schools.com/xml/plant_catalog.xml`
	&tpl=`tpl.XML.PLANTS.ROW`
	&nodeName=`PLANT`
	&xpath=`//*[LIGHT="Mostly Shady"]`
	&group=`MOSTLY SHADY`
&idxOffset=`[[!+idx]]`
]]
[[!XMLooper?
	&source=`https://www.w3schools.com/xml/plant_catalog.xml`
	&tpl=`tpl.XML.PLANTS.ROW`
	&nodeName=`PLANT`
	&xpath=`//*[LIGHT="Sun or Shade"]`
	&group=`SUN OR SHADE`
&idxOffset=`[[!+idx]]`
]]
[[!XMLooper?
	&source=`https://www.w3schools.com/xml/plant_catalog.xml`
	&tpl=`tpl.XML.PLANTS.ROW`
	&nodeName=`PLANT`
	&xpath=`//*[LIGHT="Sunny"]`
	&group=`SUNNY`
&idxOffset=`[[!+idx]]`
]]
</CATALOG>

Content of chunk "tpl.XML.PLANT.ROW"

    <PLANT POS="[[+idx:default=`0`]]" ZONE="[[+ZONE]]" LIGHT="[[+GROUP]]">
	    <COMMON>[[+COMMON]]</COMMON>
	    <BOTANICAL>[[+BOTANICAL]]</BOTANICAL>
	    <PRICE>[[+PRICE]]</PRICE>
	    <AVAILABILITY>[[+AVAILABILITY:fmt.Text2Date:date=`%B %Y`]]</AVAILABILITY>
		<LIGHT-ICON>[[+GROUP:select=`SHADE=cloud&MOSTLY SHADY=cloudy&SUN OR SHADE=cloudy&SUNNY=sun`]]</LIGHT-ICON>
    </PLANT>

Which results in…

<!--?xml version="1.0"?-->
<CATALOG>
    <PLANT POS="0" ZONE="4" LIGHT="SHADE">
	    <COMMON>Violet, Dog-Tooth</COMMON>
	    <BOTANICAL>Erythronium americanum</BOTANICAL>
	    <PRICE>$9.04</PRICE>
	    <AVAILABILITY>February 1999</AVAILABILITY>
		<LIGHT-ICON>cloud</LIGHT-ICON>
    </PLANT>
    <PLANT POS="1" ZONE="4" LIGHT="SHADE">
	    <COMMON>Trout Lily</COMMON>
	    <BOTANICAL>Erythronium americanum</BOTANICAL>
	    <PRICE>$6.94</PRICE>
	    <AVAILABILITY>March 1999</AVAILABILITY>
		<LIGHT-ICON>cloud</LIGHT-ICON>
    </PLANT>
    <PLANT POS="2" ZONE="4" LIGHT="SHADE">
	    <COMMON>Adder's-Tongue</COMMON>
	    <BOTANICAL>Erythronium americanum</BOTANICAL>
	    <PRICE>$9.58</PRICE>
	    <AVAILABILITY>April 1999</AVAILABILITY>
		<LIGHT-ICON>cloud</LIGHT-ICON>
    </PLANT>
    <PLANT POS="3" ZONE="4" LIGHT="SHADE">
	    <COMMON>Bee Balm</COMMON>
	    <BOTANICAL>Monarda didyma</BOTANICAL>
	    <PRICE>$4.59</PRICE>
	    <AVAILABILITY>May 1999</AVAILABILITY>
		<LIGHT-ICON>cloud</LIGHT-ICON>
    </PLANT>
    <PLANT POS="4" ZONE="4" LIGHT="SHADE">
	    <COMMON>Bergamot</COMMON>
	    <BOTANICAL>Monarda didyma</BOTANICAL>
	    <PRICE>$7.16</PRICE>
	    <AVAILABILITY>April 1999</AVAILABILITY>
		<LIGHT-ICON>cloud</LIGHT-ICON>
    </PLANT>
    <PLANT POS="5" ZONE="4" LIGHT="SHADE">
	    <COMMON>Buttercup</COMMON>
	    <BOTANICAL>Ranunculus</BOTANICAL>
	    <PRICE>$2.57</PRICE>
	    <AVAILABILITY>June 1999</AVAILABILITY>
		<LIGHT-ICON>cloud</LIGHT-ICON>
    </PLANT>
    <PLANT POS="6" ZONE="4" LIGHT="SHADE">
	    <COMMON>Crowfoot</COMMON>
	    <BOTANICAL>Ranunculus</BOTANICAL>
	    <PRICE>$9.34</PRICE>
	    <AVAILABILITY>April 1999</AVAILABILITY>
		<LIGHT-ICON>cloud</LIGHT-ICON>
    </PLANT>
    <PLANT POS="7" ZONE="Annual" LIGHT="SHADE">
	    <COMMON>Cinquefoil</COMMON>
	    <BOTANICAL>Potentilla</BOTANICAL>
	    <PRICE>$7.06</PRICE>
	    <AVAILABILITY>May 1999</AVAILABILITY>
		<LIGHT-ICON>cloud</LIGHT-ICON>
    </PLANT>
    <PLANT POS="8" ZONE="Annual" LIGHT="SHADE">
	    <COMMON>Jacob's Ladder</COMMON>
	    <BOTANICAL>Polemonium caeruleum</BOTANICAL>
	    <PRICE>$9.26</PRICE>
	    <AVAILABILITY>February 1999</AVAILABILITY>
		<LIGHT-ICON>cloud</LIGHT-ICON>
    </PLANT>
    <PLANT POS="9" ZONE="Annual" LIGHT="SHADE">
	    <COMMON>Greek Valerian</COMMON>
	    <BOTANICAL>Polemonium caeruleum</BOTANICAL>
	    <PRICE>$4.36</PRICE>
	    <AVAILABILITY>July 1999</AVAILABILITY>
		<LIGHT-ICON>cloud</LIGHT-ICON>
    </PLANT>
    <PLANT POS="10" ZONE="Annual" LIGHT="SHADE">
	    <COMMON>Snakeroot</COMMON>
	    <BOTANICAL>Cimicifuga</BOTANICAL>
	    <PRICE>$5.63</PRICE>
	    <AVAILABILITY>July 1999</AVAILABILITY>
		<LIGHT-ICON>cloud</LIGHT-ICON>
    </PLANT>
    <PLANT POS="11" ZONE="2" LIGHT="SHADE">
	    <COMMON>Cardinal Flower</COMMON>
	    <BOTANICAL>Lobelia cardinalis</BOTANICAL>
	    <PRICE>$3.02</PRICE>
	    <AVAILABILITY>February 1999</AVAILABILITY>
		<LIGHT-ICON>cloud</LIGHT-ICON>
    </PLANT>

    <PLANT POS="12" ZONE="4" LIGHT="MOSTLY SHADY">
	    <COMMON>Bloodroot</COMMON>
	    <BOTANICAL>Sanguinaria canadensis</BOTANICAL>
	    <PRICE>$2.44</PRICE>
	    <AVAILABILITY>March 1999</AVAILABILITY>
		<LIGHT-ICON>cloudy</LIGHT-ICON>
    </PLANT>
    <PLANT POS="13" ZONE="3" LIGHT="MOSTLY SHADY">
	    <COMMON>Columbine</COMMON>
	    <BOTANICAL>Aquilegia canadensis</BOTANICAL>
	    <PRICE>$9.37</PRICE>
	    <AVAILABILITY>March 1999</AVAILABILITY>
		<LIGHT-ICON>cloudy</LIGHT-ICON>
    </PLANT>
    <PLANT POS="14" ZONE="4" LIGHT="MOSTLY SHADY">
	    <COMMON>Cowslip</COMMON>
	    <BOTANICAL>Caltha palustris</BOTANICAL>
	    <PRICE>$9.90</PRICE>
	    <AVAILABILITY>March 1999</AVAILABILITY>
		<LIGHT-ICON>cloudy</LIGHT-ICON>
    </PLANT>
    <PLANT POS="15" ZONE="3" LIGHT="MOSTLY SHADY">
	    <COMMON>Dutchman's-Breeches</COMMON>
	    <BOTANICAL>Dicentra cucullaria</BOTANICAL>
	    <PRICE>$6.44</PRICE>
	    <AVAILABILITY>January 1999</AVAILABILITY>
		<LIGHT-ICON>cloudy</LIGHT-ICON>
    </PLANT>
    <PLANT POS="16" ZONE="3" LIGHT="MOSTLY SHADY">
	    <COMMON>Ginger, Wild</COMMON>
	    <BOTANICAL>Asarum canadense</BOTANICAL>
	    <PRICE>$9.03</PRICE>
	    <AVAILABILITY>April 1999</AVAILABILITY>
		<LIGHT-ICON>cloudy</LIGHT-ICON>
    </PLANT>
    <PLANT POS="17" ZONE="4" LIGHT="MOSTLY SHADY">
	    <COMMON>Hepatica</COMMON>
	    <BOTANICAL>Hepatica americana</BOTANICAL>
	    <PRICE>$4.45</PRICE>
	    <AVAILABILITY>January 1999</AVAILABILITY>
		<LIGHT-ICON>cloudy</LIGHT-ICON>
    </PLANT>
    <PLANT POS="18" ZONE="4" LIGHT="MOSTLY SHADY">
	    <COMMON>Liverleaf</COMMON>
	    <BOTANICAL>Hepatica americana</BOTANICAL>
	    <PRICE>$3.99</PRICE>
	    <AVAILABILITY>January 1999</AVAILABILITY>
		<LIGHT-ICON>cloudy</LIGHT-ICON>
    </PLANT>
    <PLANT POS="19" ZONE="4" LIGHT="MOSTLY SHADY">
	    <COMMON>Jack-In-The-Pulpit</COMMON>
	    <BOTANICAL>Arisaema triphyllum</BOTANICAL>
	    <PRICE>$3.23</PRICE>
	    <AVAILABILITY>February 1999</AVAILABILITY>
		<LIGHT-ICON>cloudy</LIGHT-ICON>
    </PLANT>
    <PLANT POS="20" ZONE="3" LIGHT="MOSTLY SHADY">
	    <COMMON>Mayapple</COMMON>
	    <BOTANICAL>Podophyllum peltatum</BOTANICAL>
	    <PRICE>$2.98</PRICE>
	    <AVAILABILITY>June 1999</AVAILABILITY>
		<LIGHT-ICON>cloudy</LIGHT-ICON>
    </PLANT>
    <PLANT POS="21" ZONE="7" LIGHT="MOSTLY SHADY">
	    <COMMON>Spring-Beauty</COMMON>
	    <BOTANICAL>Claytonia Virginica</BOTANICAL>
	    <PRICE>$6.59</PRICE>
	    <AVAILABILITY>February 1999</AVAILABILITY>
		<LIGHT-ICON>cloudy</LIGHT-ICON>
    </PLANT>
    <PLANT POS="22" ZONE="6" LIGHT="MOSTLY SHADY">
	    <COMMON>Anemone</COMMON>
	    <BOTANICAL>Anemone blanda</BOTANICAL>
	    <PRICE>$8.86</PRICE>
	    <AVAILABILITY>December 1998</AVAILABILITY>
		<LIGHT-ICON>cloudy</LIGHT-ICON>
    </PLANT>
    <PLANT POS="23" ZONE="6" LIGHT="MOSTLY SHADY">
	    <COMMON>Grecian Windflower</COMMON>
	    <BOTANICAL>Anemone blanda</BOTANICAL>
	    <PRICE>$9.16</PRICE>
	    <AVAILABILITY>July 1999</AVAILABILITY>
		<LIGHT-ICON>cloudy</LIGHT-ICON>
    </PLANT>
    <PLANT POS="24" ZONE="Annual" LIGHT="MOSTLY SHADY">
	    <COMMON>Shooting Star</COMMON>
	    <BOTANICAL>Dodecatheon</BOTANICAL>
	    <PRICE>$8.60</PRICE>
	    <AVAILABILITY>May 1999</AVAILABILITY>
		<LIGHT-ICON>cloudy</LIGHT-ICON>
    </PLANT>

    <PLANT POS="25" ZONE="3" LIGHT="SUN OR SHADE">
	    <COMMON>Phlox, Woodland</COMMON>
	    <BOTANICAL>Phlox divaricata</BOTANICAL>
	    <PRICE>$2.80</PRICE>
	    <AVAILABILITY>January 1999</AVAILABILITY>
		<LIGHT-ICON>cloudy</LIGHT-ICON>
    </PLANT>
    <PLANT POS="26" ZONE="3" LIGHT="SUN OR SHADE">
	    <COMMON>Phlox, Blue</COMMON>
	    <BOTANICAL>Phlox divaricata</BOTANICAL>
	    <PRICE>$5.59</PRICE>
	    <AVAILABILITY>February 1999</AVAILABILITY>
		<LIGHT-ICON>cloudy</LIGHT-ICON>
    </PLANT>
    <PLANT POS="27" ZONE="5" LIGHT="SUN OR SHADE">
	    <COMMON>Trillium</COMMON>
	    <BOTANICAL>Trillium grandiflorum</BOTANICAL>
	    <PRICE>$3.90</PRICE>
	    <AVAILABILITY>April 1999</AVAILABILITY>
		<LIGHT-ICON>cloudy</LIGHT-ICON>
    </PLANT>
    <PLANT POS="28" ZONE="5" LIGHT="SUN OR SHADE">
	    <COMMON>Wake Robin</COMMON>
	    <BOTANICAL>Trillium grandiflorum</BOTANICAL>
	    <PRICE>$3.20</PRICE>
	    <AVAILABILITY>February 1999</AVAILABILITY>
		<LIGHT-ICON>cloudy</LIGHT-ICON>
    </PLANT>
    <PLANT POS="29" ZONE="4" LIGHT="SUN OR SHADE">
	    <COMMON>Gentian</COMMON>
	    <BOTANICAL>Gentiana</BOTANICAL>
	    <PRICE>$7.81</PRICE>
	    <AVAILABILITY>May 1999</AVAILABILITY>
		<LIGHT-ICON>cloudy</LIGHT-ICON>
    </PLANT>
    <PLANT POS="30" ZONE="4" LIGHT="SUN OR SHADE">
	    <COMMON>Blue Gentian</COMMON>
	    <BOTANICAL>Gentiana</BOTANICAL>
	    <PRICE>$8.56</PRICE>
	    <AVAILABILITY>May 1999</AVAILABILITY>
		<LIGHT-ICON>cloudy</LIGHT-ICON>
    </PLANT>

    <PLANT POS="31" ZONE="Annual" LIGHT="SUNNY">
	    <COMMON>Black-Eyed Susan</COMMON>
	    <BOTANICAL>Rudbeckia hirta</BOTANICAL>
	    <PRICE>$9.80</PRICE>
	    <AVAILABILITY>June 1999</AVAILABILITY>
		<LIGHT-ICON>sun</LIGHT-ICON>
    </PLANT>
    <PLANT POS="32" ZONE="Annual" LIGHT="SUNNY">
	    <COMMON>Butterfly Weed</COMMON>
	    <BOTANICAL>Asclepias tuberosa</BOTANICAL>
	    <PRICE>$2.78</PRICE>
	    <AVAILABILITY>June 1999</AVAILABILITY>
		<LIGHT-ICON>sun</LIGHT-ICON>
    </PLANT>
    <PLANT POS="33" ZONE="3 - 5" LIGHT="SUNNY">
	    <COMMON>Primrose</COMMON>
	    <BOTANICAL>Oenothera</BOTANICAL>
	    <PRICE>$6.56</PRICE>
	    <AVAILABILITY>January 1999</AVAILABILITY>
		<LIGHT-ICON>sun</LIGHT-ICON>
    </PLANT>

</CATALOG>

So are there any problems with calling the snippet over an over again? Then I go one step further. I now used my modified xml using the same snippet.

Content of resource "plant-catalog.html"

<h1>Plant Catalog</h1>
<h2 class="p-3 my-4 bg-light">All</h2>
[[!XMLooper? &source=`22` &tpl=`tpl.plantRow` &nodeName=`PLANT`]]

if (strlen($sourcePath)<5) {

Why the check for the length? Seems better to check is_numeric($sourcePath) if you want to see if you got a resource ID or a path.

In the foreach line 64-86 you’re using $modx->setPlaceholder repeatedly and $modx->getChunk to load chunks. Instead of $modx->setPlaceholder, I’d recommend collection values into an array and passing that as the second parameter to getChunk. That way, your placeholders are scoped to that specific chunk only and don’t “leak”.

That’s all I found in a quick 3-minute read through of the code.

2 Likes