Hi,
Based on the examples in the docs (see here ), I tried to make a simple “tooltip” kirbytag (hint.php) to be invoked by (hint: text: bla bla bla tip: lorem sup bla) with the following code:
<?php
kirbytext::$tags['hint'] = array(
'attr' => array(
'text',
'tip'
),
'html' => function($tag) {
$url = '#';
$text = $tag->attr('text');
$tip = $tag->attr('tip');
return '<a herf="" data-tooltip="' . $tag->attr('tip') . '">' . $text . '</a>';
}
);
It seems to works so far but I would like to have this tag applying also for a non-link (meaning without <a>xxx</a>
just for a simple text. I turn around the code but my very poor skills in php are not helpful.
Thanks for your help.
texnixe
February 25, 2017, 7:36am
2
I’d use an option link: true/false
and then depending on that value, either return a link or simple text.
BTW. Why do you use the text attribute, wouldn’t it be easier for the user to type the text directly after hint
, instead of leaving that blank?
(hint: Some hint text tip: some tip text link: false)
You would get the the content after hint with $tag->attribute('hint')
then.
Thanks for the swift answer.
I use the following code
<?php
kirbytext::$tags['hint'] = array(
'attr' => array(
'tip',
'link'
),
'html' => function($tag) {
$tip = $tag->attr('tip');
return '<a herf="" data-tooltip="' . $tag->attr('tip') . '"> ' . $tag->attr('hint') . '</a>';
}
);
But I don’t know how to use the option link: true/false as suggested. Is there some additional code to add in the above example ?
texnixe
February 25, 2017, 8:56am
4
You would have to use an if statement, something like this:
<?php
kirbytext::$tags['hint'] = array(
'attr' => array(
'tip',
'link'
),
// set the default to true
$link = $tag->attr('link');
if($link == '') {
$link = true;
}
'html' => function($tag) {
$tip = $tag->attr('tip');
if($link) {
return '<a herf="" data-tooltip="' . $tag->attr('tip') . '"> ' . $tag->attr('hint') . '</a>';
} else
return $tag->attr('hint');
}
);
the above code doesn’t work.
I got the following error:
syntax error, unexpected ';', expecting ')'
on the line $link = $tag->attr('link');
try to correct but unsuccessfully.
texnixe
February 25, 2017, 9:58am
6
I was on the wrong line, sorry:
<?php
kirbytext::$tags['hint'] = array(
'attr' => array(
'tip',
'link'
),
'html' => function($tag) {
// set the default to true
$link = $tag->attr('link');
if($link == '') {
$link = true;
}
$tip = $tag->attr('tip');
if($link) {
return '<a herf="" data-tooltip="' . $tag->attr('tip') . '"> ' . $tag->attr('hint') . '</a>';
} else
return $tag->attr('hint');
}
);