Search code examples
htmlmustache

Issue with rendering a hyperlink using mustache


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 ?


Solution

  • 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.