Simple list

  Previous article Next article  

Simple list

  Article optimized for CMSMS 2.x

  Author:
  Last tested in: CMSMS 2.1.4
  Last updated: 31-07-2016
  Comments: 0

A User Defined Tag (UDT) in CMS Made Simple™ to convert a plain text content block value into an array of lines where lines starting with '#' and empty lines are ignored.

  How to use

Note: this method requires PHP 5.3+

UDT 'content_to_array' code:
if (!isset($params['assign']) || !isset($params['content'])) {
 echo 'UDT "content_to_array" requires parameters "content" and "assign" to be set';
 return;
}

$assign_var = $params['assign'];
$content = $params['content'];

$lines = array_filter(
  explode("\n",$content),
  function($a) {
    $b = trim($a);
    return ($b != '' && substr($a,0,1) != '#');
  }
);

$smarty->assign($assign_var, array_map(function($a){return trim($a);},$lines));
Content block definition in page template:
{content block=cnt_example assign=cnt_example label='Example' wysiwyg=false}
Use content somewhere in page template:
{content_to_array assign=lines content=$cnt_example}
{if $lines|count > 0}
  <h3>Example table</h3>
    <table>
      <tr><th>Column 1</th><th>Column 2</th><th>Column 3</th><th>Column 4</th></tr>
      {foreach from=$lines item=line}
      {$items='|'|explode:$line}
      <tr>
        <td>{$items.0|default:'&nbsp;'}</td>
        <td>{$items.1|default:'&nbsp;'}</td>
        <td>{$items.2|default:'&nbsp;'}</td>
        <td>{$items.3|default:'&nbsp;'}</td>
      </tr>
      {/foreach}
    </table>
{/if}

A2 Webhosting CMS Made Simple

  Working example

The page content is:
# This content is used to fill the holiday table
# Use a | symbol to separate fields
# Example:
# Mo 25/12|Christmas|8:00-11:00|Extra description

Mo 25/12|Christmas|8:00-11:00|Extra description
Tu 26/12|Christmas|8:00-11:00|Extra description
Su 31/12|New Years Eve|8:00-11:00|Extra description
The screen output will be:

Example table

Column 1Column 2Column 3Column 4
Mo 25/12 Christmas 8:00-11:00 Extra description
Tu 26/12 Christmas 8:00-11:00 Extra description
Su 31/12 New Years Eve 8:00-11:00 Extra description

A2 Webhosting CMS Made Simple

  Comments

  Click here to leave a comment






This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)
  « Enter the CAPTCHA code (lower case)

Your IP address will be stored on submit. I reserve the right to change or delete your response without notice!
In case of abuse your IP address will be blocked!


Can I have your attention for a moment?

Did this tutorial help you solving a problem at your (clients) website and it saved you many hours of work? Great!!
Consider buying Rolf a cup of coffee in return! [ Click here ]


  0 Comments

No comments yet...

CMS Made Simple 2.2.2 - Tutorials, tips and tricks - CMSMS 2.2.2

Simple list

  Article optimized for CMSMS 2.x

  Author:
  Last tested in: CMSMS 2.1.4
  Last updated: 31-07-2016
  Comments: 0

A2 Webhosting CMS Made Simple