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

Custom condition helper over payload array with conditions #356

Open
jmares79 opened this issue Oct 22, 2021 · 0 comments
Open

Custom condition helper over payload array with conditions #356

jmares79 opened this issue Oct 22, 2021 · 0 comments

Comments

@jmares79
Copy link

Hi all, great library!!

I'm trying to build a custom helper in order to compare, for example, 2 strings. One argument of the condition should be an attribute in the payload and the 2nd one either a constant string or another variable.

So far what I have done, by checking the docs, is:

The PHP Code:

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

// The Template:
$template = <<<VAREND
<div>{{#if_eq name 'SOME NAME'}}<div><h1>IS EQUAL</h1><div>{{/if_eq}}</div>
<div>{{#if_eq name attribute}}<div><h1>IS EQUAL</h1><div>{{/if_eq}}</div>
VAREND;

// Helpers:
$helpers = array(
  '"if_eq" => function ($arg1, $arg2) {
           return $arg1 == $arg2;
    }
);

$phpStr = LightnCandy::compile($template, array(
  // Used compile flags
  'flags' => LightnCandy::FLAG_ERROR_EXCEPTION | LightnCandy::FLAG_HANDLEBARS,
  'helpers' => $helpers
));

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

// Input Data:
$data =[
      "name" => "SOME NAME"
      "attribute" => "SOME NAME"
];

// 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);
1
``` The Issue: ----------

The result of the helper is "

1
", and I would have expected the HTML in the middle of the operator to be shown. Obviously I'm missing something or I misunderstood the way that custom block helpers are built.

Does anyone know what is the correct way of implementing something like that?

Regards!

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

No branches or pull requests

1 participant