Access content images in news summary template

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
Sendlingur
Forum Members
Forum Members
Posts: 61
Joined: Wed Aug 08, 2018 4:59 pm

Access content images in news summary template

Post by Sendlingur »

I recently copied a lot of news from a older version of my web to a newer version.

The problem is: In the older version Images was added to the news articles as CONTENT via the wyzwig editor. So the url to those images is upload/images/image.jpg...

But my news summary template assumes that images are linked to every news_id. If an article doen't have a image it displays a default image. (see below)

Code: Select all

{foreach from=$items item=entry}
<div class="news-item animation-top">
    <figure class="bg-img">{if isset($entry->fields)}
        {foreach from=$entry->fields item='field'}  
        {if $field->type == 'file'}
          {if isset($field->value) && $field->value}
            <img src="{$entry->file_location}/{$field->value}"/>
          {/if}
        {else}
     <img src="uploads/images/img-01.jpg"/> <!-- default image -->
         {/if}
  {/foreach}
Is there a way to modify the template so it uses only the <img> from the {$entry} for the older articles???
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1112
Joined: Wed Feb 25, 2009 4:25 am
Location: Victoria, BC

Re: Access content images in news summary template

Post by DIGI3 »

Take a look at the CGSmartImage module, specifically the {cgsi_getimages} wrapper. It will allow you to extract images from $entry->detail (or whatever) and use them as you see fit. It will take some work to set up.
Not getting the answer you need? CMSMS support options
Sendlingur
Forum Members
Forum Members
Posts: 61
Joined: Wed Aug 08, 2018 4:59 pm

Re: Access content images in news summary template

Post by Sendlingur »

thank you for this answer, I don't have time to install/setup new things for this project.

I have been trying to work around this problem by using this code in a UDT, but somehow the NEws template is not able to read to {$entry->content} tag

Code: Select all

$subject= {$entry->content};
$pattern='/.*<img src="(.*?)".*/';

$success = preg_match($pattern, $subject, $match);
if ($success) {
              
                echo      '<img src="'.$match[1].'"/>'; 

}

echo "<!-- blblb -->";
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1112
Joined: Wed Feb 25, 2009 4:25 am
Location: Victoria, BC

Re: Access content images in news summary template

Post by DIGI3 »

I think it would be less time to use an existing module than to try to do it with a UDT:
1) install CGSI
2) add {cgsi_getimages assign='imageinfo' nocontent=1}{$entry->content}{/cgsi_getimages} to your news detail template

There, you now have an array of image tags, $imageinfo
Not getting the answer you need? CMSMS support options
Sendlingur
Forum Members
Forum Members
Posts: 61
Joined: Wed Aug 08, 2018 4:59 pm

Re: Access content images in news summary template

Post by Sendlingur »

Ok thank you, this sound like a better solution :)

how would I use the $imageinfo to display below the {else} tag in my code?

Code: Select all

{foreach from=$items item=entry}
<div class="news-item animation-top">
    <figure class="bg-img">{if isset($entry->fields)}
        {foreach from=$entry->fields item='field'}  
        {if $field->type == 'file'}
          {if isset($field->value) && $field->value}
            <img src="{$entry->file_location}/{$field->value}"/>
          {/if}

        {else}

     
         {/if}
   
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1112
Joined: Wed Feb 25, 2009 4:25 am
Location: Victoria, BC

Re: Access content images in news summary template

Post by DIGI3 »

I'm pretty sure it's just an array but I haven't used it in quite a while. You can {$imageinfo|print_r} to see what's in it, then probably a foreach loop to output the img tags. I think it contains the full tags but you'll need to check and adjust accordingly.
Not getting the answer you need? CMSMS support options
Sendlingur
Forum Members
Forum Members
Posts: 61
Joined: Wed Aug 08, 2018 4:59 pm

Re: Access content images in news summary template

Post by Sendlingur »

this is great... I'm almost there :)

I can see the images in the array by using {$imageinfo|print_r}

Code: Select all

Array
(
    [0] =>Array
        (
            [tag] =>"<img src="uploads/images/Bokverkstaedi.jpg" alt="" width="500" height="375"> "

            [src] => uploads/images/Bokverkstaedi.jpg
            [width] => 500
            [height] => 375
        )

)
1
But if I do I get no results

Code: Select all

 
{foreach from=$imageinfo item=image}
  {$image->tag}
{/foreach}
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: Access content images in news summary template

Post by calguy1000 »

because each item is an associative array, not an object.

Code: Select all

Array
(
    [0] =>Array <-- SEE THIS
        (
            [tag] =>"<img src="uploads/images/Bokverkstaedi.jpg" alt="" 
So you should be using array syntax to access the array elements.

i.e:

Code: Select all

{foreach $items as $one}
    {$one.tag}  or {$one['tag']}
{/foreach}
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1112
Joined: Wed Feb 25, 2009 4:25 am
Location: Victoria, BC

Re: Access content images in news summary template

Post by DIGI3 »

Try $image.tag

For more control, you can also do something like:

Code: Select all

{CGSmartImage src=$image.src noembed=1 max_width=600}
(adding/adjusting parameters as you see fit)
Not getting the answer you need? CMSMS support options
Sendlingur
Forum Members
Forum Members
Posts: 61
Joined: Wed Aug 08, 2018 4:59 pm

Re: Access content images in news summary template

Post by Sendlingur »

Thank you all for your posts, I'm constantly learning new things :)

I made it all work by using

Code: Select all

 {foreach from=$imageinfo item=image}
 {$image.tag}
{/foreach}

Post Reply

Return to “Modules/Add-Ons”