We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7231
    • 4,205 Posts
    I am generating a list with a snippet and I want the code to be generated one item per line in the source code. Currently I have:
    <li>object1</li><li>object2</li><li>object3</li><li>object4</li>

    But what I want is:
    <li>object1</li>
    <li>object2</li>
    <li>object3</li>
    <li>object4</li>

    Based on what I read online I should be able to add a \n or a \r to get a newline. But when I ad it to my code it prints the \n rather than the newline. What am I doing wrong?

    Here is the output string, I have tried all sorts of ways of adding the \n to the string but it did not work:
    $output .= '<li>'.$y.'</li>';
      [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]
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You do realize that having the newline will cause IE to add unexpected spacing to the rows...
        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
        • 30223
        • 1,010 Posts
          • 7231
          • 4,205 Posts
          You do realize that having the newline will cause IE to add unexpected spacing to the rows...
          I know, but I need to have the same output as wayfinder which uses the newlines or else the spacing changes between pages. I know that wayfinder can turn off the newlines but all the spacing and alignment is done already and I would need to change several styles, so I thought it would be easier to just add the newline.

          Besides that... have a read through the php manual
          Already been there, thanks. The only thing that I was able to maybe understand is that I should have double quotes rather than single quotes. Is this what I am missing? But the MODx snippet guidelines tell us not to use double quotes. I will give this a try after lunch. Thanks.

          If the string is enclosed in double-quotes ("), PHP understands more escape sequences for special characters:

          So I guess it would be like this:
          $output .= "<li>$y</li>\n";
            [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]
            • 22303 MODX Staff
            • 10,725 Posts
            Quote from: dev_cw at Nov 19, 2007, 08:27 AM

            But the MODx snippet guidelines tell us not to use double quotes.
            Where does it say this? That’s simply inaccurate, or at least misleading. There are many who continue to try and argue that using single-quotes is faster than using double-quotes in PHP, but this is simply not true in most cases, at least not in any reasonably recent version of PHP (4.4+, 5.1+). I’ve tried calculating performance differences between simple and complex strings using both single- and double-quotes and cannot find any consistent differences in the results of the two methods.
              • 33372
              • 1,611 Posts
              There is some debate over whether your PHP code will be processed faster if you use single quotes instead of double quotes when you don’t need to parse any variables or other special code within them. I always use single quotes whenever I don’t need double quotes, but it’s more force of habit than anything else. If you’re going to add a newline character ("\n") then you must use double quotes or it will just print out literally (as you saw).

              You can also use single quotes and just add the new line within them, like so:
              $output .= '<li>'.$y.'</li>
              ';


              But I can’t imagine that this would be more efficient than just using double quotes given how short this string is anyway, and that would make your code a little bit more tidy.
                "Things are not what they appear to be; nor are they otherwise." - Buddha

                "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

                Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
                • 7231
                • 4,205 Posts
                It is in the Creating Snippets wiki post, may not be an ’official’ guideline but it seems like one to us folks who are trying to follow ’rules’ and don’t know enough to know better.
                http://wiki.modxcms.com/index.php/Creating_Snippets#Use_single_quotes_instead_of_double_quotes
                Use single quotes instead of double quotes
                When you use single quotes, your text is parsed faster by php core.

                Double quotes it is, thanks folks, this has been educating as usual.
                  [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]
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  The only reason I use single-quotes is to save myself the trouble of escaping (and the bigger trouble of forgetting to escape) the double-quote in HTML tags if they are needed:
                  $header = '<h1 class="BigHeader">Header</h1>';
                  $link = '<a href="http://url.com">Link</a>';
                    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
                    Double quotes did the trick great. Thanks

                    I find myself torn between double and single quotes, on one hand no escaping html parameters, but on the other we can embed variables without concatenation.
                      [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]
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      Not criticizing your thinking, it all depends on what kind of coding you mostly do. I usually find that most of my variables are complex variables, usually from database query results, and have to be concatenated anyway.
                        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