XaminProject / handlebars.php

Handlebars processor for php
331 stars 134 forks source link

Possible to use dot / paths with registered helper? #131

Open kezzbracey opened 8 years ago

kezzbracey commented 8 years ago

Hi, first thanks for this awesome project. It's allowing me to work on a project I've been wanting to do for ages.

What I'm trying to do at the moment is register the helper "@blog" to emulate something done in express.

I'd like to be able to use it like so:

{{@blog.title}}

However I can't figure out how to go about it.

I can get this working:

{{#@blog}} {{title}} {{/@blog}}

And without the @ symbol:

{{blog.title}}

Could anyone give me any advice?

Thanks!

fzerorubigd commented 8 years ago

The @ symbol has no special meaning in the handlebras syntax as fa as I know. could you please provide a sample code for you use case?

kezzbracey commented 8 years ago

Thanks for getting back to me so fast.

I've tried a couple of different ways.

One was just sending an array of data straight into the template rendering without a helper, e.g.:

    array(
        '@blog' => array(
            array('title' => 'The Title'),
            array('description' => 'The description')
        )
    )

But then when attempting to use the expression {{@blog.title}} I would get a message saying the @blog helper was not registered.

So I then attempted to do the same thing via registering a helper.

I tried this first inline in my new Handlebars... code, but sorry I didn't keep that code.

My attempt at doing it via implementing the Helper class was:

class BlogHelper implements Helper
{

    public function execute(Template $template, Context $context, $args, $source)
    {

        $data['title'] = "The Title";

        $context->push($data);
        $buffer = $template->render($context);

        return $buffer;

    }
}

and

$this->hbars = new Handlebars\Handlebars(
    array(
        'loader' => new \Handlebars\Loader\FilesystemLoader( $markuppath, array( 'extension' => '.hbs' ) ),
        'partials_loader' => new \Handlebars\Loader\FilesystemLoader( $markuppath . "/partials", array( 'extension' => '.hbs' ) ),
        'helpers' => new \Handlebars\Helpers( array(
                '@blog' => new Expressions\Helpers\BlogHelper()
            )
        )
    )
);

With this I can get the title to output with {{#@blog}} {{title}} {{/@blog}} but nothing that comes after that expression in the template renders.

With {{@blog.title}} there is no output.

I'm sure it's just my not knowing how to properly setup helpers, so any help would be very greatly appreciated.

Thanks!

kezzbracey commented 8 years ago

Is this perhaps the reason?

https://github.com/XaminProject/handlebars.php/pull/132

To let you know, basically I'm trying to allow for the same functionality you see in Ghost, which runs on Express using Handlebars.js:

http://themes.ghost.org/docs/blog

fzerorubigd commented 8 years ago

@JustBlackBird could you please check this one too?

JustBlackBird commented 8 years ago

@ character is used in Handlebars.js for private variables that could be set in block helpers. See this for details. At the moment there is no user-defined private variables in Handlebars.php.

Variables that starts with @ character are tricky in Handlebars.js. {{@foo}} is a private variable and one from the context is ignored but {{[@foo]}} is taken from the context. As for me it's a kind of bug but may be I'm wrong and such behavior was introduced intentionally.

I've created a JS Fiddle for you: http://jsfiddle.net/wtmgnd2w/6/

kezzbracey commented 8 years ago

Ah ok, thanks for going to so much trouble to explain.

In a nutshell, is this correct?

Right now I can't use @blog because there's no support for custom private variables in Handlebars.php.

If in the future this feature was added, I'd have to add my private variables into the "data" option for any context I wanted them available in.

JustBlackBird commented 8 years ago

Right now I can't use @blog because there's no support for custom private variables in Handlebars.php.

Correct.

If in the future this feature was added, I'd have to add my private variables into the "data" option for any context I wanted them available in.

Incorrect. data option can be passed only from helper. That's how private variables works in Handlebars.js

kezzbracey commented 8 years ago

Thanks very much for the clarification. :)