0

I'm returning a string body_class to my view. I can't seem to put in a condition without breaking it:

<body tal:condition="body_class" class="${body_class}">
<body tal:condition="not body_class">

Kinda works. It outputs the body class but the rest of the template doesn't work. I'm looking for a solution that puts in the body class if the string exists, otherwise leaves it out.

Dunno what I'm doing wrong.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
izolate
  • 1,590
  • 4
  • 22
  • 35

2 Answers2

0

I suppose your talking about a browser view (BorwserView).

Something like that should work if body_class is a method of your BrowserView:

<body tal:condition="view/body_class" class="${view/body_class}">
...
</body>
<body tal:condition="not: view/body_class">
...
</body>
Mathias
  • 6,777
  • 2
  • 20
  • 32
  • Don't think I'm using that. In my views function I `return {body_class: 'my class'}`, and that's what I'm trying to check the existence of. – izolate Sep 20 '13 at 15:07
  • If possible, I'd like to avoid having 2 instances of the `` tag. – izolate Sep 20 '13 at 15:20
0

You don't want to use tal:condition here; you are toggling the whole element on or off, including the contents.

You only want to set the class attribute:

<body tal:attributes="class: body_class">

This works fine for empty strings, you'll just get an empty class element; if body_class is None, the attribute will be omitted altogether.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343