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

[Question] #if built in helper is not working as expected #594

Open
parag-patil opened this issue Oct 18, 2024 · 7 comments
Open

[Question] #if built in helper is not working as expected #594

parag-patil opened this issue Oct 18, 2024 · 7 comments
Labels

Comments

@parag-patil
Copy link

parag-patil commented Oct 18, 2024

I am using the #if helper, but it is not working as expected. Below is my equal helper code (exception handling removed for clarity):

_handlebars.RegisterHelper("equal", (writer, context, parameters) =>
{
	if (parameters.Length != 2)
	{
		throw new TemplateServiceException(
			"The equal helper requires exactly two parameters.");
	}

	var firstDecimal = Convert.ToDecimal(parameters[0], CultureInfo.InvariantCulture);
	var secondDecimal = Convert.ToDecimal(parameters[1], CultureInfo.InvariantCulture);

	writer.WriteSafeString(firstDecimal == secondDecimal);
});

Template:

<ul class="list-buffer">
	Result of equal helper: {{equal (record_count VerifiedList) 1}}
	{{#if (equal (record_count VerifiedList) 1)}}
		is ready to pay.
	{{else}}
		are ready to pay.
	{{/if}}
</ul>

I am printing the result of {{equal (record_count VerifiedList) 1}} and it correctly shows False. However, my {{#if... condition is not rendering the else part in the template output. It prints "is ready to pay." even though VerifiedList has 2 records and record_count returns it correctly.

Could the issue be that the equal helper is writing "False" as a string instead of a boolean?

Output

image

@rexm
Copy link
Member

rexm commented Oct 18, 2024

Yes, #if follows the truthy rules of JavaScript, so a non-empty string is truthy.

@parag-patil
Copy link
Author

But result of my equal helper is False.

@rexm
Copy link
Member

rexm commented Oct 18, 2024

No, it’s “False”

@parag-patil
Copy link
Author

parag-patil commented Oct 18, 2024

You mean writer.WriteSafeString(firstDecimal == secondDecimal); or writer.Write(firstDecimal == secondDecimal); always write it as string? Can we change this behavior ?

I tried writer.Write<Boolean>(firstDecimal == secondDecimal); but it also did not work.

@rexm
Copy link
Member

rexm commented Nov 8, 2024

No matter what, it will produce a string.

@leriocn
Copy link

leriocn commented Dec 16, 2024

Any way,the only solution just like below code:
//注册eq等于 handlebars.RegisterHelper("eq", (writer, context, parameters) => { if (parameters.Length == 2 && parameters[0]?.ToString() == parameters[1]?.ToString()) { writer.Write(true); } else { writer.Write(""); } });

@leriocn
Copy link

leriocn commented Dec 16, 2024

writer.Write(""); can be work!

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

No branches or pull requests

3 participants