New to Jekyll converting a WordPress blog I'm trying to add pagination to my category layout. In _layouts directory I've created a file named category.html. I can successfully render a particular category with:
category.html:
---
layout: default
---
{% assign catName = page.title | string %}
{% for post in site.categories[catName] %}
<p>{{ post.title }}</p>
{% endfor %}
When I try to paginate a category's posts after following Jekyll's Pagination documentation:
{% for foobar in paginator.posts %}
<p>{{ foobar.title }}</p>
{% endfor %}
the code renders nothing. Upon my research Github Pages limits what plugins can be used and I cannot verify if jekyll-paginate-v2 is allowed.
My file structure:
_config.yml:
plugins:
- jekyll-feed
## - jekyll-paginate-v2
- jekyll-paginate
exclude:
- Gemfile
- Gemfile.lock
collections:
category:
output: true
defaults:
scope:
path: ""
type: category
values:
layout: "category"
paginate: 1
paginate_path: "/page:num/"
Gemfile:
gem "github-pages", group: :jekyll_plugins
group :jekyll_plugins do
gem "jekyll-feed", "~> 0.6"
## gem "jekyll-paginate-v2", "~> 1.7"
gem "jekyll-paginate"
end
Research:
Upgrade Jekyll 3 - jekyll-paginate
Pagination
jekyll-paginate-v2
Generate category page in jekyll targeting github pages
Jekyll paginate
For a Jekyll site on Github Page how can I create pagination for the category to render only posts for that category? This would be the equivelent to WordPress' category.php.
Paginate only paginates all posts and not by category or tag.
Paginate V2 does this even for collections. But you cannot run this plugin on Github pages (allowed plugins).
Two solutions :
publish your code in a branch, generate your site locally (or with a service like Travis Continuous Integration that is free for open source projects) and publish (or let your CI publish) your generated code in another branch.
use a modern hosting provider like Netlify that allows you to use any plugin.
Related
The Grav's documentation clearly describes how a whole page or a folder could be hidden from unregistered users. It also describes how a whole page could be seen only by particular user groups.
But what about pieces of a page, let's say, some links or a private info I want to show on some conditions?
Ok, for registered users I found a snippet at Login plugin docs:
{% if grav.user.authenticated %}
content for registered users goes here
{% endif %}
But going wider - how can I show/hide pieces of a particular page depending on some custom logic in PHP code, i.e. not necessarily user related?
I'm thinking about a twig/shortcode plugin, something like:
{% if some.custom.condition.or.PHP.function %}
hidden content goes here
{% endif %}
or
[hidden_if_something] hidden content goes here [/hidden_if_something]
But not sure how exactly this should be implemented. So working examples would be appreciated. Thanks.
There is a recipe in the Grav documentation here. This provides an example of how to render the output of a PHP code result in a twig template.
In the example they create a plugin, and implement a twig extension providing access to a php function. They can then simply call that php function like in a twig template.
{{ example() }}
Following that example, you can implement whatever logic you would like in php, and call the function in a twig if statement.
{% if example() == true %}
your conditional output
{% endif %
EDIT | http://symfony.com/doc/current/assetic/asset_management.html#assetic-cssrewrite
So does this mean I absolutely have to upload the CSS assets into "web" myself? Is there not a way to use assetic:dump to copy them from the bundle first?
I have an application which is divided multiple smaller application via bundles.
One bundle in particular is an attempt to integrate a legacy app into symfony to run the same platform as a few other apps.
This LegacyBundle has JS and CSS assets which I do not want manually uploaded into "web" - I was under the impression that using assetic allowed me to have the CLI tools compress and re-locate the assets into the web directory.
This seems to be the case using #LegacyBundle prefix on the asset paths:
{% block javascripts %}
{% javascripts
'#LegacyBundle/Resources/legacy/common/extjs/ext-all.js'
%}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock %}
Problem is - I need this same functionality with CSS files and apparently there is an issue using # shortcut with CSS files and assetic:
http://symfony.com/doc/current/assetic/asset_management.html
Notice that in the original example that included JavaScript files, you referred to the files using a path like #AppBundle/Resources/public/file.js, but that in this example, you referred to the CSS files using their actual, publicly-accessible path: bundles/app/css. You can use either, except that there is a known issue that causes the cssrewrite filter to fail when using the #AppBundle syntax for CSS stylesheets.
So I am trying to avoid using #LegacyBundle syntax for both JS and CSS - however I still wish to reference their paths in twig using the path relative to the bundle - where they go after that is of no concern to me.
What am I missing? Is what I am trying to do possible?
I tried moving ext-alljs.js into the bundle "Resources" root directory and then inside twig template I call the assets via
{% block javascripts %}
{% javascripts
'Resources/ext-all.js'
%}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock %}
Unfortunately this doesn't work - whereas the previous example does. I am hoping I just have the path incorrect or something?
So right now I have the following in my config.yml:
gems: [jekyll-paginate]
paginate: 3
paginate_path: "page:num"
So in my index.html page I have the following:
{% for post in paginator.posts %}
Stuff
{% endfor %}
I am having trouble figuring out how I would make separate category pages and have pagination for them.
Could anyone help me shed light? I've been googling forever and I haven't been able to find anything.
Jekyll only manages posts pagination.
If you want to paginate categories, you will need a plugin like https://github.com/midnightSuyama/jekyll-paginate-category.
I'm listing all the pages (from different folders) belonging to category "featured", and I'd like to order them by a custom value. At the moment they're being ordered by date:
{% for p in taxonomy.findTaxonomy({'category':'featured'}).order('date','desc') %}
{{ p.title }}
{% endfor %}
However I want to add an "order" field in the page header
---
// for the first page to show
title: Just a page
order: 1
---
// for the second page to show
title: Not just a page
order: 2
---
What I want is something like .order('p.header.order','asc')
Is there any way to do this?
I got help from #rhukster at Grav forums but posting the solution here as well. It's simple.
.order('header.order','asc')
grav has the option to order folders by adding a number at the beginning of the folder name.
My application shows a list of projects, project detail pages and forms to edit these projects. These are the routes:
/ - list of projects
/project/42 - view project (project detail page)
/project/42/edit - edit project
Only its owner may edit a project.
I have implemented a Voter to prevent access to /project/42/edit for non-owners.
Now, I also want to hide the link "edit project" from the project detail page. What would be the way to do this? Ideally, in Twig, I would like to do something like
{% if may_access(path('project_edit', { 'id': project.id })) %}
edit project
{% endif %}
I can implement this function as a Twig extension, but maybe a similar functionality already exists.
The function is_granted() actually has a second parameter that allows me to do just what I need:
{% if is_granted("MAY_EDIT", project) %}
edit project
{% endif %}
I use this in combination with a check in the controller action:
public function editAction(Project $project)
{
if (!$this->get('security.context')->isGranted('MAY_EDIT', $project)) {
$this->flash('You are not allowed to edit this project');
return $this->show($project);
}
// ...
}
This is actually very similar to the approach that nifr used in his answer to Sonata User - Security on custom field. I was hoping to find a way to have the voter be called automatically and avoid the call to isGranted().
If you want to have a look at the complete code, it is in the tutorial project I have published in github.