Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add support for @data variables#3

Merged
sam-osborne merged 8 commits into
masterfrom
unknown repository
Jun 8, 2020
Merged

Add support for @data variables#3
sam-osborne merged 8 commits into
masterfrom
unknown repository

Conversation

@ghost

@ghost ghost commented May 20, 2020

Copy link
Copy Markdown

This PR adds support for data variables @first, @last, @index and @key within #each (outlined in the latest documentation). These data variables can be used as variables or within the template directly.

Given the following template and data:

{{#each data}}{{#if @first}}FIRST: {{/if}}{{this}}<br>{{/each}}
'data' => ['apple', 'banana', 'carrot', 'zucchini']

The output will be

FIRST: apple<br>banana<br>carrot<br>zucchini<br>

Given the following template and the data above:

{{#each data}}{{@first}}: {{this}}<br>{{/each}}

The output will be

true: apple<br>banana<br>carrot<br>zucchini<br>

Data variables also support relative referencing within multiple #each statements.
Given

{{#each data}}{{#each this}}outer: {{@../first}},inner: {{@first}};{{/each}}{{/each}}
'data' => [['apple', 'banana'], ['carrot', 'zucchini']]

The output will be

outer: true,inner: true;outer: true,inner: false;outer: false,inner: true;outer: false,inner: false;

See the attached tests for more scenarios.

*
* @return mixed Last index
*/
public function lastIndex()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you dropping these?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, forgot to drop a comment here. These are being removed because the only usage has been replaced with the variable retrieval within Template.php. The data variables support retrieving the last index ({{@index}}) or relative pathing ({{@../index}}) so we can use the same function for both situations.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same applies for the lastKey function below.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, I wonder if we run into any issues by virtue of changing the public API with this though?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I didn't think about that so it's a good call out. I've updated the code to put the new data variables behind a feature flag. It doesn't quite match how Handlebars JS works but it solves the problem of opting-in to the new behavior.

To enable data variables, the user must pass in enableDataVariables when constructing the Handlebars instance. By default this is set to false.

$engine = new \Handlebars\Handlebars(array(
  'enableDataVariables'=> true,
));

Comment thread src/Handlebars/Context.php Outdated
* @param bool $enableDataVariables Enables data variables (default: false)
*/
public function __construct($context = null)
public function __construct($context = null, $enableDataVariables = false)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to think ahead here, maybe this second parameter should be an array of options like you did over on the main class? That way if stuff crops up in the future we're not just endlessly tacking on parameters and maybe have less API breakage that way.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea - I've updated the code to reflect the suggestion.

Comment thread src/Handlebars/Context.php
Comment thread src/Handlebars/Context.php
Comment thread src/Handlebars/Context.php Outdated
Comment thread src/Handlebars/Helpers.php Outdated
Comment thread src/Handlebars/Context.php Outdated

@sam-osborne sam-osborne left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall aside from a few minor comments, thanks @jpearson-sfdc !

@ghost

ghost commented May 26, 2020

Copy link
Copy Markdown
Author

@sam-osborne I've updated the code as you requested. Whenever you get a chance can you take a look? TIA

@sam-osborne

sam-osborne commented May 26, 2020

Copy link
Copy Markdown
Contributor

@sam-osborne I've updated the code as you requested. Whenever you get a chance can you take a look? TIA

@jpearson-sfdc LGTM with the changes. If only Pardot supported the each tag, this seems like it would be a really cool feature for customers.

Comment thread src/Handlebars/Context.php
Comment thread src/Handlebars/Context.php
Comment thread src/Handlebars/Context.php
Comment thread src/Handlebars/Context.php
Comment thread src/Handlebars/Context.php
Comment thread src/Handlebars/Context.php
if (!$context instanceof Context) {
$context = new Context($context);
$context = new Context($context, [
'enableDataVariables' => $this->handlebars->isDataVariablesEnabled(),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: You could use that constant here too.

@stanlemon

Copy link
Copy Markdown

@perfectgait & @sam-osborne are we cool to merge this now?

@sam-osborne

Copy link
Copy Markdown
Contributor

Sorry to put this out there last minute @jpearson-sfdc , but having some basic documentation on how to use this in the README would be good. Even if its just a lift/shift of your PR description above.

@perfectgait

Copy link
Copy Markdown

@stanlemon I agree with what @sam-osborne suggested. Other than that I think it looks good to merge.

@ghost

ghost commented Jun 8, 2020

Copy link
Copy Markdown
Author

@perfectgait @sam-osborne @stanlemon - Can you give a proofread for the new documentation added to README.md.

https://github.com/salesforce/handlebars-php/blob/72f3c082be861cc519989e19671726d742f4fcd4/README.md#data-variables-for-each

@sam-osborne

Copy link
Copy Markdown
Contributor

LGTM, thanks @jpearson-sfdc I will go ahead and merge this in.

@sam-osborne sam-osborne merged commit 59fc47c into salesforce:master Jun 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants