• twitter image
  • facebook image
  • youtube image
  • linkedin image
Language: CMS Made Simple Czech CMS Made Simple France CMS Made Simple Spain CMS Made Simple Hungary CMS Made Simple Russia CMS Made Simple Netherlands

All times are UTC




Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: CGBlog summary template categories
PostPosted: Sun Jul 12, 2020 9:39 pm 
Offline
Forum Members
Forum Members

Joined: Thu Aug 23, 2007 5:20 pm
Posts: 26
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.

\$1:
{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
Screenshot_2020-07-12-Ribble-Valley-Jazz-and-Blues---Home.jpg [ 73.71 KiB | Viewed 194 times ]
Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: CGBlog summary template categories  Topic is solved
PostPosted: Mon Jul 13, 2020 5:44 am 
Offline
Dev Team Member
Dev Team Member

Joined: Mon Nov 28, 2011 9:29 am
Posts: 3233
Location: The Netherlands
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):
\$1:
{$is_socialmedia = false}
{foreach from=$entry->categories item='category'}
   {if $category.name == 'Social Media'}
      {$is_socialmedia = true}
      {break}
   {/if}
{/foreach}


Rest of the code:

\$1:
{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:

\$1:
<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:

\$1:
{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).

\$1:
<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.


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: CGBlog summary template categories [solved]
PostPosted: Tue Jul 14, 2020 8:40 pm 
Offline
Forum Members
Forum Members

Joined: Thu Aug 23, 2007 5:20 pm
Posts: 26
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.


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
Display posts from previous:  Sort by  
Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
A2 Hosting