MENU

Simple list

  Previous article Next article  

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

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}

  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

Advertisement


Ads help me to help you! Thanks!

Ads help me to help you! Buy products from these advertisers!


  Comment Form

Click here to open the form


  1 Comment

CMS Made Simple - Tutorials, tips and tricks - CMSMS

Simple list

  Article optimized for CMSMS 2.x

  Author:
  Last tested in: CMSMS 2.2.3.1
  Last updated: 07-08-2018
  Comments: 1
  http://cms.ms/Ryme

Advertisement


Ads help me to help you! Thanks!

Ads help me to help you! Buy products from these advertisers!

Advertisement


Ads help me to help you! Thanks!

Ads help me to help you! Buy products from these advertisers!