0

I'm using an if statement in a layout to check if a layout variable contains any content.

{if layout:sidebar}
  <div class="sidebar">
    {layout:sidebar}
  </div>
{/if}

This works well if {layout:sidebar} hasn't been set at all by the template that is using this layout, but sometimes {layout:sidebar} will have spaces or newline characters in it, but nothing else (because contents get set conditionally in the templates that use this layout).

Things I've tried:

{!-- Will display the sidebar if there are newlines/spaces in layout:sidebar --}
{if layout:sidebar}
  <div class="sidebar">
    {layout:sidebar}
  </div>
{/if}

{!-- Same result as above...which makes sense --}
{if layout:sidebar != ''}
  <div class="sidebar">
    {layout:sidebar}
  </div>
{/if}

{!-- Will never display the sidebar --}
{if {exp:streeng trim="both"}{layout:sidebar}{/exp:streeng} != ''}
  <div class="sidebar">
    {layout:sidebar}
  </div>
{/if}

{!-- Will never display the sidebar --}
{if {exp:streeng find='SPACE|NEWLINE' trim='' }{layout:sidebar}{/exp:streeng} != ''}
  <div class="sidebar">
    {layout:sidebar}
  </div>
{/if}

{!-- Breaks if {layout:sidebar} contains a double quote --}
{if "{exp:streeng find='SPACE|NEWLINE' trim='' }{layout:sidebar}{/exp:streeng}" != ''}
  <div class="sidebar">
    {layout:sidebar}
  </div>
{/if}

{!-- Breaks if {layout:sidebar} contains a single quote --}
{if '{exp:streeng find='SPACE|NEWLINE' trim='' }{layout:sidebar}{/exp:streeng}' != ''}
  <div class="sidebar">
    {layout:sidebar}
  </div>
{/if}

Other than combing through templates that use this layout and set layout:sidebar and removing any newlines or spaces, what is the best way to conditionally include or exclude the sidebar in this scenario?

1
  • You could try adding parse='inward' to the if statement May 20 '16 at 1:39
0

How about using streeng to replace the spaces / newline characters before you test.

If you use find in streeng and put no replace value streeng will delete the search terms from its object if it finds them there. You can use pipe to search for multiple values, and streeng has special terms for spaces and newline characters ... so something like:

{if {exp:streeng find="SPACE|NEWLINE" trim="both"}{layout:sidebar}{/exp:streeng} != ''}
  <div class="sidebar">
    {layout:sidebar}
  </div>
{/if}

This should get rid of any spaces and newlines in the layout variable before you test to see if it is empty.

HTH

2
  • That helped me get a little further. Thanks @Gavin. Now I'm finding that I have to wrap the exp:string expression in quotes for the if statement to work, which is fine except that it breaks if there are any quotes in {layout:sidebar}. So I'm still stuck...
    – Divey
    May 20 '16 at 13:54
  • Is this for the exp:streeng in the {if} then you could simply remove quotes from the {layout:sidebar} prior to testing - use find="SPACE|NEWLINE|QUOTE" in the streeng`... - see documentation here github.com/caddis/streeng HTH May 20 '16 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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