This is a good question. I have a little bit dirty approach, but it is practical and saves time.
Within your "views/" folder make a file "feedback_messages.php" with the following sample content:
PHP Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
$normal_message = isset($normal_message) && trim($normal_message) != '' ? $normal_message : trim(get_instance()->session->flashdata('normal_message'));
$confirmation_message = isset($confirmation_message) && $confirmation_message != '' ? $confirmation_message : trim(get_instance()->session->flashdata('confirmation_message'));
$warning_message = isset($warning_message) && $warning_message != '' ? $warning_message : trim(get_instance()->session->flashdata('warning_message'));
$error_message = isset($error_message) && $error_message != '' ? $error_message : trim(get_instance()->session->flashdata('error_message'));
?>
<!-- Feedback messages -->
<div id="main_feedback_messages" class="clearfix">
<?php
if ($normal_message) {
?>
<div class="alert alert-info alert-dismissable centered-block">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<?php echo $normal_message; ?>
</div>
<?php
}
if ($confirmation_message) {
?>
<div class="alert alert-success alert-dismissable centered-block">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<?php echo $confirmation_message; ?>
</div>
<?php
}
if ($warning_message) {
?>
<div class="alert alert-warning alert-dismissable centered-block">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<?php echo $warning_message; ?>
</div>
<?php
}
if ($error_message) {
?>
<div class="alert alert-danger alert-dismissable centered-block">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<?php echo $error_message; ?>
</div>
<?php
}
?>
</div>
<script type="text/javascript">
//<![CDATA[
function show_main_feedback_message(message, message_type) {
if (typeof message != 'undefined' && message != '') {
if (typeof message_type == 'undefined' || message_type == '') {
message_type = 'normal_message';
}
var visual_class = 'alert alert-info alert-dismissable centered-block';
switch (message_type) {
case 'confirmation-message':
case 'confirmation_message':
visual_class = 'alert alert-success alert-dismissable centered-block';
break;
case 'warning-message':
case 'warning_message':
visual_class = 'alert alert-warning alert-dismissable centered-block';
break;
case 'error-message':
case 'error_message':
visual_class = 'alert alert-danger alert-dismissable centered-block';
break;
}
$('#main_feedback_messages').html('<div class="' + visual_class + '"> <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> ' + message + ' </div>');
}
}
function clear_main_feedback_message() {
$('#main_feedback_messages').html('');
}
//]]>
</script>
<!-- Feedback messages end -->
Then include directly this file within your view file that makes the final html layout (the html template of the pages), i.e. Include this file at the desired place within your html template.
This is a widget that shows feedback messages to the user. The messages may come from 3 different sources:
- directly passed into the view variables;
- flash data items
- activation by JavaScript - if your AJAX calls return feedback messages you can show them with this widget too.
You may pass text messages, of course you may pass simple HTML too, but you don't need to pass the design of the text messages again and again many times.