We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29703
    • 217 Posts
    Hello,

    I’ve been working on a snippet for weeks now, but with no understanding of PHP i have failed repeatedly. :’(

    I basically want to be able to enter this into the content editor and for it to call a snippet, which calls a chunk, modifying the variables with the selected parameters:

    [!DownloadBox?&thumbnail=`images/thumbnail.jpg`&downloadurl='downloads/file.zip' &description='This is the description for the download'!]


    I’m a student at the moment and am unsure of pricing, but I’m open to offers. No design needs to be done as it’s all finished.

    In total there needs to be 8 variables: 3 single line simple text entries (no RTE), 3 spaces for image URLS (only two variations of image), a file URL and an image.


    In regards to IP ownership constraints, I would prefer that it was given back to the community (not that I know if it’ll be any use for love or money)


    I plan to use the default filemanager to upload everything so that isn’t a problem.

    Part of the website before being ModX’d can be viewed in its tabled invalid state at www.stemplate.com/icons.html

      • 22303 MODX Staff
      • 10,725 Posts
      For each variable you want to output in the chunk, set a placeholder (see the $modx->setPlaceholder() function) and use that in the chunk, i.e. [+downloadurl+]. If it’s going to repeat on a single page, use $modx->parseChunk() instead as described in several places here in the forums, such as here, and in the wiki here.
        • 7231
        • 4,205 Posts
        I basically want to be able to enter this into the content editor and for it to call a snippet, which calls a chunk, modifying the variables with the selected parameters:

        Just wondering, would this snippet return one entry or all the entries to be downloaded? If this is a 1-to-1 relationship then you do not need a snippet just use the chunk directly with the document variable and TV placeholders.

        I guess that the question is if the download file info is stored in a MODx document or in a custom data table.

        BTW - that is a nice looking site.

        Edit - Taking another look at the site you should be able to accomplish this with a handful of TVs (template variables) and Ditto as long as the info is stored in MODx document structure.
          [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

          Something is happening here, but you don't know what it is.
          Do you, Mr. Jones? - [bob dylan]
          • 10449
          • 956 Posts
          Yes, setPlaceholder() would def. work.

          On the other hand, there’s nothing stopping you to include the HTML directly in the snippet. It’s not recommended, but not forbidden either smiley

          The heredoc syntax might be useful here: http://www.hudzilla.org/phpwiki/index.php?title=Heredoc_syntax


            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Or use a chunk, and fetch it via $modx->getChunk(’chunkname’);
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 29703
              • 217 Posts
              Thanks everyone for the help! I was expecting people to cash in on my ignorance, though clearly not.

              I have a much better understanding of Placeholders etc, though I haven’t a clue how to use:

              $modx->parseChunk()


              I looked at the suggested topics and quite honestly didn’t have a clue. I don’t even really know what parsing is.

              This is the snippet I currently have:

              <?php
              $modx->setPlaceholder(preview, $preview);
              $modx->setPlaceholder(filetype, $filetype);
              $modx->setPlaceholder(scalable, $scalable);
              $modx->setPlaceholder(editabletext, $editabletext);
              $modx->setPlaceholder(resolution, $resolution);
              $modx->setPlaceholder(layered, $layered);
              $modx->setPlaceholder(fonts, $fonts);
              $modx->setPlaceholder(download, $download);
              
              return $modx->getChunk('downloadbox');
              ?>


              This is the chunk I currently have:

              <div id="downloadimage">
              <img src="[(base_url)]assets/files/downloads/[+preview+]" alt="Download Preview">
              </div>
              <div id="downloadcontainer">
                <div id="downloadleft">
                 	<div id="downloadwidecollumn">
                 	  <div id="row">
                          Format
                    </div>
                          <div id="row">
                          Scalable
                          </div>
                          <div id="row">
                          Editable Text
                          </div>
                  </div>       
                      <div id="downloadnarrowcollumn">
                          <div id="row">
                          [+filetype+]
                          </div>
                          <div id="row">
                          <img src="[(base_url)]assets/templates/stemplate/images/[+scalable+].png" alt="scalable">
                          </div>
                          <div id="row">
                          <img src="[(base_url)]assets/templates/stemplate/images/[+editabletext+].png" alt="editable text">            </div>
                      </div>
                      <div id="downloadwidecollumn">
                          <div id="row">
                          Resolution
                          </div>
                          <div id="row">
                          Layered
                          </div>
                          <div id="row">
                          Fonts
                          </div>
                      </div>
                      <div id="downloadnarrowcollumn">
                          <div id="row">[+resolution+]
                          </div>
                          <div id="row">
                          <img src="[(base_url)]assets/templates/stemplate/images/[+layered+].png" alt="layered">
                          </div>
                          <div id="row">[+fonts+]</div>
                      </div>
                </div>
                  <div id="downloadright">
                    <a href="[(base_url)]assets/files/downloads/[+download+]"><img src="[(base_url)]assets/templates/stemplate/images/downloadbutton.png" alt="Download file"></a>    </div>
              </div>


              and this is what I use to call it:
              [[downloadbox? &preview=banners/banner1.jpg&filetype=.PSD&scalable=tick&editabletext=tick&resolution=300x500&layered=tick&fonts=Arial&download=banners/banner1.psd]]


              Now all this works for a single instance, though I clearly need to parse it to create multiple instances. Can anyone help me out?
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                Parsing is going through something one bit at a time looking for specific things. For example, MODx parses the template assigned to the requested document, looking for its tags (such as [*content*]), then does things when it finds those tags.

                If it runs across a placeholder, it replaces it with the value for it from its placeholders[] array.

                The parseChunk function looks through a chunk and replaces the tags in it with the elements from the array you specify.

                I found that printing out the document.parser.inc.php file and using it for evening reading was very useful. It took a few passes, but I finally began to get a good understanding of what what going on under the hood.
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                  • 29703
                  • 217 Posts
                  Ah I see. I’m pleased I now understand a basic aspect.

                  Quote from: sottwell at Nov 26, 2007, 02:11 AM

                  I found that printing out the document.parser.inc.php file and using it for evening reading was very useful. It took a few passes, but I finally began to get a good understanding of what what going on under the hood.


                  Heh, that sounds like a great idea, though I’ll probably print out a PHP documentation beforehand. ’PHP for dummies’ or something.


                  Right, I’ve been completely mentally analysing the API:parseChunk wiki entry applying any reason and logic that my mind can cope with and I’m just not cracking it.

                  API:parseChunk wiki entry: http://wiki.modxcms.com/index.php/API:parseChunk

                  The example shown just seems rather overly complicated for what I want to achieve. I can’t visualise what the example’s final output would be, let alone apply the code to my own situation.

                  If I could just have some help on how to format it, I might be able to figure it out for myself smiley
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    You have a chunk:
                    <h1>My Big Listing</h1>
                    <ul>
                    [makelist]
                    </ul>
                    

                    Now in your snippet you have code to go get a list of stuff from the database. You put the stuff into an array. Call $modx->parseChunk with the name of your chunk and the name of your array, and it will automagically load the chunk with the whole list pulled out of the database. It parses the chunk, finds that [makelist] tag, and replaces it with whatever was in the array.

                    That’s such a simplistic example that it’s really almost useless, but it should help you get the idea.
                      Studying MODX in the desert - http://sottwell.com
                      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                      Join the Slack Community - http://modx.org
                      • 29703
                      • 217 Posts
                      Okay, I’ve looked into PHP arrays and have tried to create a working snippet - but low and behold, it doesn’t work.

                      Am I even nearly on the right track?:

                      $output = array (
                          $preview  => 'preview',
                          $filetype  => 'filetype',
                          $scalable  => 'scalable',
                          $editabletext  => 'editabletext',
                          $resolution  => 'resolution',
                          $layered  => 'layered',
                          $fonts  => 'fonts',
                          $download  => 'download'
                      );
                      
                      $output .= $modx->parseChunk('downloadbox', $array);