0

Hi I have tried various things but not sure if I am trying to over complicate things. Quite simply I want to add a small image country flag for the word 'Spanish' and 'English' and have an 'Alt' tag with those words instead. The following works fine with just text and as is but it would be cool to have the image either to replace or sit side by side with the word. Any help available?

    echo $this->Html->link('Spanish', array('controller'=>'settings', 'action'=>'lang', 'spa'),
        array('rel'=>'nofollow'));
}

else {
    echo $this->Html->link('English', array('controller'=>'settings', 'action'=>'lang', 'eng'),
        array('rel'=>'nofollow'));
}
3
  • I should add the html out put for English is like this at the moment: <a href="/lang/eng" rel="nofollow">English</a> </div> Commented Aug 8, 2016 at 12:34
  • please tag the question accordingly to you exact cakephp version Commented Aug 8, 2016 at 12:35
  • Thank you, CakePHP 2.8 Commented Aug 8, 2016 at 12:37

2 Answers 2

1

You can either output just a linked image using the image method with a url attribute:-

<?php

echo $this->Html->image(
    'spain.jpg', 
    array(
        'alt' => 'Spanish',
        'url' => array('controller' => 'settings', 'action' => 'lang', 'spa')
    )
);

Or you can include an image with a text link by combining the normal CakePHP link method and the image method:-

<?php

echo $this->Html->link(
    h('Spanish') . $this->Html->image('spain.jpg', array('alt' => 'Spanish')),
    array('controller' => 'settings', 'action' => 'lang', 'spa'),
    array('escape' => false, 'rel' => 'nofollow')
);

With the link method you need to remember to prevent Cake from escaping the img tag using 'escape' => false. If you disable escaping like this it is work remembering to escape any user provided text by using the h() method to prevent any HTML injection (I've shown this in my example wrapping the word 'Spanish' but this is only necessary if this is coming from a variable).

Sign up to request clarification or add additional context in comments.

Comments

0

if you are using cake2 see this link to the cookbook

echo $this->Html->image("spain.jpg", array(
    "alt" => "Spanish language",
    'url' => array('controller' => 'settings', 'action' => 'lang', 'spa')
));

6 Comments

That didn't work unfortunately. It broke the page. I am using version 2.8. I just want the code I put up to give this output:<a href="/lang/spa" rel="nofollow">Spanish<img src="/img/spain.jpg" class="photo" alt="Spanish"/></a>
please explain: do you want the image to be a link, or do you simply want the image to be right after the link but not clickable?
Sorry making mistakes...nervous!
I would like the image to be clickable. With the alt attribute word to say Spanish
If it is easier just to put the image next to the link that would help me. Thanks
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.