Sending ExpressionEngine 2.0 templates through the Communicate panel - Christopher Imrie.com

← Next Previous →

This site is built on HTML5 & CSS3.

Christopher Imrie is an Adobe Certified Instructor

Sending ExpressionEngine 2.0 templates through the Communicate panel

So one of the most frustrating things I always found about ExpressionEngine is Communicate panel.  Now dont get me wrong, I think the communicate panel does exactly what it should do in a very clear and functional manner.  The frustration comes from not being able to use the greatest thing about ExpressionEngine: the templates.

Now I’m not suggesting that EllisLab have got it wrong by not allowing the templates to be used in the Communicate panel, but for some of my projects I like to at least have the option.  Using the templates allows you to really get some nice looking emails stacked with all your site’s content.

So to remedy my frustration I came up with a method of adding a drop down list to the ExpressionEngine 2.0 communicate panel that lists your sites templates by template group. You can send plain emails as normal but selecting a template from the list will fill the message text area with the rendered template. Since the template is fully rendered, you can send this very easily to your mailing lists and the unsubscribe links will be appended to the bottom of the email automatically.

Here is a screenshot:

On a serious note, the code below is to be considered a hack. It requires modifying core system files, and as such it will have to be reapplied after each ExpressionEngine update. There may also be a future update that breaks it all together.

Modifying the communicate controller

The first step is inserting some code into the communicate controller.  The file you want is:

system/expressionengine/controllers/cp/tools_communicate.php

This file controls the rendering of the Communicate panel.  We dont need to do anything very complicated in the backend, since all we want to do is add a list of the templates to the control panel and then have this added to the message textarea when its selected.

Add this at around line 274:

$this->javascript->output('$("select#template").change(function(){
								$.get($(this).val(), function(data){
										$("#message").val(data);
								});});');
$q = $this->template_model->get_templates();
$qa = $q->result_array();
$tmpl = array(" " => array(" " => "None"));
for($i = 0; $i < sizeof($qa); $i++){
		$tmpl[$qa[$i]['group_name']][site_url($qa[$i]['group_name']."/".$qa[$i]['template_name'])] = $qa[$i]['template_name'];
}
$vars['templates']	= 	$tmpl;

Modifying the communicate view

Since ExpressionEngine 2.0 is now based on the MVC pattern, we need to modify the view file that is used to render the communicate control panel. The file you want is:

themes/cp_themes/default/tools/communicate.php

All you need to do is add the following at around line 59:

<h3>Template</h3>
		<ul class="shun">
				<li class="odd">
				<?php echo form_dropdown('template', $templates, '0', 'id="template"')?>
				</li>
		</ul>

You should now have a drop down list on your communicate panel that will allow you send your templates via email.

Using non-standard site index name

If you are using a non-standard site index name (ie: not index.php) then you may hit a problem where the template is not inserted into the message text area.  To fix this, find the config file:

system/expressionengine/config/config.php

And then change the index page name at around line 49 to whatever you have named your index file:

$config['index_page'] = 'index.php';