« Back

Setting a default image for News or CGBlog

Posted by: mark | May 8, 2013 | Category: Smarty

When using News or CGBlog sometimes we need to add an image but may not have one for every article, so we need to call a default image to fill in.

This can be used in any module that has extra fields, you just need to know the syntax on how to call it, using <pre>{get_template_vars}</pre> in the modules template will output a nice list of what variables are available.

This is going to be using the field definition type 'file', CGBlog does have an image field but not News so we will stick with the same for both in this example.

Using the infamous 'if not empty' call, {if !empty($somefield)}.


Place this in the modules template where you want it to appear.

{if !empty($entry->fields.image->value)}
<img src="{$entry->file_location}/{$entry->fields.image->value}" width='200' />
{else}
<img src="uploads/images/logo1.gif" width='200' />
{/if}

The width='200' is an option, it keeps them all the same width in case they add an image that is too wide and messes up the layout.

In the example I added an extra div around all the articles floated left as I'm floating the image and text left this will allow me to get them in line where I want them.

/* -------- Example CSS -------- */

.holder {
	float: left;
	clear: both;
	margin: 5px 0 14px;
}
.newsimage, .NewsSummary {
	float: left;
}
.NewsSummary {
	width: 220px;
	margin-left: 14px;
}