Bootstrap + Wordpress + Timber - wordpress

Are there any tutorials or templates created with wordpress+timber+bootstrap?
I'm having problem with the menu...
menu-head.twig:
<nav class="navbar navbar-custom">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Brand</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
{% include "menu.twig" with {'menu': menu.get_items} %}
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container-fluid -->
</nav>
menu.twig:
{% if menu %}
<ul class="nav navbar-nav navbar-right">
{#
<li>Link</li>#} {% for item in menu %}
<li class="{{item.classes | join(' ')}}">
{{item.title}} {% include "menu.twig" with {'menu': item.get_children} %}
</li>
{% endfor %}
</ul>
{% endif %}
and the unfortunate result is this one:
I don't know how to implement it with the child menus being shown as dropdown menus... please help...
EDIT: I will be moving to foundation... :)

#manson-mamaril you're just missing a small bit in menu.twig. Instead of {% for item in menu %} it should be {% for item in menu.get_items %}:
{% if menu %}
<ul class="nav navbar-nav navbar-right">
{#
<li>Link</li>#}
{% for item in menu.get_items %}
<li class="{{item.classes | join(' ')}}">
{{item.title}}
{% include "menu.twig" with {'menu': item.get_children} %}
</li>
{% endfor %}
</ul>
{% endif %}

I came across a blog post from about a year ago while searching for WP-Timber-Foundtion 6 info.
http://kmturley.blogspot.com/2015/01/wordpress-timber-twitter-bootstrap.html

Something like this should work
<div class="collapse navbar-collapse" id="navbarmain">
<ul class="nav navbar-nav main-menu">
{% for item in menu.get_items %}
<li class="dropdown"> <a class="dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" href="{{item.get_link}}">{{item.title}}<b class="caret"></b></a>
{% if item.get_children %}
<ul class="dropdown-menu" role="menu">
{% for child in item.get_children %}
<li>{{child.title}}</li>
{% endfor %}
</ul>
{% endif %}
</li>
{% endfor %}
</ul>
</div>

In your menu-head.twig, replace {% include "menu.twig" with {'menu': menu.get_items} %} by {% include "menu.twig" with {'menu': menu} %}
Then, in your menu.twig
{% if menu %}
<ul class="nav navbar-nav navbar-right">
{#
<li>Link</li>#} {% for item in menu.items %}
<li class="{{item.classes | join(' ')}}">
{{item.title}} {% include "menu.twig" with {'menu': item.get_children} %}
</li>
{% endfor %}
</ul>
{% endif %}
You should iterate trough menu.items which is an array containing all your menu items.
By the way, make sure, you pass your menu to the context :
$context['menu'] = new \Timber\Menu(ID);
Finally, if you easily want to see what happens in you menu object, you can display this object with menu|print_r

Related

Menu [Timber] in Wordpress

How to make a drop-down menu in Timber WordPress, the following is my code but is not showing as drop-down, also I am inserting an image how it looks now, the menu is horizontal menu
enter image description here
<nav class="collapse navbar-collapse order-lg-2" id="mainNavigation">
<ul class="navbar-nav ms-auto">
{% for item in topmenu.items %}
<li class="nav-item">
<a class="nav-link {{ item.classes|join(' ') }} mx-2" href="{{ item.link }}">{{ item.title }}</a>
{% if item.children %}
<ul class="nav-drop">
{% for child in item.children %}
<li class="nav-drop-item">
{{ child.title }}
</li>
{% endfor %}
</ul>
{% endif %}
</li>
{% endfor %}
</ul>
use menu.twig file with the following code:
{% if menu %}
<ul>
{% for item in items %}
<li class="{{ item.classes | join(' ')}}">
<a target="{{ item.target}}" href="{{ item.link}}">{{ item.title}}</a>
{% include "menu.twig" with{'items':item.children} %}
</li>
{% endfor %}
</ul>
{% endif %}
and then inside the base.twig or inside the other file where you want to display the menu add the following code:
{% include "menu.twig" with{'items':menu.get_items} %}

Symfony 4 - Customize KnpPaginator pagination template?

I'm looking to customize a KnpPaginator paging template to look like this:
It is a simple template normally, I just want to display the range in which I am located, with the max and a previous and next button.
I saw that it was possible to make our own templates. So I wanted to draw inspiration from those already existing, but I don't understand at all how they work, they seem so different from each other, I find it hard to find my way and find what I have to use
Can someone help me please ?
I have this base template but I don't know how edit it to have my own customization template :
{#
/**
* #file
* Twitter Bootstrap v4-beta.2 Sliding pagination control implementation.
*
* View that can be used with the pagination module
* from the Twitter Bootstrap CSS Toolkit
* https://getbootstrap.com/docs/4.0/components/pagination/
*
*/
#}
{% if pageCount > 1 %}
<nav>
{% set classAlign = (align is not defined) ? '' : align=='center' ? ' justify-content-center' : (align=='right' ? ' justify-content-end' : '') %}
{% set classSize = (size is not defined) ? '' : size=='large' ? ' pagination-lg' : (size=='small' ? ' pagination-sm' : '') %}
<ul class="pagination{{ classAlign }}{{ classSize }}">
{% if previous is defined %}
<li class="page-item">
<a class="page-link" rel="prev" href="{{ path(route, query|merge({(pageParameterName): previous})) }}">« {{ 'label_previous'|trans({}, 'KnpPaginatorBundle') }}</a>
</li>
{% else %}
<li class="page-item disabled">
<span class="page-link">« {{ 'label_previous'|trans({}, 'KnpPaginatorBundle') }}</span>
</li>
{% endif %}
{% if startPage > 1 %}
<li class="page-item">
<a class="page-link" href="{{ path(route, query|merge({(pageParameterName): 1})) }}">1</a>
</li>
{% if startPage == 3 %}
<li class="page-item">
<a class="page-link" href="{{ path(route, query|merge({(pageParameterName): 2})) }}">2</a>
</li>
{% elseif startPage != 2 %}
<li class="page-item disabled">
<span class="page-link">…</span>
</li>
{% endif %}
{% endif %}
{% for page in pagesInRange %}
{% if page != current %}
<li class="page-item">
<a class="page-link" href="{{ path(route, query|merge({(pageParameterName): page})) }}">{{ page }}</a>
</li>
{% else %}
<li class="page-item active">
<span class="page-link">{{ page }}</span>
</li>
{% endif %}
{% endfor %}
{% if pageCount > endPage %}
{% if pageCount > (endPage + 1) %}
{% if pageCount > (endPage + 2) %}
<li class="page-item disabled">
<span class="page-link">…</span>
</li>
{% else %}
<li class="page-item">
<a class="page-link" href="{{ path(route, query|merge({(pageParameterName): (pageCount - 1)})) }}">{{ pageCount -1 }}</a>
</li>
{% endif %}
{% endif %}
<li class="page-item">
<a class="page-link" href="{{ path(route, query|merge({(pageParameterName): pageCount})) }}">{{ pageCount }}</a>
</li>
{% endif %}
{% if next is defined %}
<li class="page-item">
<a class="page-link" rel="next" href="{{ path(route, query|merge({(pageParameterName): next})) }}">{{ 'label_next'|trans({}, 'KnpPaginatorBundle') }} »</a>
</li>
{% else %}
<li class="page-item disabled">
<span class="page-link">{{ 'label_next'|trans({}, 'KnpPaginatorBundle') }} »</span>
</li>
{% endif %}
</ul>
</nav>
{% endif %}
I believe you are looking for something like the code below. You need to adjust buttons formatting to your needs.
With standard Bootstrap CSS my example should render something alike to this:
Also make sure you adjusted the pagination template path in config/packages/knp_paginator.yaml to your's
IE: pagination: "your/path/inside/templates/directory/your_template.yaml"
{% if pageCount > 1 %}
items {{ firstItemNumber }} to {{ lastItemNumber }} of {{totalCount}}
{% if previous is defined %}
<a type="button" class="btn btn-secondary" rel="prev" href="{{ path(route, query|merge({(pageParameterName): previous})) }}">{{'<'}}</a>
{% else %}
<button type="button" class="btn btn-secondary" disabled><</button>
{% endif %}
{% if next is defined %}
<a type="button" class="btn btn-secondary" rel="next" href="{{ path(route, query|merge({(pageParameterName): next})) }}"> > </a>
{% else %}
<button type="button" class="btn btn-secondary" disabled> > </button>
{% endif %}
{% endif %}

No breadcrumb for custom view in Sonata Admin?

I created a custom view in Sonata Admin following recipe from official doc (here).
Everything is working good, but breadcrumb is empty (the same in last screenshot of recipe).
I googled a lot without success... Isn't it possible to display breadcrumbs with custom views in Sonata Admin?
I achieved this by extending sonata_breadcrumb block:
{% block sonata_breadcrumb %}
<div class="hidden-xs">
<ol class="nav navbar-top-links breadcrumb">
<li>
<a href="/admin">
<i class="fa fa-home"></i>
</a>
</li>
<li class="active">
<span>My Custom View</span>
</li>
</ol>
</div>
{% endblock sonata_breadcrumb %}
Can someone confirm it's the right way to do it?
Another option to show a custom breadcrumb is to define a block for it, like this:
{% extends '#SonataAdmin/standard_layout.html.twig' %}
{% block breadcrumb %}
<li>
<a href="/admin/dashboard">
<i class="fa fa-home"></i>
</a>
</li>
<li class="active">
<span>My Profile</span>
</li>
{% endblock %}
{#
More HTML/Twig code
#}

Twig recursive menu applied only to the second level

I have the following recursive macro which works fine but not the way i want it.
I want to display my menu like in this codepen, hover over services.
How can i get the same structure of the generated html ?
{% extends '::base.html.twig' %}
{% import _self as forms %}
{% macro recursive(item) %}
{% import _self as forms %}
<li class="nav-item">
{% if item.__children|length %}
{% if item.treeLevel == 0 %}
<a class="nav-link dropdown-toggle" href="http://example.com" id="navbarDropdownMenuLink"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ item.name }}
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
{% for item in item.__children %}
{{ forms.mega(item) }}
{% endfor %}
</div>
{% endif %}
{% endif %}
{% if item.__children is empty %}
<a class="nav-link" href="#">{{ item.name }}</a>
{% endif %}
</li>
{% endmacro %}
{% macro mega(item) %}
{% import _self as forms %}
<ul class="">
{% if item.__children|length %}
{% for item in item.__children %}
{{ forms.mega(item) }}
{% endfor %}
{% endif %}
<li>
<a class="dropdown-item" href="#">{{ item.name }}</a>
</li>
</ul>
{% endmacro %}
{% block body %}
{{ dump(tree) }}
{% if tree %}
<nav class="navbar navbar-toggleable-md navbar-light bg-faded">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse"
data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">Navbar</a>
<div class="collapse navbar-collapse" id="navbarNavDropdown">
<ul class="navbar-nav">
{% for item in tree %}
{{ forms.recursive(item) }}
{% endfor %}
</ul>
</div>
</nav>
{% endif %}
{% endblock %}
EDIT:
<nav class="navbar navbar-toggleable-md navbar-light bg-faded">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">Navbar</a>
<div class="collapse navbar-collapse" id="navbarNavDropdown">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Homepage</a>
</li>
<li class="nav-item">
<a class="nav-link dropdown-toggle" href="http://example.com" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Idea
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<ul class="">
<li>
<a class="dropdown-item" href="#">Company</a>
</li>
</ul>
<ul class="">
<ul class="">
<li>
<a class="dropdown-item" href="#">asdasd</a>
</li>
</ul>
<li>
<a class="dropdown-item" href="#">Services</a>
</li>
</ul>
<ul class="">
<li>
<a class="dropdown-item" href="#">Google</a>
</li>
</ul>
</div>
</li>
</ul>
</div>
</nav>

Bootstrap dropdown not working but used to

I have a bootstrap menu whith a dropdown. It used to work but something happened. I click on it and nothing happens.
<ul class="nav navbar-nav navbar-right">
<li class="{% if this.page.id == 'home' %}active{% endif %}">Főoldal</li>
{% if user %}
<li class="dropdown">
{{ user.name }}<span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<li>New post
</li>
<li class="divider"></li>
<li><a data-request="onLogout" data-request-data="redirect: '/good-bye'">Logout</a>
</li>
</ul>
</li>
{% else %}
<li class="">Login</li>
{% endif %}
</ul>
At the bottom:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="{{ 'assets/js/bootstrap.min.js'|theme }}"></script>
Update: Checked with the original example but still not working so the problem is somewhere in the js part. But the JS is there.
u missed a </ul> after {% endif %}
see http://jsfiddle.net/26vpc9o3/1/
<ul class="nav navbar-nav navbar-right">
<li class="{% if this.page.id == 'home' %}active{% endif %}">Főoldal</li>
{% if user %}
<li class="dropdown">
{{ user.name }}<span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<li>New post</li>
<li class="divider"></li>
<li><a data-request="onLogout" data-request-data="redirect: '/good-bye'">Logout</a></li>
</ul>
</li>
{% else %}
<li class="">Login</li>
{% endif %}
</ul>
Solved. The bootstrap js was included twice. I didnt noticed it because the bootstrap wasnt in its name.

Resources