Get link data from databoject type link

Hello there.

trying to output a link from a dataobject. See you can see how it is set up

link2

In template I try to get the data with
<?php echo $slider->getBtnlink();?>

But the field has no output. Link is set. Do you have to handel links different?

thanks!

Hi @sandra-walker,

Echoing btnlink doesn’t work, because the method getBtnlink() returns an object and doesn’t render any HTML output. I usually add a twig macro (another reason to switch to Twig already :sweat_smile:), which handles the rendering:

{% macro link(link, editmode = false, class = null) %}
    {% if editmode %}
        {{ link|raw }}
    {% else %}
        {% if not link.empty %}
            {% set target = link.target is not empty ? link.target %}
            {% set rel = target is not empty and target == '_blank' ? 'noreferrer noopener' %}

            <a href="{{ link.href }}" {% if class is not empty %}class="{{ class }}"{% endif %}
               {% if link.title is not empty %}title="{{ link.title }}"{% endif %}
               {% if target is not empty %}target="{{ target }}"{% endif %}
               {% if rel is not empty %}rel="{{ rel }}"{% endif %}>
                {{ link.text }}
            </a>
        {% endif %}
    {% endif %}
{% endmacro %}

With that I can render a button link like this:

{% import 'Macros/editables.html.twig' as editable %}

{{ editable.link(my_object.link, editmode, 'my-button-class') }}

HTH!