XaminProject / handlebars.php

Handlebars processor for php
331 stars 134 forks source link

Equality check helper #18

Closed diosney closed 10 years ago

diosney commented 10 years ago

Hi,

Currently the if helper doesn't support equality checks and I'm in needed of that.

I implemented an equality check helper in JavaScript, as defined in:

                    ´´´javascript
                    // Need to check if a conditional statement is true or false.
        Handlebars.registerHelper('equal', function (lvalue, rvalue, options) {
            if (arguments.length < 3)
                throw new Error('Handlebars Helper equal needs 2 parameters');
            if (lvalue != rvalue) {
                return options.inverse(this);
            } else {
                return options.fn(this);
            }
        });

And is used like this:

                   ´´´javascript
                   {{#equal options.type "1-2+1-2"}}
                             Some content here.
                   {{/equal}}

I'm trying to convert that helper into Handlebars.PHP, but I'm having a hard time.

Can you help me on this?

Thanks!

diosney commented 10 years ago

Hi! I finally come with a working helper solution, but I don't know if it follows your recommended codestyle and that you have the interest to include in the code (maybe not since Handlebars.js don't have it? :( )

Well, this is the code, can you check it if it follows your code standard?

/**
 * Create handler for the 'equal' helper.
 * Needed for compatibility with PHP 5.2 since it doesn't support anonymous functions.
 * 
 * @param $template
 * @param $context
 * @param $args
 * @param $source
 *
 * @return string
 * @throws InvalidArgumentException
 */
public static function _helper_equal($template, $context, $args, $source) {
    $tmp = explode(' ',  $args);
    $buffer = '';

    if (count($tmp) < 2) {
        throw new InvalidArgumentException('Handlebars Helper equal needs 2 parameters');
    }
    if ($context->get($tmp[0]) == $tmp[1]) {

        echo($tmp[1]);
        $buffer = $template->render($context);
    }
    return $buffer;
}
everplays commented 10 years ago

Thanks for submitting your helper but,

  1. You can register your own helper without need of modifying any class, take a look at #12 issue (use closures).
  2. You shouldn't bring your logic into template, it's not handlebars way. On Sep 1, 2013 7:58 AM, "Diosney" notifications@github.com wrote:

Hi! I finally come with a working helper solution, but I don't know if it follows your recommended codestyle and that you have the interest to include in the code (maybe not since Handlebars.js don't have it? :( )

Well, this is the code, can you check it if it follows your code standard?

/**

  • Create handler for the 'equal' helper.
  • Needed for compatibility with PHP 5.2 since it doesn't support anonymous functions. *
  • @param $template
  • @param $context
  • @param $args
  • @param $source *
  • @return string
  • @throws InvalidArgumentException */ public static function _helper_equal($template, $context, $args, $source) { $tmp = explode(' ', $args); $buffer = '';

    if (count($tmp) < 2) { throw new InvalidArgumentException('Handlebars Helper equal needs 2 parameters'); } if ($context->get($tmp[0]) == $tmp[1]) {

    echo($tmp[1]);
    $buffer = $template->render($context);

    } return $buffer; }

— Reply to this email directly or view it on GitHubhttps://github.com/XaminProject/handlebars.php/issues/18#issuecomment-23618278 .

diosney commented 10 years ago

Okey :) I expected some answer like that :)

Then I will put my helper outside Handlebars.PHP source code.

Thanks