2

I have this code

- value="true"

- if (value)
  p yes #if the condition is true I want to insert a glyphicon. (<i class="fa fa-check-circle"></i>)
- else
  p no

How to convert it into condition? <i class="fa fa-check-circle"></i> : <i class="fa fa-times-circle"></i>

But this is causing error!

matt
  • 78,533
  • 8
  • 163
  • 197
Ajey
  • 7,924
  • 12
  • 62
  • 86
  • 1
    Do you want a version of that code that uses the ternary expression, or are you asking about inserting the `i` tag mentioned in the comment? Is this in Haml or Slim? What are `yes` and `no`? Do you mean to use `"true"` (a string) or did you mean `true` (the Boolean)? – matt Feb 28 '14 at 18:44
  • I want to print out yes and the icon in that para. And this is ruby slim, since there is no existing tag for slim, I mentioned as HAML cos the syntax is similar. – Ajey Feb 28 '14 at 18:46
  • String yes along with the icon into the HTML page? stuck with this for 2 hours now :( – Ajey Feb 28 '14 at 18:51
  • Should the "yes" be in a `

    ` element? If so should the `i` be inside it too?

    – matt Feb 28 '14 at 19:00
  • its working if I write if else statement, but I want to perform the same thing using ternary operator. i.e condition? : – Ajey Feb 28 '14 at 19:01
  • Why does it need to be a ternary? I don’t think that it is possible with a ternary using slim syntax, maybe if you use literal html. – matt Feb 28 '14 at 19:18
  • @Ajey, shouldn't my answer work? That is, add quotes around the HTML tags. – tobiasvl Feb 28 '14 at 19:30
  • @matt well it can be done using normal if/else but I prefer ternary because it takes less lines of code and serves the purpose. – Ajey Feb 28 '14 at 19:38

1 Answers1

4

Your first code can simply be replaced with this:

= value ? "yes" : "no"

Any lines starting with = are evaluated, and the resulting return value is inserted into the document after a call to escape_html.

Because you explained in your comments that you actually want HTML code to be inserted, you'll have to do this:

== value ? '<i class="fa fa-check-circle"></i>' : '<i class="fa fa-times-circle"></i>'
tobiasvl
  • 570
  • 4
  • 20
  • when I add quotes, it is being interpreted as string and the output is rather than the icon. – Ajey Feb 28 '14 at 19:37
  • @Ajey: Ahaaa, the single quotes escape the HTML (I thought this was Haml, but I see you changed the tags afterwards). Use double quotes instead? I've updated my answer. – tobiasvl Feb 28 '14 at 19:42
  • bang on mate! but how do I output the icon ? – Ajey Feb 28 '14 at 19:47
  • 1
    @Ajey: Updated again: Try == instead of =, as the latter calls `escape_html` and the former does not. – tobiasvl Feb 28 '14 at 19:49