The challenge was to create a mechanism for routinely customising email notifications based on a library of about 20 different draft emails. Also, those draft emails needed to be available to site administrators for editing.
The CI Template Parser Class went a long way to help overcome the challenge in an efficient manner but it was by no means a painless solution. Up to and including CI v.1.7.3 the Template Parser class the only input accepted for parsing was a template file, meaning a View file per email draft. I was not keen on this for a couple of reasons. Having the draft emails stored as files made the CMS aspect messier than I wanted. Also I didn't want to dump a load of email related templates in with the actual site Views.
Then along came CI v.2 which included a broadening of the accepted inputs. Specifically, it now also accepts a string.
Yureka! By storing the various email templates as text within a database table they can be easy retrieved and passed into the Template Parser. Thus making the CMS aspect a lot tidier as we no longer have to consider any file handling for the CRUD functionality as it is all stored within a database.
Thanks EllisLabs for doing the hard work for me, before I even got there. Should anybody reading this be stuck on an older version of CodeIgniter here is the library extension I created, using the CodeIgniter2 Template Parser code, for backwards compatability.
$user = $this->db->where( 'id', 1 )->get( 'user_account' )
$data = array(
'user_email' => $user->name,
'user_password' => $user->password,
'user_profile_link' => site_url( 'user/'.$user->id )
$email = $this->db->where( 'email', 'welcome message' )->get( 'draft_email' )
$message = $this->parser->parse_string( $email->content, $data, TRUE )