We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 52075
    • 9 Posts
    I've been a client-side web developer for a while now but having trouble wrapping my head around the "MODX way of doing things".


    Here's the situation. I imagine it's simple enough. See the attached.

    I want to have something like an Articles Container, but possibly more lightweight, where the template variables (TVs) are


    • title of article
    • URI/link to article
    • logo of publication
    • date of publication

    The script/template/snippet (?) should run through all of these entries and generate a page like the attached. It should also be paginated (say, 10 limit per page).

    I used to be the guy answering a million forums threads for folks with complex JavaScript problems. I'd GREATLY appreciate if someone could help me out here and give me a quick walkthrough or a link to some resources. I looked at the documentation and somehow I'm just having trouble putting the pieces together.


    Also, I'm on MODX Revolution 2.5.0-pl.


    Thanks a ton,
    Fred


    [ed. note: fvaughn last edited this post 10 years, 1 month ago.]
      • 3749
      • 24,544 Posts
      It sounds like a job for getResources (or its faster incarnation, pdoResources).

      You tell getResources to use your Tpl for each entry:

      [[getResources? 
          &tpl=`MyTpl`
      ]]


      Your Tpl chunk is a template for each item displayed. It usually contains HTML code interspersed with MODX tags. You use placeholder tags for any resource fields or TVs (they will be replaced with the field or TV values for that resource):

      [[+pagetitle]] [[+logo]]  


      (link):
      <a href="[[~[[+id]] ]]">Link Text</a>
      

      The last one needs a little explanation. Here's an example of a link tag:

      [[~12]] 


      That returns the URL of the resource with the ID 12. So the tag above asks MODX to make a URL based on the ID of the resource currently being processed by getResources: [[~[[+id]] ]]. IOW, it's a placeholder tag inside a link tag.

      Once you have your getResources tag working, you can add pagination by replacing the getResources tag with a call to the getPage snippet with getResources as the "element". Any properties you include in the tag (e.g., &tpl) will be passed through to getResources.

      Welcome to MODX. smiley

        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 52075
        • 9 Posts
        Thanks a lot, BobRay. That's helpful. I do have both getResources and pdoResources at my disposal already installed. However, a few questions:


        • In terms of "myTpl", how would that look / how would I build that to accommodate the information that I've laid out?
        • Forgetting pagination for a second - I'll add that later - what would the actual code look like on the page displaying all of this? I'm imagining "Article" type items and then the code that I'm looking for would traverse all of these items/Article objects to generate something like that I attached in my original post.

        I understand you're the 'guru' on this board and I do truly appreciate your time.
          • 52075
          • 9 Posts
          Put another way, here's the fundamental issue. I'm having trouble understanding how the template actually "parses" and runs through all of the entries to generate a page. As in, what's the missing link that I'm not grasping here? For example, see https://web.archive.org/web/20140625134056/http://netcracker.com/en/news/media_coverage/media_coverage_2014/ (copy and paste)


          How would I build a template such that it each article entry has the corresponding TVs. See the attached for a very rough sketch.

          So I fundamentally 'get' that you need to build a template which allows for all of these variables, but then, how do you build the client-side/front-end part that runs through all of the entires and actually generates the page? If you could explain that, I'd be all set.


          Thanks a ton!

            • 3749
            • 24,544 Posts
            Basically, getResources (or pdoResources), gets each resource, then creates an associative array of the key/value pairs for all the resource fields (pagetitle, longtitle, menutitle, publishedon, content, etc., then adds all the resources TV's to that array with the name of the TV as the key and it's value for the currently plucked resource as the value).

            Then, it calls $modx->getChunk('YourTplChunk', $fields), where $fields is the associative array. getChunk() returns the Tpl chunk with all the placeholders replaced with the values from the associative array.

            As each resource is processed, getResources appends the return from getChunk() to the output.

            When the loop is finished, it returns that output.

            The Tpl chunk is nothing more than the HTML you want for each item to be displayed with the placeholders embedded in the HTML.

            The easiest way to understand it is to just do this to start with (replace 12 with some appropriate parent resource ID that has some children):

            <ul>
            [[!getResources? 
                &parents=`12` 
                &tpl=`MyTpl`
                &includeTVs=`1`
                &tvPrefix=``
            ]]
            </ul>
            


            Then create a chunk called MyTpl with this content:

            <li>[[+pagetitle]]</li> 


            Once that works to list the pagetitles, extend it like this:

            <li>[[+pagetitle]]  <a href="[[~[[+id]] ]]">See more ... </a></li>


            Just keep editing the Tpl chunk until the results look like you want them to. Tags for the TVs look just like the ones for the resource fields -- but with the name of the TV in the tag.

            I hope that makes sense.

            Here's a full list of the resource fields.

            The date fields are a little tricky, but let's save that for later.






              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 10208 ☆ A M B ☆
              • 1,780 Posts
              One thing that always confused me at first was the difference between using TVs with snippet tpls (such as getResources) vs straight up in the template or resource i.e. [[*yourTV]]. If you're using a chunk as a tpl with a snippet, the TV must be a placeholder i.e. [[+yourTV]]. [[*yourTV]] won't work in the tpl for the snippet.
                Frogabog- MODX Websites in Portland Oregon
                "Do yourself a favor and get a copy of "MODX - The Official Guide" by Bob Ray. Read it.
                Having server issues? These guys have MODX Hosting perfected - SkyToaster
                • 52075
                • 9 Posts
                Quote from: BobRay at Sep 01, 2016, 03:35 PM

                The easiest way to understand it is to just do this to start with (replace 12 with some appropriate parent resource ID that has some children):

                <ul>
                [[!getResources? 
                    &parents=`12` 
                    &tpl=`MyTpl`
                    &includeTVs=`1`
                    &tvPrefix=``
                ]]
                </ul>
                


                Then create a chunk called MyTpl with this content:

                <li>[[+pagetitle]]</li> 


                Once that works to list the pagetitles, extend it like this:






                Hi BobRay,

                So regarding the above:


                1. What do you mean a "parent resource" - I've created an Articles Container and added a few items to it.

                1a. I added the getResources bit into a new template. Is that the correct place?

                2. When I execute the code above targeting my Articles Container resource ID as the "parents" variable, I get a blank result. Please advise?
                  • 3749
                  • 24,544 Posts
                  By "parent resource" I just meant any resource that has children under it in the tree. Be sure to change the 12 in my example to the ID of that parent.

                  It should work with the Articles container, but I've never tried it. Make sure the resources are published and double-check the exact spelling of the Tpl chunk's name versus the &tpl property in the getResources snippet tag.

                  The tag can go in a template or in the Resource Content field of a particular resource. It can also go in a chunk, but let's save that for later.

                  Also, be sure that the property values in the snippet tag are enclosed in back-ticks, not single quotes.

                  If it doesn't work with the Articles container, try using the ID of another parent resource.
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 52075
                    • 9 Posts
                    OK - so you could just have for example a folder with Document items under it. I see. I need to experiment more and read the documentation (https://rtfm.modx.com/extras/revo/getresources). Thanks again.

                    It does seem to work with just using a folder (?) node as the 'parents' variable. However, could you let me know if you figure out how to use an Articles container? I'm just struggling to figure out how people dynamically read in Article titles, etc., to make pages dynamic. Here's the particular page I am working on: https://www.netcracker.com/company/about-netcracker/in-the-news.html

                    It's currently largely static (the news article info - title/date/description). Ideally those would all be pulled from MODX (and I guess, ideally an articles container).


                    Any further feedback appreciated although I know I'm asking a lot here.
                      • 3749
                      • 24,544 Posts
                      Articles is weird, and hard to explain.

                      Start with the instructions here: https://rtfm.modx.com/extras/revo/articles/articles.creating-a-blog.

                      Once that's sort of working, you'll want to duplicate the sample Articles template, and the Articles Container template (Elements tab: Templates -> Articles). You'll also want to duplicate the Articles Row Tpl chunk (Elements tab: Chunks -> Articles). As you create them, rename them by replacing "Sample" with "My".

                      You duplicate them, rather then just editing them, so your changes won't be overwritten if you upgrade Articles.

                      Then, edit your main articles blog resource (just click on it in the tree). On the "Templates" tab, change the three "sample" entries (two templates and the chunk) to point to your duplicates.

                      Finally, you modify those three duplicates to meet your needs. The Tpl chunk is where you'll modify things to show look like the page in your link above. You'll want to create a Template Variable (TV) for the image, but I'd save that until everything else is working.

                      I hope this makes sense.

                      [update] I should mention that many people use Collections in preference to Articles, though I have no experience with it. I think it's probably much more straightforward in how it works.


                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting