We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4749
    • 623 Posts
    Does anyone know of a way to create a dynamic list. In other words, how can a client create a list on a page with a different number of list items. For example, I have a page split into four sections. Each section is already manageable by the user with TV’s, but they way I have it set up now there is already a specific number of list items, but it can’t be changed.

    Looks like this:
    <ul>
       <li>[+itemone+]</li>
       <li>[+itemtwo+]</li>
       <li>[+itemthree+]</li>
       <li>[+itemfour+]</li>
    </ul>
    


    I would like the client to be able to create a list with any number of list items and not have it be dependent upon a set number of TV’s. Is there a way to do this easily. The client doesn’t know HTML. Thanks a bunch!
      The MODx has you...
      Utah Web Design
      • 4749
      • 623 Posts
      The list is inside a chunk that’s called by ditto. I was hoping that someone might know of a way to dynamically generate a list with a tv for a specific document.
        The MODx has you...
        Utah Web Design
        • 2734
        • 165 Posts
        Hi

        I guess you could accomplish this easily by using a textarea TV and a Snippet
        The user may enter list items in the textarea, one item per line for example.

        To convert this text to a list, you have to programm a simple snippet. To make this reusable you just pass the name of the TV to the snippet.
        Lets assume your TV is named "ListItems" and your Snippet will be named "TextToList", then you create a html list by calling
        [[TextToList?tv=`ListItems`]]
        The snippet will convert the list Items to HTML and ouput it.

        Cheers -- roman

        EDIT:
        Here is a snippet you could use:
        <?php
        /**
         * Convert a text with linebreaks to a html list
         */
        $chunk = isset($chunk) ? $chunk : false;
        $tv = isset($tv) ? $tv : false;
        $delimiter = isset($delimiter) ? $delimiter : "\n";
        
        if($tv){
        	$tvArray = $modx->getTemplateVarOutput(array($tv), $modx->documentObject['id']);
        	if(!$tvArray[$tv])
        		return;
        	
        	$text = $tvArray[$tv];
        } else if($chunk){
        	$text = $modx->getChunk($chunk);
        	if(!$text)
        		return;
        } else {
        	return;
        }
        
        $lines = explode($delimiter, $text);
        $out = '<ul>';
        foreach($lines as $line){
        	$out .= '<li>' . trim($line) . '</li>';
        }
        $out .= '</ul>';
        return $out;
        ?>
        


        Copy it into a snippet called "TextToList", then call it by either specifying a template Variable or a Chunk name
        eg. [[TextToList?tv=`TVNAME`]]
        or: [[TextToList?chunk=`CHUNKNAME`]]
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          You should use
          return $out;

          instead of the echo at the end of the snippet.
            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
            • 4749
            • 623 Posts
            Thank you so much for the responses. Sorry, I’ve have been very sick recently and unable to respond sooner. I have been hoping to find something like this for a while. I will put it to the test and get back to you all. Oh, BTW, how is the list created? Just list items at the linebreak </li> or what? Just wondering how to style it. Thanks.
              The MODx has you...
              Utah Web Design
              • 4749
              • 623 Posts
              OK, just implemented the snippet Roman. It’s not showing up in the source. I’m not getting anything. Any ideas...?
                The MODx has you...
                Utah Web Design
                • 7231
                • 4,205 Posts
                Oh, BTW, how is the list created? Just list items at the linebreak </li> or what?
                Looking at the code the linebreak is a an actual carriage return (???) created by simply hitting the return key on the keyboard (this creates an invisible \n which gets used to identify the lines in the php code). It does not need any markup like </li> or
                .

                Note: you can change the \n to anything you want by changing the delimiter parameter.

                  [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]
                  • 4749
                  • 623 Posts
                  Thanks, once I actually looked at the snippet I realized that. embarrassed
                    The MODx has you...
                    Utah Web Design
                    • 2734
                    • 165 Posts
                    Hi. Sorry, i was away this weekend. Did you manage to make it work?
                    Cheers -- roman
                      • 4749
                      • 623 Posts
                      Roman,
                      No, still haven’t got it working. I’ve double-checked everything without any luck. Is there anything I should try?

                      Thanks!
                        The MODx has you...
                        Utah Web Design