Ghost foreach missing post context - ghost-blog

I'm trying to access the post published_at date inside a loop that's iterating over the authors of a post but it's not working
This is an example: the date outside of the foreach block is correct but inside the block it will default to the current date
{{date published_at format="D MMMM YYYY"}}
{{#foreach authors}}
<time class="author--date">{{date published_at format="D MMMM YYYY"}}</time>
{{/foreach}}

Ghost uses Handlebars template syntax, and in your case foreach creates a new scope. So inside it you need to access parent scope to get published_at variable.
By prepending ../ to the property name, you can reference the parent scope. Check docs for more info.
So in your case it should be:
{{date published_at format="D MMMM YYYY"}}
{{#foreach authors}}
<time class="author--date">{{date ../published_at format="D MMMM YYYY"}}</time>
{{/foreach}}

Related

have any way to change tag name dynamically in get query ?

{{#get "posts" filter="tag:news" as |news_post|}}
{{#foreach news_post}}
{{title}}
{{/foreach}}
{{/get}}
How Can I Change "news" tag dynamically ?
By your question I suggest you want to base it on some that within your .. post perhaps ? You can use it within the {{post}} by using the {{tags.[0].slug}}
{{#get "posts" filter="tag:{{tags.[0].slug}}" include="tags" as |news_post|}}
{{#foreach news_post}}
- {{title}} - {{tags.[0].slug}}<br>
{{/foreach}}
{{/get}}

how to get all posts on post.hbs page in ghost

I want all posts on post.hbs but it's not displaying any post list, I want check title with my string and display particular post on post.hbs.
Below code is not working in post.hbs
{{#foreach posts}}
<p>{{content}}</p>
{{/foreach}}`
Its not working on post.hbs page
Post.hbs is for displaying a specific post, so you are in {{post}} scope:
{{#post}}
{{/post}}
You can try something like this:
{{#get "posts" limit="all" as |allposts| }}
{{#foreach allposts}}
{{content}}
{{/foreach}}
{{/get}}

How can i can display fields from an item from Collection B referenced by an id from an item from Collection A?

i have two collections
CategoryCollection = new Mongo.Collection("CategoryCollection");
usersWordpressCollection = new Mongo.Collection("usersWordpressCollection");
CategoryCollection hold posts within a category, within the posts objects there is the Author, which comes as an id. take a look at the image below, draw your attention to posts.author
usersWordpressCollection holds the authors whose id (see image below) is referenced in categoryCollection under posts.author
when i display a post, its coming from categoryCollection so i can get the post, the link etc. i can also get the author id however when it comes to this, i want to return, not the id, but the referenced Author information found in the usersWordpressCollection
latest html
<template name="latest">
<ul>
{{#each articles}}
<li class=" home-latest ">
<span class="label"> <a class="" href="/catsingle/CategorySingle/{{_id}}">{{name}}</a></span>
{{#each posts}}
<div class="card">
<div class="category-img">{{{image}}}</div>
<div class="card-divider">
{{title}}
</div>
<div class="card-section">
<p>
{{> authors}}
</p>
</div>
</div>
</li>
{{/each}}
</ul>
</template>
latest js
Template.latest.helpers({
articles: function () {
var id = FlowRouter.getParam('_id');
return CategoryCollection.find({}, {sort: {date_created: -1}, limit:1}).fetch();
}
});
authors html
<template name="authors">
{{#each authors}}
<p>
{{name}}
</p>
{{/each}}
</template>
authors js
Template.authors.helpers({
authors: function () {
var id = this.author;
console.log(id);
var alias = usersWordpressCollection.findOne(id, {fields: {name: 1} });
return alias.name;
console.log(alias.name);
});
As you can see, this.author is the author id found in categoryCollection posts.author. Im trying to find that id in the usersWordpressCollection matching the author, then i want to display the name field of the Author whose id was matched. but i can't seen to get it working, how can i achieve this.
Since there is only one author per post you don't need to iterate over a cursor of authors, you only need one. So where you have:
{{#each authors}}
<p>
{{name}}
</p>
{{/each}}
Instead use:
{{#with author}}
<p>
{{name}}
</p>
{{/with}}
Now, the data context coming into the authors template will be a post object (since you are within an {{#each posts}}. You can therefore construct your author helper as follows:
Template.authors.helpers({
author(){
return usersWordpressCollection.findOne({ id: parseInt(this.author) });
}
});
Since you've indicated that this.author is a string but the id of your user collection is an integer then you need to convert the type - you can do this directly in your query.

Passing through variable into partial and using an #is helper - Handlebars

I'm trying to pass a variable (tag name) into a Handlebars partial and use an #is block helper on the tag but for some reason it just won't play ball. This is my code:
Call to my partial and passing through the tag name.
{{> nav tagged='page' }}
In the partial itself I do the following (tagged is the variable name passed through):
{{#each tags}}
{{#is tag tagged}}
{{#each pages}}
// Do code here
{{/each}}
{{/is}}
{{/each}}
If I just render the tagged variable it displays the variable value as expected so a bit confused as to why its not working.
Thanks.
The issue you have is that the tagged variable is in the parent context but you're trying to reference it within the #each tags loop.
You can reference the parent context with ../ so the working code would be
{{#each tags}}
{{#is tag ../tagged}}
{{#each pages}}
// Do code here
{{/each}}
{{/is}}
{{/each}}

Assemble / Handlebars Sort Order

I created a simple dynamic navigation using the example from the the Assemble FAQs
{{#each pages }}
{{#is data.section "main"}}
<li{{#is ../../page.dest this.dest}} class="active"{{/is}}>
{{data.menutitle}}
</li>
{{/is}}
{{/each}}
How does one achieve sort-order? Right now the links seem to be in random order. They seem to be in the alpha-order of the page alias (index.html).
They should be:
Index
Products
Find Us
but what's rendered is:
Find Us
Index
Products
Here's a link to the Assemble documentation that has a section on using withSort.
I'd use something like:
{{#withSort pages data.sortOrder}}
{{#is data.section "main"}}
<li{{#is ../../page.dest this.dest}} class="active"{{/is}}>
{{data.menutitle}}
</li>
{{/is}}
{{/withSort}}
Where sortOrder is defined at page level, for example having this in your page templates:
---
title: Index
sortOrder: 0
---
etc.

Resources