Welcome Guest, Not a member yet? Register   Sign In
Suggestion for helper function: anchor_image
#1

[eluser]t'mo[/eluser]
I got tired of writing stuff like this:

Code:
<a href="&lt;?=site_url('A-G')?&gt;"><img src="&lt;?=site_url('res/a-g.png')?&gt;" border="0" alt="A-G"></a><br>
<a href="&lt;?=site_url('H-P')?&gt;"><img src="&lt;?=site_url('res/h-p.png')?&gt;" border="0" alt="H-P"></a><br>
...
<a href="&lt;?=site_url('A-Z')?&gt;"><img src="&lt;?=site_url('res/all.png')?&gt;" border="0" alt="All"></a>

so I wrote a little helper to do it for me:

Update 5/22/2008, 12:16 PM EDT - shortened this up a bit, removed check for defined function

Code:
&lt;?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

// file: application/helpers/MY_url_helper.php

function anchor_image($href, $src, $alt = null)
{
  $altStr = 'alt="' . (null != $alt ? $alt : $href) . '"';
  $imgStr = '<img src="' . site_url($src) . '" border="0" ' . $altStr . '>';
  return anchor($href, $imgStr);
}

and now my view code looks like this:

Code:
&lt;?=anchor_image('A-G', 'res/a-g.png')?&gt;<br>
&lt;?=anchor_image('H-P', 'res/h-p.png')?&gt;<br>
...
&lt;?=anchor_image('A-Z', 'res/all.png', 'All')?&gt;

It could be improved by adding the ability to take arrays as params (for things like attributes), but it does what I need for now. I would really like to see something like this in the Array helper core; if any of you working on the core want to "steal" this code, feel free.
#2

[eluser]xwero[/eluser]
I think the border attribute shouldn't be hard coded. In you css file you can do
Code:
a img { border : 0; }
Which is a more flexible way of handling the browser behavior.

I also think you want to use base_url instead of site_url for the image links because for your example it's possible to get an url like this : http://site.com/index.php/res/all.png.

The changed function
Code:
function anchor_image($href, $src, $extra = NULL)
  {
    if(is_string($extra) && strpos($extra,' ') !== 0){ $extra = ' '.$extra; }
    $img = '<img src="' . base_url() . $src . '"' . $extra . '>';

    return anchor($href, $img);
  }
If you put this function in the extended helper file you don't have to add the function_exists check because that file can't be extended.
#3

[eluser]t'mo[/eluser]
Thanks for the suggestions; I hadn't considered the implications of 'base_url' vs. 'site_url' for images. Another thing I thought of right after posting this was what happens if your images are not located on the same server? I'll have to come up with a solution for that...

[quote author="xwero" date="1211455220"]I think the border attribute shouldn't be hard coded.
...
If you put this function in the extended helper file you don't have to add the function_exists check because that file can't be extended.[/quote]

Regarding the 'border="0"', some browsers don't pay attention to the CSS solution.

I'm not sure I understand what you're saying with the 'function_exists' check; the core url_helper file checks, so why shouldn't I?
#4

[eluser]Czarchaic[/eluser]
Code:
&lt;?=anchor('A-G', img(array('src'=>'res/a-g.png','alt'=>'')),array('class'=>'myAnchor'))?&gt;
#5

[eluser]xwero[/eluser]
Which browsers doesn't pay attention to the css solution? I can't think of one. And if it is a browser quirk it still could be fixed in css i think.
What if the designer just wants to use a border, with your solution he has to write the tags himself.

CI's url_helper checks if the function exists because it can be extended. You cant extend the MY_url_helper.php file. The extending of the helpers differs from extending classes where the methods get overloaded. For the helpers the extended file gets loaded first.
#6

[eluser]xwero[/eluser]
[quote author="Czarchaic" date="1211487678"]
Code:
&lt;?=anchor('A-G', img(array('src'=>'res/a-g.png','alt'=>'')),array('class'=>'myAnchor'))?&gt;
[/quote]
I think the situation is that common to create a new function for it.

One more thing there also should be a parameter to add attributes to the anchor tag.
#7

[eluser]t'mo[/eluser]
[quote author="xwero" date="1211487699"]What if the designer just wants to use a border, with your solution he has to write the tags himself.
[/quote]

Yes; like I said, what I've come up with is specific to my current project, and "the designer" (/me) doesn't need borders...but others might. However, and it sounds like you agree, the problem of making hyperlinks that consist of images rather than text is common, and I think solution in the core would be very nice to have (and though none of my code can be salvaged, the name "anchor_image" would be great). Also, if this helper were in the core, it should also handle (arrays of) attributes (for both the A and the IMG).

[quote author="xwero" date="1211487699"]
CI's url_helper checks if the function exists because it can be extended. You cant extend the MY_url_helper.php file. The extending of the helpers differs from extending classes where the methods get overloaded. For the helpers the extended file gets loaded first.[/quote]

Thanks for explaining that. You killed two birds with one stone in fact, saving me from making another post. I was wondering why "my_url_helper.php" wasn't loading when I asked only for to load the "url" helper. ("my_url..." != "MY_url...")

Update: fixed typo, clarified which "previous poster" I referred to
#8

[eluser]Czarchaic[/eluser]
[quote author="xwero" date="1211487871"][quote author="Czarchaic" date="1211487678"]
Code:
&lt;?=anchor('A-G', img(array('src'=>'res/a-g.png','alt'=>'')),array('class'=>'myAnchor'))?&gt;
[/quote]
I think the situation is that common to create a new function for it.

One more thing there also should be a parameter to add attributes to the anchor tag.[/quote]

The third parameter of the anchor() function is to set attributes.
#9

[eluser]xwero[/eluser]
Czarchaic my remark was for the anchor_image function. You have to load the url and html helpers for your solution.

t'mo if you like to see your function added to CI you start a topic in the request feature section with the working code. They make you work to get your code in CI Wink




Theme © iAndrew 2016 - Forum software by © MyBB