CGBlog summary template categories

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Locked
johnflan
Forum Members
Forum Members
Posts: 35
Joined: Thu Aug 23, 2007 5:20 pm

CGBlog summary template categories

Post by johnflan »

I'm using CGBlog and in the summary template I want to to ad a rule that if a category is 'Social Media' then it should go to an external link (which I can add) and not link to the content.

Ok this works but it's not right because, if the blog entry has more than one category then it duplicates them. See attached at the bottom "RV JAZZ BIG BAND" with 2 images.

So it's the "foreach from=" I need help with.

Here's the bit of code from the template I'm having problems with.

Code: Select all

{if $entry->categories}
 {foreach from=$entry->categories item='category'}
   {if $category.name == 'Social Media'}
      <div class="CGBlogSummaryLink">
    <h4>{$entry->title|escape}</h4>
    </div>
    {if isset($entry->fields)}
      {foreach from=$entry->fields item='field'}
        <div class="CGBlogSummaryField">
            {if $field->type == 'image_select'}
              <img src="{CGSmartImage notag="1" src="{uploads_url}/{$field->value}" filter_croptofit="330,330,c"}"/>     
            {else}
              {$field->name}:&nbsp;{eval var=$field->value}
            {/if}
        </div>
      {/foreach}
    {/if}
{else}


   <div class="CGBlogSummaryLink">
    <h4><a href="{$entry->detail_url}" title="{$entry->title|escape:htmlall}">{$entry->title|escape}</a></h4>
    </div>
    {if isset($entry->fields)}
      {foreach from=$entry->fields item='field'}
        <div class="CGBlogSummaryField">
            {if $field->type == 'image_select'}
            <a href="{$entry->detail_url}" title="{$entry->title|escape:htmlall}">
              <img src="{CGSmartImage notag="1" src="{uploads_url}/{$field->value}" filter_croptofit="330,330,c"}"/>    
              <span class="image-overlay">
                <span class="overlay-icon"><i class="fa fa-file-text"></i></span>
              </span>       
            </a>    
            {else}
              {$field->name}:&nbsp;{eval var=$field->value}
            {/if}
        </div>
      {/foreach}
    {/if}


{/if}
 {/foreach}
{/if}
Attachments
Screenshot_2020-07-12-Ribble-Valley-Jazz-and-Blues---Home.jpg
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3483
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: CGBlog summary template categories

Post by velden »

Try to think about the logic you need in normal language and then translate that to code:

"I want to know if the entry is in the 'Social Media' category."

code (untested):

Code: Select all

{$is_socialmedia = false}
{foreach from=$entry->categories item='category'}
   {if $category.name == 'Social Media'}
		{$is_socialmedia = true}
		{break}
   {/if}
{/foreach}
Rest of the code:

Code: Select all

{if $is_socialmedia}
	<div class="CGBlogSummaryLink">
		<h4>{$entry->title|escape}</h4>
	</div>
	{if isset($entry->fields)}
		{foreach from=$entry->fields item='field'}
			<div class="CGBlogSummaryField">
			{if $field->type == 'image_select'}
				<img src="{CGSmartImage notag="1" src="{uploads_url}/{$field->value}" filter_croptofit="330,330,c"}"/>     
			{else}
				{$field->name}:&nbsp;{eval var=$field->value}
			{/if}
			</div>
		{/foreach}
	{/if}
{else}
	<div class="CGBlogSummaryLink">
		<h4><a href="{$entry->detail_url}" title="{$entry->title|escape:htmlall}">{$entry->title|escape}</a></h4>
	</div>
	{if isset($entry->fields)}
		{foreach from=$entry->fields item='field'}
			<div class="CGBlogSummaryField">
			{if $field->type == 'image_select'}
				<a href="{$entry->detail_url}" title="{$entry->title|escape:htmlall}">
				<img src="{CGSmartImage notag="1" src="{uploads_url}/{$field->value}" filter_croptofit="330,330,c"}"/>   
				<span class="image-overlay">
				<span class="overlay-icon"><i class="fa fa-file-text"></i></span>
				</span>       
				</a>   
			{else}
				{$field->name}:&nbsp;{eval var=$field->value}
			{/if}
			</div>
		{/foreach}
	{/if}
{/if}
You could even use the variable in more places to make the template smaller and less repeating code. But that's up to you:

Code: Select all

<div class="CGBlogSummaryLink">
	{if $is_socialmedia}
		<h4>{$entry->title|escape}</h4>
	{else}
		<h4><a href="{$entry->detail_url}" title="{$entry->title|escape:htmlall}">{$entry->title|escape}</a></h4>
	{/if}
</div>
{if isset($entry->fields)}
	{foreach from=$entry->fields item='field'}
		<div class="CGBlogSummaryField">
		{if $field->type == 'image_select'}
			{if $is_socialmedia}
				<a href="{$entry->detail_url}" title="{$entry->title|escape:htmlall}">
			{/if}
			<img src="{CGSmartImage notag="1" src="{uploads_url}/{$field->value}" filter_croptofit="330,330,c"}"/>     
			{if $is_socialmedia}
				<span class="image-overlay">
				<span class="overlay-icon"><i class="fa fa-file-text"></i></span>
				</span>       
				</a>   
			{/if}
		{else}
			{$field->name}:&nbsp;{eval var=$field->value}
		{/if}
		</div>
	{/foreach}
{/if}
Now we're at it I want to give you a tip about the use of fields in a smarty template.

I see you're using the code from the sample template that comes with the module:

Code: Select all

{if isset($entry->fields)}
	{foreach from=$entry->fields item='field'}
Be aware that a sample template doesn't know what field definitions a website builder will add, so it has no choice but using a foreach loop to work.
BUT, you are the website builder and you DO know what fields you've added. Also you know if/what fields are required to be filled by an editor. AND you know of what type a specific field is (file, image, text, etc.).

So, don't go for the inefficient and hard to read 'foreach' approach but just build your template in the layout you want, using the fields you have. (You're already doing it with the title. The title is a required field and always present, that's why it is used like that in the sample templates).

The template code will be easier to read as it looks more like the html output. Also it will be easier to convert an existing html template to a CMSMS module template this way.
Further, the order of the fields in the back end is not relevant for the output on the front end.

I don't know your field aliases so the code below is just an example (using the fictive aliases 'image' and 'description' for your fields).

Code: Select all

<div class="CGBlogSummaryLink">
	{if $is_socialmedia}
		<h4>{$entry->title|escape}</h4>
	{else}
		<h4><a href="{$entry->detail_url}" title="{$entry->title|escape:htmlall}">{$entry->title|escape}</a></h4>
	{/if}
</div>
<div class="CGBlogSummaryField">
	{if $is_socialmedia}
		<a href="{$entry->detail_url}" title="{$entry->title|escape:htmlall}">
	{/if}
	<img src="{CGSmartImage notag="1" src="{uploads_url}/{$entry->fields['image']->value}" filter_croptofit="330,330,c"}"/>     
	{if $is_socialmedia}
		<span class="image-overlay">
		<span class="overlay-icon"><i class="fa fa-file-text"></i></span>
		</span>       
		</a>   
	{/if}
</div>
<div class="CGBlogSummaryField">
	{$entry->fields['description']->value}
</div>
Looking at your screenshot I think you may already be using this logic.
johnflan
Forum Members
Forum Members
Posts: 35
Joined: Thu Aug 23, 2007 5:20 pm

Re: CGBlog summary template categories [solved]

Post by johnflan »

Thanks Velden

That works so thank you. I'm just studying "the variable in more places" to make the template smaller and also I can see the sense in not using the "foreach" approach but sorry it's my complacency and laziness because I've always managed to make it work.

I really should work with a coder it would save me such a lot of time and produce a better website.

Many thanks for your help.
Locked

Return to “Modules/Add-Ons”