Link inside Custom Kirbytag

kirbytag

#1

I have this Problem, that I have created a new Kirbytag which was created so the users can easily create Call-To-Actions without the knowledge of HTML/CSS. But I am running against a wall when trying to use a link inside the Kirbytag (Kirbytag inside Kirbytag).

This is the Tag which is used for Standard Text insert:
(cta: Call-To-Action Text)

And this is the one with the Link inside the KirbyTag
(cta: Call-To-Action Text with (link: /contact text: Link))

Kirbytag:

kirbytext::$tags[‘cta’] = array(
‘attr’ => array(
),
‘html’ => function($tag) {

$article = $tag->attr('cta');
return '<div class="' . 'cta-content' . '">' . $article . '</div>';

}
);

Does anyone know how i would manage to do this?


#2

You can’t nest Kirbytags, I suggest you add the link as an option within your own tag.

(cta: Call-to-action-text link: /contact text: Link)

#3

Thanks for your Help!


#4

But that also would deny me using Google Event Tracking or is there a way around this (I dont want to hardcode it since the Tracking has to be dynamic)?

If I did this:
(cta: Call-to-action-text link: onclick="gtag('event', 'click', {'event_category': 'email','event_label': 'jobs'});" text: Link)

I would run into problems.


#5

I’m afraid I don’t quite understand. What is the difference between adding the link with the link tag or adding it directly in your custom tag? Of course, you would have to modify your tag to include the link?


#6

Sorry I copyed my Kirbytag wrong:

  (cta: CallToAction InfoText link: /contact tracking: onclick="gtag('event', 'click', {'event_category': 'email','event_label': 'jobs'});")

If i did this the output on the Website would break just like with the nested Kirbytags.

The reason I dont want to direcly add the Tracking inside the Kirbytag (in the tags.php) is because on One Page of the Website I might want to create a Call-To-Action linking to the contact Page with a Individual Tracking Code.
Example:
(cta: CallToAction InfoText link: /contact tracking: onclick="gtag('event', 'click', {'event_category': 'email','event_label': 'jobs'});")

But at the same time I want to have a Call-To-Action on a different page and with a different Tracking Code.
Example:
(cta: CallToAction InfoText link: /profile tracking: onclick="gtag('event', 'click', {'event_category': 'email','event_label': 'profile'});")

If the Tracking Code was hardcoded this would not be possible.

My current tags.php:

kirbytext::$tags['cta'] = array(
'attr' => array(
  'link',
  'tracking
  ),
  'html' => function($tag) {

  $article = $tag->attr('cta');
  $link    = $tag->attr('link', 'cta');
  $tracking    = $tag->attr('tracking', 'cta');
  return '<a ' . $tracking . ' href="'. $link . '"><div class="' . 'cta-content' . '">' . $article . '</div></a>';

  }
);

#7

I see, this is due to the fact that you can’t use parenthesis inside Kirbytags without escaping them, see this post for a possible workaround.

However, since the code is always the same only with a different label, you might as well include those as attributes:

(cta: CallToAction InfoText link: /contact event-label: jobs )

etc. if you need categories as well.


#8

You are completly right… :smiley: Thanks alot like always!