Suggestion for helper function: anchor_image |
[eluser]t'mo[/eluser]
I got tired of writing stuff like this: Code: <a href="<?=site_url('A-G')?>"><img src="<?=site_url('res/a-g.png')?>" border="0" alt="A-G"></a><br> 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: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); and now my view code looks like this: Code: <?=anchor_image('A-G', 'res/a-g.png')?><br> 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.
[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; } 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)
[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?
[eluser]Czarchaic[/eluser]
Code: <?=anchor('A-G', img(array('src'=>'res/a-g.png','alt'=>'')),array('class'=>'myAnchor'))?>
[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.
[eluser]xwero[/eluser]
[quote author="Czarchaic" date="1211487678"] Code: <?=anchor('A-G', img(array('src'=>'res/a-g.png','alt'=>'')),array('class'=>'myAnchor'))?> 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.
[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
[eluser]Czarchaic[/eluser]
[quote author="xwero" date="1211487871"][quote author="Czarchaic" date="1211487678"] Code: <?=anchor('A-G', img(array('src'=>'res/a-g.png','alt'=>'')),array('class'=>'myAnchor'))?> 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.
[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 ![]() |
Welcome Guest, Not a member yet? Register Sign In |