-2

From what I can gather from How can I disable an anchor element in mustache file using YML configuration? I should be able to have a link shown depending on whether the href attribute exists or not.

I have the following in my script,

 <a {{#href}} href= {{Link}} {{/href}}> url </a>

The issue i am having is that the word url appears as plain text i.e. without the link. {{Link}} holds a valid link.

Edit:

If i use the below script the hyperlink is rendered,

<a href={{Link}}> url2 </a>

But there are instances where {{Link}} would be null, in those instances i do not want url2 to be displayed, which is why i am trying to use {{#href}} and {{/href}}.

What could i be missing ?

Bisoux
  • 532
  • 6
  • 18
  • 1
    Maybe show us the rendered HTML in case I'm wrong about something. – isherwood Dec 09 '22 at 15:26
  • 1
    @isherwood it is supposed to hide/show the url depending on the value of {{Link}}. I have attached the question where from i got the inspiration. – Bisoux Dec 09 '22 at 15:29
  • Ok. I'd still like to see the rendered output. – isherwood Dec 09 '22 at 15:30
  • @isherwood *url* shows up without the hyperlink i am expecting it point to, just plain text – Bisoux Dec 09 '22 at 15:34
  • 1
    So you said. Why won't you show us the output? It's not a big ask. – isherwood Dec 09 '22 at 15:35
  • Typo: You said the name of the variable is `Link` but before trying to render `Link` you are testing for `href` instead of testing for `Link`. (Do remember to provide a [mcve] in future, this would have been easier to spot if we'd seen the data you were putting into your template). – Quentin Dec 09 '22 at 15:37
  • @Quentin that is not the case, the test is carried out using the {{#href}} and closing {/href}} tags. The variable that it is being tested for can technically be named anything. – Bisoux Dec 09 '22 at 15:41
  • @Bisoux — Then show us a [mcve] with the input data. – Quentin Dec 09 '22 at 15:41
  • https://jsbin.com/nocodufoge/1/edit?html,console — I can't reproduce the problem. – Quentin Dec 09 '22 at 15:49
  • not quite @Quentin, first the rendered link from your script is not exactly a hyperlink, granted the special characters could probably handled by applying *&*. I also removed *Link* from data, i still see *url* appearing, which should not be the case – Bisoux Dec 09 '22 at 19:50

1 Answers1

-1

Thanks to inspiration from this post How do I accomplish an if/else in mustache.js? , an if/else (sort of) in mustache (inverted sections) the below script works,

{{#Link}}
  <a href={{Link}}> url2 </a> 
{{/Link}} 
{{^Link}} 
{{/Link}}

which could be read as if Link has a non null value render the link otherwise do something else, in my case nothing.

Bisoux
  • 532
  • 6
  • 18