Skip to content
This repository has been archived by the owner on Jan 26, 2019. It is now read-only.

Redundant newlines #154

Open
jcubic opened this issue Aug 27, 2016 · 10 comments
Open

Redundant newlines #154

jcubic opened this issue Aug 27, 2016 · 10 comments
Labels

Comments

@jcubic
Copy link

jcubic commented Aug 27, 2016

I have newline issue, I have header helpers that create http headers.

        $handlebars->addHelper('header', function($template, $context, $arg, $source) {
            $para = $template->parseArguments($arg);
            header($para[0]);
        });

If I have template like this:

{{header "Content-Type: text/plain"}}
{{#each foo}}
    {{this}}
{{/each}}

I have redundant newline at the beginning and when I'm using this one:

{{header "Content-Type: text/plain"}}{{#each foo}}
    {{this}}
{{/each}}

I have newline at the begining of each iteration (in fact I have two newlines, one expected).

@JustBlackBird
Copy link
Contributor

There is a special syntax {{~smth~}} for whitespace removing.

Look at tests for details.

@jcubic
Copy link
Author

jcubic commented Aug 28, 2016

This don't remove newlines, If I have first template I don't have any newlines and spaces between items in #each and if I use second one I have expected newlines at the end of the items in #each but in both cases I have newline at the beginning of the template.

@jcubic
Copy link
Author

jcubic commented Aug 28, 2016

Also if I use

{{~header "Content-Type: text/plain"~}}
{{#each foo}}
    {{this}}
{{/each}}

I don't get newline at the begining but I also don't get spaces before {{this}}. I got this:

lorem
    ipsum
    dolor
    sit
    amet

@JustBlackBird
Copy link
Contributor

JustBlackBird commented Aug 28, 2016

Please, provide a test case containing:

  1. the template you use
  2. the data your template is running on
  3. the expected result
  4. the real result.

In this case we could talk about your use case in more details.

Be aware: nobody (except you) has neither header nor sql helpers, so you should not use them in a provided test cases.

@jcubic
Copy link
Author

jcubic commented Aug 28, 2016

I have code like this:

$handlebars->addHelper('bar', function($template, $context, $arg, $source) {
});
$template = '{{bar "baz"}}
{{#each foo}}
    {{this}}
{{/each}}';
echo $handlebars->render($template, array(
    'foo' => array(
        '1', '2', '3', '4'
)));

the result is:


    1
    2
    3
    4

extra newline at the beginning,

expected result:

    1
    2
    3
    4

no extra newline at the beginning

it don't happen if I use bar helper as block (#bar).

@jcubic
Copy link
Author

jcubic commented Aug 28, 2016

Same heppen if you use this template:

{{if "baz"}}
{{#each foo}}
    {{this}}
{{/each}}

@JustBlackBird
Copy link
Contributor

Instead of

{{bar "baz"}}
{{#each foo}}
    {{this}}
{{/each}}

try to use

{{bar "baz"}}
{{~#each foo}}
    {{this}}
{{/each}}

(notice the tilde position)

@jcubic
Copy link
Author

jcubic commented Aug 28, 2016

yes it works, thanks. but maybe this can be changed in the library to not introduce new line if empty non block helper is used, same as block helper.

@JustBlackBird
Copy link
Contributor

You're welcome.

You see, Handlebars.php tries to work in the same way as Handlebars.js. As I know, JS implementation introduces new line for empty non block helpers. So PHP implementation should follow this behavior. There is little we can do here.

@jcubic
Copy link
Author

jcubic commented Aug 30, 2016

I have another problem with custom helpers I have template like this:

{{#each foo}}
    {{#each bar}}
        {{~../id}}: {{content}}
        {{~helper~}}
        {{#if $id == 2}}
            id = 2
        {{/if}}
    {{/each}}
{{/each}}

and I end up with this result:


1: lorem

2: ipsum

            id = 2
3: dolor

4: sit

5: amet

and if I add {{content~}} I ended up with this:

1: lorem2: ipsumid = 2
3: dolor4: sit5: amet

adding ~ to beginning of if also didn't help

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

No branches or pull requests

2 participants