Problems, need help? Have a tip or advice? Post it here.
6 posts Page 1 of 1
HI,
I have a data-bound form of type textarea and I want the next line inputted by the user to create a "li" list.
Example:
Textarea:
line1
line2
line3

i want this textarea to output in list fromat when I do <cms:show textareaname />
like this:
<li>line1</li>
<li>line2</li>
<li>line3</li>

how do I approach it? thanks!

The best way to handle such use-case would be to use type 'repeatable' instead of 'textarea' - it would be trivial then to format each item as you wish.

That said, for cases where that is not possible (e.g. when the input in question is already a part of repeatable-region), you may try using the following -

Paste this code in your addons/kfunctions.php file (rename the sample file if a file of this name is not present) -
Code: Select all
// split string at double newlines
function my_each_handler( $params, $node ){
    global $FUNCS, $CTX;
    extract( $FUNCS->get_named_vars(
                array( 'var'=>'',
                      ),
                $params)
           );

    $var = trim( $var );

    if( strlen($var) ){
         $arr_vars = array_map( "trim", preg_split('/(?:\r\n){2,}|(?:\n\r){2,}|(?:\n){2,}|(?:\r){2,}/', $var) );
    }

    $children = $node->children;
    for( $x=0; $x<count($arr_vars); $x++ ){
        $CTX->set( 'k_count', $x );
        $CTX->set( 'item', $arr_vars[$x] );

        foreach( $children as $child ){
            $html .= $child->get_HTML();
        }
    }
    return $html;
}
$FUNCS->register_tag( 'my_each', 'my_each_handler', 1, 1 );

V.IMP: The code above would require each line in the textarea to be separated by *double* newlines.
While we could have made it recognize a single newline, I have found that very often users need to input individual items that themselves contain newlines; so a double newline requirement is a safer choice.

OK, so assuming the input in your textarea has each line separated by double newlines, on the frontend of your template, you can display it (assuming the name 'text_en ') as follows
Code: Select all
<ul class="cv">
    <cms:my_each text_en >
        <li><cms:show item /></li>
    </cms:my_each>
</ul>

Hope this helps.

I agree with KK about an empty line between separate items to allow line breaks, but if you don't need that complication the following code works with any number of new line -
Code: Select all
<cms:each text as='line' sep='\n'>
    <cms:if "<cms:strlen line />">
        <li><cms:show line /></li>
    </cms:if>
</cms:each>


I tested it with this sample and got perfect <li> items.
Code: Select all
<!-- testing text -->
<cms:capture into='text'>
123


345

567
789
</cms:capture>

yep! it worked!
thanks!

Adding for documentation sake -
have added a new parameter to <cms:each> that allows it to accept a regular-expression as the separator.

So now, assuming splitting the text on double-newlines is needed, instead of creating a custom tag (as done in my first reply above), we can use the following -
Code: Select all
<ul class="cv">
    <cms:each text_en as='line' sep='/(?:\r\n){2,}|(?:\n\r){2,}|(?:\n){2,}|(?:\r){2,}/' is_regex='1'>
        <li><cms:show line /></li>
    </cms:each>
</ul>

KK, this is awesome, will note the change in a help page. ;)
6 posts Page 1 of 1