Working with Blade and ACF

I’m new to Blade and wondering where should I have the logic for creating a phone link - could you guys help me?

I want to use @nathobson code and display an anchor with a phone number in a few different partials, the phone is stored on an ACF options page and has to be sanitized before inserting it to an anchor.

$number = get_field('phone', 'option');
$sanitized = preg_replace("/[^0-9]/", "", $number);
return '<a href="tel:+1'.$sanitized.'">Call Me</a>';

Where should I have this code, and how should I pass the results to a view?

A nice and easy way to do this would be with a controller.

In app/Controllers/App.php, inside the App class, add something like:

public function phone() {

  // Initialize the return variable
  $return = '';

  // Get the phone number from the ACF options field
  $number = get_field(‘phone’, ‘option’);

  // Sanitize the phone number!
  $return = '+1' . preg_replace("/[^0-9]/", “”, $number);

  // Always return
  return $return;
}

Then in your blade partial:

@if($phone)
  <a href="tel://{{ $phone }}">Call Me</a>
@endif

Thanks, but going further - Is there a point where I put any HTML in a function?
What if I want to make it more complex like for example

<a href="tel://{{ $phoneSanitized }}" aria-label="our phone {{ $phoneNormal }}">{{ $phoneNormal }}</a>

Because since this anchor will be displayed in a few partials writing this everywhere doesn’t seem DRY.

I would normally make a function that returns the whole anchor including the HTML and add it to the partial using

@php echo function() @endphp

but after reading a bunch of topics here I don’t think you guys do it like that, and this one line of code seems too small to put it in a separate partial.

Hi, @mefusia,
If the phone number component is truly as reusable as you say, I would make it into its own partial. I prefer not to have HTML outside of my templates.

It’s totally your call. If writing HTML with the function makes sense to you, then go for it.

But even then, I would write it all as a controller function as follows:

public function phone() {

  // Initialize the return variable
  $return = '';

  // Get the phone number from the ACF options field
  $number = get_field(‘phone’, ‘option’);

  // Sanitize the phone number!
  $output = '+1' . preg_replace("/[^0-9]/", “”, $number);

  // Only output HTML if there was a number to work with
  if(isset($number) {
    $return = "<a href=\"tel://" . $output . "\">Call Me</a>";
  }

  // Always return
  return $return
}

And then add it to your template like this:

{{ $phone }}

That way you’re keeping your PHP and logic out of your templates.

1 Like

Thank you :slight_smile:

Also worth thinking about controller traits if this is a component that will be used across multiples templates/contexts.

More info: https://github.com/soberwp/controller#using-components

This way, you can have a single view/components but also define the logic in a single place.

1 Like

This topic was automatically closed after 42 days. New replies are no longer allowed.