We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14933
    • 6 Posts
    Hello All,
    I am trying to create a snippet from scratch. The snippet is supposed to display tip of the day in random order. I didnt start this topic to ask the logic of the module but to see if someone would help me in setting it up according to the modx way.
    The module got three parts

    1) Home Page Module: Here the tip would be randomly displayed. To the end of the tip there will be a link which would be "Click for More " Which takes to the page in point 2.
    2) Tip of the day Listing: This page would display all the tips that is been displayed. This is the page to which the user is taken to when he clicks "Click for More" that is been discussed in point 1
    3) Admin Side: This page is supposed to be displayed in the modx manager. This module enables the admin to add new tips.


    I am not sure how to implement all the above said things in MODX. After reading through the documentation I think for the first step I need to create a snippet. I tried to create a test snippet in order to learn the process but failed. I am not sure what all elements are required to create a snippet. I have following doubts:

    1) Is there any need to set up a database connection to retrieve the data from the table modx_tod. If yes how can I do that?
    2) The template and logic are supposed to be in two pages. How should I set up the variable in logic page that need to be displayed in the template page. After setting up the variable in the logic page how can I show that in the template page?

    Even though the post is bit long I hope the solution to the post is simple. I hope I will get a solution to the said problem soon.
    Thanks
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      I would create a folder document in the Document Tree, "Tips". Set it to not show in the menu. Then each tip would get its own page in that folder. Set their template to "none". Use Ditto on the "Tips" page to show all the tips, then use another Ditto call on the home page to get the random tip with link to the main "tips" page.
        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
        • 7231
        • 4,205 Posts
        I agree with sottwell’s method, put the tips in a container and use ditto to get the results you want.

        Regarding creating snippets, there is a great how to in the wiki that is definitely worth reading:
        http://wiki.modxcms.com/index.php/Creating_Snippets
          [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]
          • 3749
          • 24,544 Posts
          As suggested, Ditto is definitely the way to go.

          You shouldn’t have to write any snippets and, if you want, you can create your individual tips documents using the NewsPublisher snippet.

          Be sure that you check the "container" box on the Page Settings tab when you create your Tips container document so that MODX knows that it will hold other documents. (If you’ve already created it, you can just right click on it in the document tree at the left, select "edit," click on the Page Setting tab on the right, click on the "container" checkbox, and click on Save.

          First, create the tips folder as described but don’t put anything in it.

          Then, create a document called "Add a Tip" with just this call:

          [!NewsPublisher? &folder=`8` &clearcache=`1` &postid=`1`  !]


          Note: change &folder to the document number of your tips container document, change &postid to the document number of the "add a tip page" itself. That way when you’re done entering one tip, you’ll be ready to enter another.

          Going to that document will let you enter a new tip and store it automatically.


          Then, on your home page (or wherever you want to display the random tip), put this line:

          [!Ditto?startID=`8` &display=`1` &randomize=`1`!]


          Note: Change startID to the number of the Tips container document.

          Where you want the tips to display. You can surround it with a <div> if you want to style your tips using .CSS, or you can create a custom &tpl (see the Ditto docs).

          Note that anyone can create a new tip with this setup. You can protect the "Add a tip" page by adding something like &canpost=`wugNewsAdmins` to the parameters of the NewsPublisher snippet call, but then you need to add a web login function and create the appropriate user and document groups. For the time being, you can just unpublish the "Add a tip" document when you’re not using it.

          Hope this helps,

          Bob



            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
            • 3749
            • 24,544 Posts
            Quote from: BobRay at Nov 04, 2007, 03:09 PM

            [!NewsPublisher? &folder=`8` &clearcache=`1` &postid=`1`  !]


            Question: Would either of these work as a generic solution for setting &postid to the current document? undecided

            [!NewsPublisher? &folder=`8` &clearcache=`1` &postid=$modx->documentIdentifier  !]


            [!NewsPublisher? &folder=`8` &clearcache=`1` &postid=[~[id]~]  !]


            Bob
              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
              • 7231
              • 4,205 Posts
              I think this works...
              [!NewsPublisher? &folder=`8` &clearcache=`1` &postid=[*id*]  !]

              This will give the current ID
              [!NewsPublisher? &folder=`[*parent*]` &clearcache=`1` &postid=[*id*]  !]

              This will give the parent ID.
                [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&#39;t know what it is.
                Do you, Mr. Jones? - [bob dylan]
                • 3749
                • 24,544 Posts
                Quote from: dev_cw at Nov 04, 2007, 07:03 PM

                I think this works...
                [!NewsPublisher? &folder=`8` &clearcache=`1` &postid=[*id*]  !]

                This will give the current ID
                [!NewsPublisher? &folder=`[*parent*]` &clearcache=`1` &postid=[*id*]  !]

                This will give the parent ID.

                Thanks. This questions gets asked often enough that I’m thinking of creating a general "how to" page to save a little work in the future.

                I may be too short of sleep, but I think your second example would only work if the "add a new tip" page was a child of the tips folder, in which case, Ditto might eventually pick it as a tip to display.

                Bob
                  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
                  • 14933
                  • 6 Posts
                  Thanks to all those who replied for my post. It was really helpful.
                  dev_cw i had already gone through the create snippet page that you pointed to. Even though it gave me a small idea on how to create a snippet I am still confused on displaying data. I had already posted my doubt in my first post

                  The template and logic are supposed to be in two pages. How should I set up the variable in logic page that need to be displayed in the template page. After setting up the variable in the logic page how can I show that in the template page?
                  I would be grateful if anyone could explain me about it.
                  About the tip of the day, thanks I was able to setup the stuff and never knew that it was so easy. But I have some problems that need to be fixed. I will list it as points below

                  1) I don’t want the users to view the page "Add a Tip" and add their tips. It is for the admin only (or the modx manager). How can I make the page viewable through the modx manager page?
                  2) I dont want all the options in the "Add a tip" page. I only need the "Content" box. Not even the publishing date.
                  3) The tip is supposed to change after every 24 hrs. Tips once displayed should not be repeated again.
                  4) I need a page which will display all the tips that was displayed and need not display tips that is yet to be displayed.
                  I thought of creating a snippet due to the above requirements. I am still not sure if we can customize ditto to such a level. Thanks.
                    • 7231
                    • 4,205 Posts
                    The template and logic are supposed to be in two pages. How should I set up the variable in logic page that need to be displayed in the template page. After setting up the variable in the logic page how can I show that in the template page?
                    You want to use the api functions to set the placeholders, then in your display chunk you place the placeholder where you want the data to show. There is some good info at the setPalceholder api page: http://wiki.modxcms.com/index.php/API:setPlaceholder

                    As for the Q’s:

                    1) make this private. If you do not want to add tips in the front end you don’t need this page, just do it in the manager.

                    2) You can 100% personalize the way ditto dispalys the tips. In ditto you create a chunk and use placeholders to position the document data that you need.

                    3) and 4) - I don’t know about these.
                      [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&#39;t know what it is.
                      Do you, Mr. Jones? - [bob dylan]
                      • 3749
                      • 24,544 Posts
                      You can certainly customize the "add a tip" page, but if it doesn’t bother you, you can just leave most of the fields blank. Those fields will exist in the DB whether you see them on the form or not so you’re not really wasting any space.

                      About your strategy to display the tips. I’m shooting from the hip here, but I think I’d approach it this way. I’m pretty confident that there’s no way to do a truly random display with Ditto out of the box but I could be wrong. If you want the tips to be in truly random order on each pass, you’re code would be much more complicated and error-prone.

                      If you can settle for having the tips displayed in the same order on each pass, it gets much easier. If you create all the tips in sequence so that their document numbers are in sequence, it’s even easier still.

                      Just save three values (in a file or DB table) with the document ID of: 1. the first tip, 2. the current tip, and 3. the last tip.

                      In your snippet,

                      1.get the three values

                      2. Show the current tip (Use Ditto and specify the document ID).

                      3. Increment the current tip#

                      4. If the current tip# > the last tip#, set current tip# to the first tip#.

                      5. save the values.

                      For the display of already shown tips, all you need to do is have a separate snippet that shows them from the first tip to (current tip -1). (Also using Ditto).

                      You can pseudo-randomize the tips by creating a list of tips to show and sorting them on different fields each time then showing them in order as above but it will be a lot more more work.

                      If you want to truly randomize the tips, you’ll have to store an "already shown" variable for each document then figure out some way to select a random tip and re-select if it’s been shown, but the selection will get slower and slower as the unshown tips get fewer and you’ll have to figure out a way to keep it from going into an endless loop when all the tips have been shown.

                      Personally, I’d live with showing them in the same order each time and using the steps above. As long as there are enough tips, the users won’t be able to tell the difference.

                      Bob

                        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