alexeyrybak/blitz

How to remove \n in tpl if a variable is empty?

Closed this issue · 4 comments

With the following tpl:

{{ $first }}
{{ $middle }}
{{ $last }}

And the following php:

<?php
$infos = array('first' => 'Firstname', 'middle' => '', 'last' => 'LASTNAME');
$T = new Blitz('test.tpl');
$T->display($infos);

When I display the source code of the page:

Firstname

LASTNAME

When {{ $middle }} is empty, how can I remove the second line without update my tpl to this unreadable file:

{{ $first }}{{ $middle }}{{ $last }}

Any idea?
Thanks.

First, it's not that unreadable. HTML is not very well readable in general, you know ;) Second I'd say that would be something like

'Firstname', 'middle' => '', 'last' => 'LASTNAME'); $body = "{{ \$first }} {{ IF \$middle }} {{ \$middle }} {{ END }} {{ \$last }}"; $T = new Blitz(); $T->load($body); $T->display($infos); ~/blitz> php space.php Firstname LASTNAME IF/contexts work differently: blitz "eats" newlines around these tags but not around variables, as sometimes you _need_ this space/newline for some markups. Hope this helps.

In my example it's readable but with 20 variables… ;-)
Ok, it works well with "if / else".
Thanks.

With 20 variables and IF/ELSE this will lead to even something more unreadable, I guess. Anyway, this also works with blocks (BEGIN-tags) - it's not your case (you need to check variable, not hash/array) -but just to explain where also this useless-empty-space removal works.

Yes I already use BEGIN/END for other cases ;-) Thanks for your help.