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

Spaces in partial block params causes params to be ignored #288

Closed
lougreenwood opened this issue Jan 28, 2018 · 5 comments
Closed

Spaces in partial block params causes params to be ignored #288

lougreenwood opened this issue Jan 28, 2018 · 5 comments
Assignees

Comments

@lougreenwood
Copy link

The PHP Code:

{{#> my_partial_block title="Some Title" sub_title="Some Sub Title"}}
    <p>Some Content</p>
{{/my_partial_block}}
<section>
	<h1>{{{title}}}</h1>
	<h2>{{{sub_title}}}</h1>

	<main>
		{{> @partial-block this }}
	</main>
</section>

The Issue:

I have a partial block where I would like to inject some params, e.g title and sub title.

It seems that when a space is included in a param for a partial block (e.g: {{#> my_partial_block title="Some Title"}}), then the param/variable is skipped in the actual partial block (e.g: <h1>{{{title}}}</h1> is rendered as <h1></h1>, I expect <h1>Some Title</h1>.

I'm running:

  • PHP 7.1
  • lightncandy 1.2.1

Flags:

LightnCandy::FLAG_RUNTIMEPARTIAL | LightnCandy::FLAG_THIS | LightnCandy::FLAG_SPVARS | LightnCandy::FLAG_ERROR_LOG | LightnCandy::FLAG_ERROR_EXCEPTION | LightnCandy::FLAG_ERROR_SKIPPARTIAL | LightnCandy::FLAG_PARENT | LightnCandy::FLAG_ELSE | LightnCandy::FLAG_NAMEDARG
@lougreenwood
Copy link
Author

Just am update. I'm able to temporarily work around this by substituting the spaces for &nbsp; - however this means that any strings which passed this way don't get any word wrapping (because it's just&nbsp;one&nbsp;long&nbsp;string... :(

Also, it seems that simply supplying any partial block param in the template with spaces breaks any old partial block...

@lougreenwood
Copy link
Author

Also, here's an example where the param with space breaks things:

<?php

require('./vendor/autoload.php');
use LightnCandy\LightnCandy;

// The Template:
$template = <<<VAREND
<body>
    {{#> test_modal title="My&nbsp;Title"}}
        <p>With `&nbsp` replacing spaces</p>
    {{/test_modal}}

    {{#> test_modal title="My Title"}}
        <p>With broken title/p>
    {{/test_modal}}
</body>
VAREND;

// Helpers:
$helpers = array(
  'foo' => function () {
    return 'Hello World';
  },
);

// Partials:
$partials = array(
  'test_modal' => '<div class="some_modal"><h1>{{{title}}}</h1><p>Hello from test_modal</p>{{> @partial-block }}</div>',
);

$phpStr = LightnCandy::compile($template, array(
  // Used compile flags
  'flags' => LightnCandy::FLAG_RUNTIMEPARTIAL | LightnCandy::FLAG_THIS | LightnCandy::FLAG_SPVARS | LightnCandy::FLAG_ERROR_LOG | LightnCandy::FLAG_ERROR_EXCEPTION | LightnCandy::FLAG_ERROR_SKIPPARTIAL | LightnCandy::FLAG_PARENT | LightnCandy::FLAG_ELSE,
  'helpers' => $helpers,
  'partials' => $partials,
));

echo "Generated PHP Code:\n$phpStr\n";

// Input Data:
$data = array(
  'qoo' => 'moo'
);

// Save the compiled PHP code into a php file
file_put_contents('render.php', '<?php ' . $phpStr . '?>');

// Get the render function from the php file
$renderer = include('render.php');

echo "Result:\n" . $renderer($data);
die();
?>

@lougreenwood
Copy link
Author

Some more info:

  • If I use {{#>_MyPartial title="[My Title]"}}, the space doesn't break the template, but renders as [My Title].
  • If I use {{&title}} in the partial block template, it renders correctly.

@zordius
Copy link
Owner

zordius commented Mar 17, 2018

Sorry for the long time waiting, will check on this.

@zordius
Copy link
Owner

zordius commented Mar 17, 2018

{{#> my_partial_block title="Some Title" sub_title="Some Sub Title"}} should be compiled with LightnCandy::FLAG_ADVARNAME (enable advanced variable parsing) and LightnCandy::FLAG_NAMEDARG (enable named argument feature) flag . Change the option then the issue should be fixed:

  $phpStr = LightnCandy::compile($template, array(
    // Used compile flags
-   'flags' => LightnCandy::FLAG_RUNTIMEPARTIAL | LightnCandy::FLAG_THIS | LightnCandy::FLAG_SPVARS | LightnCandy::FLAG_ERROR_LOG | LightnCandy::FLAG_ERROR_EXCEPTION | LightnCandy::FLAG_ERROR_SKIPPARTIAL | LightnCandy::FLAG_PARENT | LightnCandy::FLAG_ELSE,
+   'flags' => LightnCandy::FLAG_RUNTIMEPARTIAL | LightnCandy::FLAG_THIS | LightnCandy::FLAG_SPVARS | LightnCandy::FLAG_ERROR_LOG | LightnCandy::FLAG_ERROR_EXCEPTION | LightnCandy::FLAG_ERROR_SKIPPARTIAL | LightnCandy::FLAG_PARENT | LightnCandy::FLAG_ELSE | LightnCandy::FLAG_ADVARNAME | LightnCandy::FLAG_NAMEDARG,
    'helpers' => $helpers,
    'partials' => $partials,
  ));

Or, you can just use the alias LightnCandy::FLAG_HANDLEBARS which means LAG_THIS + FLAG_PARENT + FLAG_HBESCAPE + FLAG_ADVARNAME + FLAG_SPACECTL + FLAG_NAMEDARG + FLAG_SPVARS + FLAG_SLASH + FLAG_ELSE + FLAG_RAWBLOCK

@zordius zordius closed this as completed Mar 17, 2018
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