Sometimes it happens, that a template produces errors or warnings and nobody knows why, because you can't find an error-statement.
In this context arose the question if it's possible to collect all error- and warning-statements and show them all together or just their quantity at the end of the page within the preview.
The answer is: Yes, both is possible and it's only your decision, what you want or what you need.
(Note
The following text is based on section templates but of course it's possible to use the code in page templates, too.)
Show error- and warning-statements unsorted
Code:
$CMS_IF(#global.preview)$
$CMS_SET(#global.collectLogs, 3)$
$CMS_END_IF$
$-- -1: off, 0: TRACE, 1: DEBUG, 2: INFO, 3: WARN, 4: ERROR --$
... some template code ...
$CMS_IF(#global.preview)$
<div class="errors">
$CMS_FOR(logEntry, #global.logs)$
<br /> $CMS_VALUE(logEntry)$
$CMS_END_FOR$
</div>
$CMS_END_IF$
Preview:
Show error- and warning-statements sorted
Code:
$CMS_IF(#global.preview)$
$CMS_SET(list, [] )$
$CMS_FOR(logEntry, #global.logs)$
$CMS_SET(list, list.add(logEntry.toString))$
$CMS_END_FOR$
$CMS_SET(list, list.sort)$
<div class="errors">
$CMS_FOR(item, list)$
$CMS_VALUE(item)$
<br />
$CMS_END_FOR$
</div>
$CMS_END_IF$
Preview:
Show quantity of error- and warning-statements
Code:
$CMS_SET(#global.collectLogs, 4)$
$-- -1: off, 0: TRACE, 1: DEBUG, 2: INFO, 3: WARN, 4: ERROR --$
... some template code ...
$CMS_IF(#global.preview)$
$CMS_SET(errors, 0)$
$CMS_SET(warnings, 0)$
$CMS_FOR(logEntry, #global.logs)$
$CMS_SET(substring ,logEntry.toString.subString(0,4))$
$CMS_IF(substring.equals("WARN"))$
$CMS_SET(warnings, warnings+1)$
$CMS_ELSIF(substring.equals("ERRO"))$
$CMS_SET(errors, errors+1)$
$CMS_END_IF$
$CMS_END_FOR$
$CMS_IF(errors != 0 || warnings != 0)$
<div class="errors">
This Template includes
$CMS_IF(warnings != 0)$<span class="bold">$CMS_VALUE(warnings)$ Warning(s)</span>$CMS_END_IF$
$CMS_IF(warnings != 0 && errors != 0)$ and $CMS_END_IF$
$CMS_IF(errors != 0)$<span class="bold"> $CMS_VALUE(errors)$ Error(s)</span> $CMS_END_IF$ !
</div>
$CMS_END_IF$
$CMS_END_IF$
Preview: