I
I
Igor Vasiliev2017-09-01 04:16:32
JavaScript
Igor Vasiliev, 2017-09-01 04:16:32

How to display phone number via Html helper?

Hello.
--
The situation is such that I was looking for a way to make the phone on the site clickable. That is, not just a link, but a function call. tel:
Very useful for mobility, when you click on such a number with your finger on the phone, the phone number is dialed. That is, you do not need to copy, paste, retype from the browser, but simply clicked and called. So, in Yii2 there is a solution with email: we get:
<?= Html::mailto('[email protected]') ?>

<a href="mailto:[email protected]">[email protected]</a>

I was looking for a solution on the Internet, but I only came across a solution with <?=Html::a();?>
And I need to enter it like this:
<?=Html::phone('+7 (999) 00-00-000', ['class' => 'phone']);?>

And it came out:
<a class="phone" href="tel:+7 (999) 00-00-000">+7 (999) 00-00-000</a>

In short, I freaked out, and got into the box, found the BaseHtml.php inheritance class
And wrote this code:
...
    public static function phone($phone, $options = [])
    {
        $options['href'] = 'tel:'.$phone;
        if (!isset($options['class'])) {
            $options['class'] = '';
        }
        return static::tag('a', $phone, $options);
    }
...

Works! BUT, I thought about such a problem. What if the project is updated via composer?
All the experience will be washed away, like a drawing on the sand by a sea wave, and nothing will work, and inexperienced programmers will swear and look for "where is this mysterious function?".
Hence the question, maybe I invented the wheel, and there is such a function, but I didn’t notice? Help me solve this issue, such a code is much more convenient than writing the phone number twice. A very necessary and useful item! Maybe the developers will implement this?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
dicem, 2019-05-11
@NikitaFedorov

Swiper is ideal https://github.com/nolimits4web/Swiper/
set all slides to opcaity: .5; and active slide opacity: 1
use text part as thumb:
https://github.com/nolimits4web/Swiper/blob/master...

S
Shohruh Shaimardonov, 2019-05-11
@joeberetta

There are a bunch of ready-made jQuery slider plugins

A
Alexander, 2017-09-01
@p0vidl0

The easiest option is to create your own class inherited from Html and use it throughout the project:

<?php
namespace app\helpers;
class Html extends \yii\helpers\Html
{
    public static function phone($phone, $options = [])
    {
        $options['href'] = 'tel:'.$phone;
        if (!isset($options['class'])) {
            $options['class'] = '';
        }
        return static::tag('a', $phone, $options);
    }
}

And then a few options:
  • Add a few more convenience methods and wrap it all up as a composer package, example .
  • Make a pull-request in yii2 or the same Kartik.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question