GDPR privacy disclaimer
This form will send me your email address and your IP address.
If you don't want that, don't use this form!
Simple list
Previous article Next articleA 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));
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:' '}</td>
<td>{$items.1|default:' '}</td>
<td>{$items.2|default:' '}</td>
<td>{$items.3|default:' '}</td>
</tr>
{/foreach}
</table>
{/if}
{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:' '}</td>
<td>{$items.1|default:' '}</td>
<td>{$items.2|default:' '}</td>
<td>{$items.3|default:' '}</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
# 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 1 | Column 2 | Column 3 | Column 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 |
Comment Form
ReviewManager
ReviewManager
1 Comment
20-02-2018
This is a very useful solution I think. Obviously this mandatory 'content' parameter will not work in the same way in Products Manager module - is there any better way of manipulating this code to use in some type of module?
Many thanks!