Page 1 of 1
Access content images in news summary template
Posted: Tue Feb 26, 2019 12:59 pm
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???
Re: Access content images in news summary template
Posted: Tue Feb 26, 2019 2:35 pm
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.
Re: Access content images in news summary template
Posted: Tue Feb 26, 2019 2:46 pm
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 -->";
Re: Access content images in news summary template
Posted: Tue Feb 26, 2019 3:06 pm
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
Re: Access content images in news summary template
Posted: Tue Feb 26, 2019 3:31 pm
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}
Re: Access content images in news summary template
Posted: Tue Feb 26, 2019 3:34 pm
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.
Re: Access content images in news summary template
Posted: Tue Feb 26, 2019 4:11 pm
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}
Re: Access content images in news summary template
Posted: Tue Feb 26, 2019 4:33 pm
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}
Re: Access content images in news summary template
Posted: Tue Feb 26, 2019 4:34 pm
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)
Re: Access content images in news summary template
Posted: Tue Feb 26, 2019 6:30 pm
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}