URL as Tag return statement

Dear all,

while I have been using the amazing Kirby for a while, I only recently started looking into extending it.

I intend to write a tag, which outputs a URL in its return statement. along the lines of:

<?php
kirbytext::$tags['google'] = array(
  'html' => function($tag) {
    $html = 'https://www.google.com';
    return $html;
  }
);
?>

However, it seems that the link is always replaced to

<a href="https://www.google.com">https://www.google.com</a>

While this is generally nice, as users can avoid the (link: …) tag in markdown, it is a bit annoying when one wants to output Javascript in a Kirby Tag.

Is this functionality intended? How can I use URL in tags without them being replaced as valid HTML? Am I missing something here?

Turning off URL rewriting in the config only seems to turn off the automatic URL conversion in user entered markdown text, back-end functions such as tags are not affected:
http://getkirby.com/docs/cheatsheet/options/rewrite

I don’t really understand what you want to do. What output do you want from your Kirbytag?

All of this has nothing to do with URL Rewriting. What URL rewriting does is make nice URLs like “http://yourdomain.de/blog/your-article” instead of “http://yourdomain.de/index.php/blog …” etc. i.e., without the index.php bit in it.

Oh ok, my bad. In that case the option really has nothing to do with it.

As a minimal working example, I am looking to write a Kirby tag, which allows me to return a plain URL (without <a href ...) into HTML. Such as the one shown above. In my case, the text which is returned and entered into the page is no www. google. com (without spaces, forum doesn’t allow me to post different), but
<a href="https://www.google.com">https://www.google.com</a>

I am building a Kirby tag that outputs Javascript. In this case I of course do not want the URL that I return from my tag to be replaced with a proper HTML link, but rather have the raw string, as I defined it.

Basically the minimal working example in the first post should output text, not a link.

I hope this helps to clear things up.

If you want the tag to return JavaScript, then the minimal working example is a <script> tag, isn’t it? :smiley:
Because if you return a <script> tag (or any HTML tag), the Markdown parser won’t mess with it.

Well, yes. Of course this is not JavaScript. Maybe the MWE was too minimal.

While trying to construct an example closer to the reality of my problem, I actually managed to find the error: It seems Kirby expects that the return from a tag is a single HTML “unit” and in itself complete. If that is not the case, it is enclosed in <p>...</p> and the Markdown parser might run.

Let me explain in more detail by example:

Example 1 (not working):

Tag:

<?php
kirbytext::$tags['google'] = array(
  'html' => function($tag) {
    $html = '<link rel=stylesheet href=assets/style.css>';
    $html .= '<script type=text/javascript src=assets/scriptABC.js></script>';
    $html .= '<script type=text/javascript>';
    $html .= '$(document).ready(function() {';
    $html .= 'functionABC(\'https://www.google.com\');';
    $html .= '});';
    $html .= '</script>';
    return $html;
  }
);
?>

Output:

<p>
<link rel=stylesheet href=assets/style.css>
<script type=text/javascript src=assets/scriptABC.js></script>
<script type=text/javascript>
   $(document).ready(
     function() {
        functionABC('<a href="https://www.google.com">https://www.google.com</a>');
     }
   );
</script>
</p>

Example 2 (working):

Tag:

<?php
kirbytext::$tags['google'] = array(
  'html' => function($tag) {
  	$html = '<div>';
  	$html .= '<link rel=stylesheet href=assets/style.css>';
  	$html .= '<script type=text/javascript src=assets/scriptABC.js></script>';
  	$html .= '<script type=text/javascript>';
        $html .= '$(document).ready(function() {';
        $html .= 'functionABC(\'https://www.google.com\');';
        $html .= '});';
        $html .= '</script>';  
	$html .= '</div>';
        return $html;
  }
);
?>

Output:

<div>
<link rel="stylesheet" href="assets/style.css">
<script type="text/javascript" src="assets/scriptABC.js"></script>
<script type="text/javascript">
    $(document).ready(
        function() {
            functionABC('https://www.google.com');
        }
    );
</script>
</div>

Solution:

Add a <div>...</div> (or some similar grouping) around everything you want to return from a Kirby tag.

I think that clearly answers the question now and works as documentation for others as well, in case they stumble over this question.

Thank you all very much for your time!

1 Like

Generally the issue is that you are returning multiple HTML tags from your code. The following works fine for example:

<?php
kirbytext::$tags['google'] = array(
  'html' => function($tag) {
    $html = '<script type=text/javascript>';
    $html .= '$(document).ready(function() {';
    $html .= 'functionABC(\'https://www.google.com\');';
    $html .= '});';
    $html .= '</script>';
    return $html;
  }
);

I recommend including the JS and CSS files one time in your footer anyway, not every time the tag is called. Otherwise you might have multiple instances of the <link> and <script> tags on your page.

Indeed, I noticed that this works while creating the examples above. This is how I came up with the solution.

Thanks for the hint. It is certainly not ideal to include all scripts here, I might have to convert the tag into a full size plugin to accomplish a separation of actual code and includes, or add an additional tag to be added to every header, both not ideal for the user. While the resulting tag could be used more than once on a single page, this is typically not the case, due to its application. Anyway, I will consider.

BTW: Here is an early version of the tag in action: https://www.mundhenk.org/publications

Thanks again!