Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

whitespace control #4

Closed
Mrhyuan opened this issue Sep 10, 2014 · 11 comments
Closed

whitespace control #4

Mrhyuan opened this issue Sep 10, 2014 · 11 comments

Comments

@Mrhyuan
Copy link
Contributor

Mrhyuan commented Sep 10, 2014

better to remove all blank lines inside the for tags. (not sure if it exists in other tags as well)

for example:
delimiters: '<{', '}>'

template source:

<ul>
<{for $key => $value in $data}>
    <li><{$key}>:<{$value}></li>
<{/for}>
</ul>

actual output:

<ul>

    <li>1:111</li>

    <li>2:222</li>

</ul>

expected:

<ul>
    <li>1:111</li>
    <li>2:222</li>
</ul>

whitespaces before and after the block tags should be suppressed:)

@bugadani
Copy link
Owner

To be honest I would instead add manual whitespace control for the tag delimiters. E.g. similar to Twig, {- would eat all whitespaces before the tag, -} after the tag. Also it would be possible to make the tag closing delimiter eat one newline character but automatic strategies may not work well. Needs further thought on my part.

@bugadani
Copy link
Owner

Implemented using - suffix/prefix for the tag opening and closing delimiters.

E.g. <li> {- $whatever -} </li>

Still thinking about removing only the extraneous newlines.

@Mrhyuan
Copy link
Contributor Author

Mrhyuan commented Sep 10, 2014

Many thanks. Is it better to use \n instead of \s? Most people like formatted html. Whitespace control is really a hard thing.

@bugadani
Copy link
Owner

Please explain, what you mean by \n instead of \s. We could change the pattern but I believe that \s is a good enough choice to trim with.
It would be possible to create a modifier to trim newlines only, e.g. with {+ +} delimiters but I'm not sure if it is any good. More possibilities usually mean that code and templates will get harder to read.

@Mrhyuan
Copy link
Contributor Author

Mrhyuan commented Sep 10, 2014

Totally agree.

For \n instead of \s, use my example above again.

source:

<ul>
<{-for $key => $value in $data-}>
    <li><{$key}>:<{$value}></li>
<{-/for-}>
</ul>

output(should be, not tested):

<ul><li>1:111</li><li>2:222</li></ul>

expected:

<ul>
    <li>1:111</li>
    <li>2:222</li>
</ul>

more readable, is it?

@bugadani
Copy link
Owner

Well, if you expect the secound output, simply modify your template to this (note the missing - signs):

<ul>
<{-for $key => $value in $data }>
    <li><{$key}>:<{$value}></li>
<{-/for}>
</ul>

If you'd modified the \s to \n, your output would simply be everything in a one-line mess.

@Mrhyuan
Copy link
Contributor Author

Mrhyuan commented Sep 10, 2014

Thanks. Didn't realize it:( my bad. By the way, how about adding it as an option, like auto-trim or auto-strip? Type one more hyphen - is very annoying.

@bugadani
Copy link
Owner

I could add an option to trim the first newline after the closing delimiter, just like PHP does it. Also I could add an invert-switch to make the hyphen not to trim whitespaces and the default delimiter to trim.
I'll try to come up with a sensible option (or set of oftions) in the near future. Thanks for the feedback.

@bugadani
Copy link
Owner

From now on you'll be able to:
1: set different delimiters to the whitespace controlling, e.g. you can swap { and {- so that the hyphened version does not strip but the simple brace does.
2: there is a new option called tag_consumes_newline that can be used to enable stripping one newline character after each tag.
I believe this should suffice but feel free to suggest modifications.

@Mrhyuan
Copy link
Contributor Author

Mrhyuan commented Sep 11, 2014

Thanks!!!

For tag_consumes_newline option, its better to skip include (maybe block as well) tag.

header:

<h1>blabla..</h1>

example:

<body>
{include 'header'}
</body>

output:

<body><h1>blabla..</h1>
</body>

expected:

<body>
<h1>blabla..</h1>
</body>

@bugadani
Copy link
Owner

First of all, your example is wrong. :-) Actual output would be:

<body>
<h1>blabla..</h1></body>

Also it depends. It is recommended to put a newline character at the end of each file. That would mean that the include tag strips the newline but the included template adds it back.
I was thinking that generally speaking tags that output something should not strip while control tags (the ones that don't output) should. This is possible to do, but extremely messy. I would suggest putting two newlines (or a space after the tag) where it matters.

Also, block, define and include are especially problematic. When a larger template (one with more than 1 lines) is included, it will most likely be indented in a wrong way. Using blocks, the first line would be at the right indentation level, the rest would not and this is a problem that makes it really hard to make nice html output.

{# template index #}
<div>
    { include 'other_template' }
</div>


{# template other_template #}
<ul>
    <li>list element</li>
</ul>

Output:

<div>
    <ul>
    <li>list element</li>
</ul>
</div>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants