Skip to content
This repository has been archived by the owner on Jan 26, 2019. It is now read-only.

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

Open
kezzbracey opened this issue Jul 16, 2015 · 8 comments
Open

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

kezzbracey opened this issue Jul 16, 2015 · 8 comments

Comments

@kezzbracey
Copy link

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
Copy link
Contributor

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
Copy link
Author

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
Copy link
Author

Is this perhaps the reason?

#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
Copy link
Contributor

@JustBlackBird could you please check this one too?

@JustBlackBird
Copy link
Contributor

@ 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
Copy link
Author

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
Copy link
Contributor

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
Copy link
Author

Thanks very much for the clarification. :)

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

No branches or pull requests

3 participants