How do i display subcategories in octobercms - laravel

I have been on this for a while with octobercms. I don't know if i am going about it the right way. I have my categories and subcategories already set up with nestedtree. In the Category model i used scope below to render just the parent category on a page.
public function scopeCategory($query)
{
return $query->where('parent_id', 0);
It all works fine.i connected it to a record details page by slug which only shows the parent category on the page as a heading.
{% if record %}
<h4>{{ record.cat_title }}</h4>
{% else %}
{{ notFoundMessage }}
{% endif %}
Now i want to render the subcategories here on the details page so when a user clicks on a category in the list page like agriculture the details page shows all the subcategories under agriculture, then the user can navigate further to the products. Not sure how to implement the subcategory part. I have tried a bunch of codes but it does nothing. Any solution to this?
Additional Info: I have already used scope to get only the parent categories on the list page

I am assuming you already used nested-tree [ https://octobercms.com/docs/database/traits#nested-tree ].
First we just pass parent parentCats to our view / page
use HardikSatasiya\SoTest\Models\Categories;
function onStart() {
$this['parentCats'] = Categories::getAllRoot();
}
In page/view to show category and its sub-categories up to N level we can use this render_cats macro
{% import _self as thisPage %}
{% macro render_cats(items) %}
{% import _self as thisPage %}
{% for item in items %}
<li>{{ item.level }} {{ item.title }}
{% if item.childcount > 0 %}
<ul>
{{ thisPage.render_cats(item.children) }}
</ul>
{% endif %}
</li>
{% endfor %}
{% endmacro %}
<ul>
{{ thisPage.render_cats(parentCats) }}
<!-- we are passing parent cats ^ here -->
</ul>
Output
<ul>
<li>
0 Parent Cat 1
<!-- ^ this is level -->
<ul>
<li>
1 Sub cat 1
<ul>
<li>2 Sub Sub cat 1
</li>
<li>2 Sub Sub cat 3
</li>
<li>2 Sub Sub cat 3
</li>
</ul>
</li>
<li>1 Sub cat 2
</li>
</ul>
</li>
<li>
0 Parent Cat 2
<ul>
<li>1 Sub cat 3
</li>
<li>1 Sub cat 4
</li>
</ul>
</li>
</ul>
If any doubt please comment

Related

Product Image in mega menu

I am build a megamenu in Shopify that will show a mix of collection image, product images and a generic image for any other link that might be in there.
Currently the collection image displays fine and the generic image, but having some trouble getting the product image to display. My code for the mega menu is show below:
<ul class="megaMenu">
<div class="megaMenuWrapper">
{% for child_link in link.links %}
<li {% if child_link.active %}class="active {% if child_link.child_active %}child-active{% endif %}"{% endif %}>
<a href= "{{ child_link.url }}">
{% if child_link.type == "collection_link" and child_link.object.image %}
<div class="menuImage" style="background-image: url('{{ child_link.object.image | img_url: '500x' }}')"></div>
{% elsif child_link.type == "product_link" and child_link.object.image %}
<div class="menuImage" style="background-image: url('{{ child_link.object.image | img_url: '500x' }}')"></div>
{% else%}
<div class="menuImage" style="background-image: url('https://cdn.shopify.com/s/files/1/0924/5464/files/map_macarons_paris.jpg?1158498038497005180')"></div>
{% endif %}
<span>{{ child_link.title }}</span>
</a>
</li>
{% endfor %}
</div>
</ul>
Any ideas on what might be wrong are welcome.
Products doesn't have an image object, it has a featured_image or images.
So you should call {{ child_link.object.featured_image | img_url: '500x' }} instead.
The same applies for your if check where you check if the image is present.

On clicking on an li a drop-down appears but clicking again on li the drop-down is not closing

I have written HTML for a drop-down menu, but it is not working properly. When I click on ‘Products’ the drop-down menu appears, but when I click again on ‘Products’ the drop down is not closing.
<div class="dropdown-menu" id="dropdown-toggle">
<div class="dropdown-inner" id="dropdown-down"> {% for children in category.children|batch(category.children|length / category.column|round(1, 'ceil')) %}
<ul class="list-unstyled" id="ul-dropdown">
{% for child in children %}
<li>
{{ child.name }}
</li>
{% endfor %}
</ul>
{% endfor %}</div>
{# {{ text_all }} {{ category.name }} #}
</div>

Display array items that have title in jekyll

I have this code:
<nav>
<ul>
{% for page in site.pages %}
{% if page.title %}
<li>{{ page.title }}</li>
{% endif %}
{% endfor %}
</ul>
</nav>
I don't have any pages with title yet but I may have in the future and tidy-html5, that I'm using to output html with indent, give warning that ul is empty. Without title check it output generated pages tags or global pages like sitemap or rss feed.
Is there a way to first filter array and then don't show nav if the array is empty, something like:
{% pages = [for page in pages if page.title] %}
{% if pages.length %}
<nav>
<ul>
....
</ul>
</nav>
{% endif %}
To get only pages with titles:
pages = site.pages.select{ |p| p.title }
So your code might look something like:
{% assign pages = site.pages.select{ |p| p.title } %}
{% if pages %}
<ul>
{% for page in pages %}
<li>...</li>
{% endfor %}
</ul>
{% endif %}

How to use JSON data in a nunjucks template?

js return list =
<ul><li data-name="{id:1,name:'a'}">a</li><li data-name="{id:2,name:'b'}">b</li></ul>
{{list|dump}} Why does it show?
<ul><li data-name=\"[object Object]\">a</li><li data-name=\"[object Object]\">b</li></ul>
you can use raw
you can use a {% raw %} block and anything inside of it will be output as plain text.
Your code:
<ul>
<li data-name="{id:1,name:'a'}">a</li>
<li data-name="{id:2,name:'b'}">b</li>
</ul>
Example:
{%
set list = [
{name: 'a', id: 'yourId1'},
{name: 'b', id: 'yourId2'}
]
%}
<ul>
{% for item in list %}
<li data-name="{{item.name}}" id="{{item.id}}">{{item.name}}</li>
{% endfor %}
</ul>
Output:
<ul>
<li data-name="a" id="yourId1">a</li>
<li data-name="b" id="yourId2">b</li>
</ul>

Is there a way to loop through posts with specific tags on Siteleaf using liquid template?

Here's the scenario: I have a portfolio website running on Siteleaf (siteleaf.com - which uses liquid template) and I have a portfolio section with several projects as posts. The trick is that I only wish to bring the projects with the tag featured
I was able to bring the posts but only limit the number of posts that is shown, but not the tag that they are assigned. Here's the code I'm using:
<ul class="container work-gallery clearfix">
{% for post in site.pages['work'].posts limit:6 %}
<li class="col-md-6 thumb">
<a href="{{post.url}}" style="background-image: url('{{post.assets.first.url}}');">
<h4>{{post.title}}</h4>
</a>
</li>
{% endfor %}
</ul>
Is there a way to do it? Thanks for the help!
I'll bet you've since figured this out, because you're super close.
There are a few ways to do this (more info here and here). I'll assume for this example that you're using Siteleaf's default tags taxonomy (aka Tag Set).
<ul>
{% for post in taxonomy['tags']['featured'].posts limit:6 %}
<li>
<a href="{{post.url}}">
<h4>{{post.title}}</h4>
</a>
</li>
{% endfor %}
</ul>
You could then just add an if if you wanted to do something with the others.
Alternatively, if you wanted to really party, you could use the assign tag to store a variable, then use it as a condition for output (or for additional tags or variables).
{% assign featured_posts = '' %}
<ul>
{% for post in taxonomy['tags']['featured'].posts limit:6 %}
{% assign featured_posts = featured_posts | append:',' | append:post.id %}
<li>
<a href="{{post.url}}">
<h4>{{post.title}}</h4>
</a>
</li>
{% endfor %}
...
<ul>
{% for post in posts %}
{% unless featured_posts contains post.id %}
<li class="not-featured">
<a href="{{post.url}}">
<h4>{{post.title}}</h4>
</a>
</li>
{% endunless %}
{% endfor %}
</ul>

Resources