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

How to create a helper with a template var in the attributes? #66

Closed
remy opened this issue Sep 2, 2014 · 5 comments
Closed

How to create a helper with a template var in the attributes? #66

remy opened this issue Sep 2, 2014 · 5 comments

Comments

@remy
Copy link

remy commented Sep 2, 2014

My apologies if I come with preconceptions, I'm used to https://github.com/donpark/hbs - but I wanted to know if it was possible to handle the following in a template:

{{link '{{url}}' href='http://{{url}}'}}

<!-- should output: <a href="http://foo.com">foo.com</a> -->

This is a contrived example, but I don't understand how I can process the attributes from inside the helper. I'm normally used to applying options.fn but that's not available in your adapter. Is this possible at all?

@ErisDS
Copy link
Member

ErisDS commented Sep 2, 2014

I'm not 100% on what you mean here, there are a couple of things that I can think of that might be pertinent:

Subexpressions in handlebars use parenthesis, which is supported

{{link '(url)' href='http://(url)'}}

except there is a known issue if the inner helper is async.

Inside a helper function the attributes passed in are accessible through options.hash, and options.fn ought to be available as well.

There are quite a few examples of helpers in the Ghost codebase that might lead you to what you're looking for.

Alternatively if you post the code you would expect to write for donpark/hbs, I can see if I can convert it.

@remy
Copy link
Author

remy commented Sep 2, 2014

Sod. I know where I'm going wrong (though I don't have the solution).

I was expecting options.fn when when I'm using {{content ...}}, but you don't get it unless you use {{#content ...}} ... {{/content}}.

@ErisDS I'm impressed you picked up this issue, it was because I spelunking the Ghost code looking to make a fix (which also includes this PR).

What I actually was is this to work (though perhaps this belongs in the Ghost repo?):

<section class="post-excerpt post-content">
  {{content words="50" append='[&hellip;] <a class="read-more" href="{{url}}">Read more &raquo;</a>'}}</p>
</section>

I've updated the content helper with:

coreHelpers.content = function (options) {
    var truncateOptions = (options || {}).hash || {};
    truncateOptions = _.pick(truncateOptions, ['words', 'characters', 'append']);

// ...etc

This way I hope to use the append property properly with the content helper, but realistically I want a "read more" and that should include a link...which is why I raised the current issue. I can't see how to get a template to be prepopulated.

I did try swapping {{url}} to (url) but that didn't seem to make a difference.

...should I also close this issue and open a new one for this topic against Ghost?

@ErisDS
Copy link
Member

ErisDS commented Sep 2, 2014

Haha, this is a pretty core module to Ghost, so I try to pay attention.

With regard to your downsize PR it's not documented, but that feature already exists and it's called contextualTags. Furthermore, the guy who added it then PR'd to Ghost but it didn't make it in due to us relying on a feature in older versions of downsize, which only just got resolved - now we need to work out if it's a useful implementation for the Ghost Theme API, and if so how to add it (we don't add stuff to the Theme API without careful consideration).

With regard to having an append property on the content helper, that has been raised before and closed as unnecessary (we really don't add stuff to the Theme API unless it's absolutely necessary and very carefully considered). So I'd recommend dropping a comment on there with your use case if you're not satisfied that the feature is unnecessary :)

The final issue you've got here is that {{url}} is an async helper, so doesn't currently work as a sub expression. Which is exactly why #56 is open, and something that I think we might have a fix for in the near future.

@remy
Copy link
Author

remy commented Sep 2, 2014

Okay, closing this issue since it's not applicable.

I'll raise my case for append (well, I'll search first). I believe there's a case, or the alternative is to remove functionality.

@remy remy closed this as completed Sep 2, 2014
@remy
Copy link
Author

remy commented Sep 2, 2014

Heh, or just add my comments to the existing thread!

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

2 participants