2

So I'm playing with Mako on Pyramid and I'm trying to do inline if statements.

<li>${'<a href="#">Opinions</a></li>' if whichnav == 'opinions' else 'Opinions'}

Outputs:

<li>&lt;a href=&#34;#&#34;&gt;Opinions&lt;/a&gt;&lt;/li&gt;

Whereas:

% if whichnav =='opinions':
      <li><a href="#">Opinions</a></li>
% else:
      <li>Opinions</li>
% endif

Outputs correctly without escaping the HTML characters:

<li><a href="#">Opinions</a></li> 

I want to make my code as clean as possible so inline if statements are preferable, but I don't understand why HTML characters are escaped whereas using % they are not. Thanks!

1 Answer 1

2

Looks like your HTML is being escaped. What happens if you change your inline if to this:

${'<a href="#">Opinions</a></li>' if whichnav == 'opinions' else 'Opinions' | n}

(Edit: Put the | n to disable the filtering AFTER the conditional).

2
  • 1
    Yup, that works. Thanks for pointing out where to look!. Sep 28, 2011 at 0:51
  • Cool! I was gonna link that too, but then I got owned.
    – jathanism
    Sep 28, 2011 at 2:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.