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';

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

$lines = array_filter(
  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>
      <tr><th>Column 1</th><th>Column 2</th><th>Column 3</th><th>Column 4</th></tr>
      {foreach from=$lines item=line}

  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

Buy Me A Coffee

  Comment Form


Click here to open the form


  1 Comment

Buy Me A Coffee

CMS Made Simple - Tutorials, Tips and Tricks - CMSMS

Simple list

  Article optimized for CMSMS 2.x

  Last tested in: CMSMS
  Last updated: 07-08-2018
  Comments: 1

Buy Me A Coffee


Ads help me to help you! Thanks!

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