Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel dynamic HTML tag name, based on if href is passed

Tags:

php

laravel

I have a laravel component

<x-link :href="{{ $href }}">
    some text
</x-link>

If I don't pass $href prop I want to print span tag instead, without duplicating code, but on tag directly.

Something like this that exists in Vue.js would be nice:

<component :is="href ? 'a' : 'span'">
    some text
</component>
like image 207
Marko Avatar asked Dec 07 '25 02:12

Marko


1 Answers

If you want to conditionally render Laravel components, use syntax:

<x-dynamic-component :component="$componentName">
    some text
</x-dynamic-component>

If you want to conditionally render HTML tags, just render tag name as a variable. (Maybe your editor will not parse and highlight it correctly, but functionally it should be ok.)

<{{ $tagName }}>
    some text
</{{ $tagName }}>

It's not possible to conditionally render Laravel component or HTML tag. So if you want to switch between <x-link> and <span>, just create custom component <x-span> and use it instead of <span>.

like image 59
user14967413 Avatar answered Dec 08 '25 15:12

user14967413



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!