Icon next to article title if specific tag is set

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
Post Reply
mo
Regular
Posts: 40
Joined: Sun Jan 27, 2008 11:33 pm
Contact:

Icon next to article title if specific tag is set

Post by mo »

I want to display a small icon next to the article title if a specific tag is set (freetag plugin). I currently insert the icon manually in my blog body, but it would be more flexible and clean if I could insert the icon globally.

Can you give me a hint on how to do this within my template? How can I check if a tag is set?

Code: Select all

<div class="serendipity_entry serendipity_entry_author_{$entry.author|@makeFilen
ame} {if $entry.is_entry_owner}serendipity_entry_author_self{/if} ">

        {if $entry.tags contain "my_tag"}
          // ... insert image ...
        {/if}

        <h4 class="serendipity_title"><a href="{$entry.link}">{$entry.title|@def
ault:$entry.body|truncate:200:" ..."}</a></h4>
        <div class="serendipity_entry_body">
 {...}
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Icon next to article title if specific tag is set

Post by garvinhicking »

Hi!

You can edit your template's config.inc.php and disable PHP security by using:

Code: Select all

<?php
$serendipity['smarty']->security = false;
This allows you to access all PHP functions. then you can use in your template:

Code: Select all

{if in_array('MY_TAG', $entry.properties.freetag_tags)}
.... insert image ...
{/if}
HTH,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post by judebert »

If you don't feel like disabling security, you can also loop through the tags, searching for your specific entry:

Code: Select all

{foreach from=$entry.properties.freetag_tags item='tag'}
    {if $tag == 'MY_TAG'}<img src="my icon" />{/if}
{/foreach}
Of course, you might not like to waste time in the looping. In that case, you could provide your own Smarty function in the template's config.inc.php:

Code: Select all

<?php
global $serendipity;
$serendipity['smarty']->register_function('tag_icon', 'template_tag_icon');

function template_tag_icon($params, &$smarty)
{
  if(empty($params['tags'])) {
    return;
  } 
  if(empty($params['name'])) {
    return;
  }
  if (in_array('MY_TAG', $params['tags'])) {
    $img_data = array('src' => 'MY_TAG_IMG_SRC',
        'alt' => 'MY_TAG_ALT_TEXT');
    $smarty->assign($params['name'], $img_data);
  } else {
    $smarty->assign($params['name'], '');

  return;
}
?> 
And then in your template:

Code: Select all

{tag_icon tags=$entry.properties.freetag_tags name='tag_img'}
{if $tag_img}<img src="{$tag_img.src}" alt="{$tag_img.alt}" />{/if}
Besides not turning off security, you also get to assign additional information (like the alt tag), and you can look up more than one tag, or provide different icons depending on which information is most important to you.
Judebert
---
Website | Wishlist | PayPal
Post Reply