开发者

Inline if statement escapes HTML characters

开发者 https://www.devze.com 2023-04-09 02:23 出处:网络
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\'}

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;&g开发者_C百科t;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!


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

0

精彩评论

暂无评论...
验证码 换一张
取 消