How can I group a twig array by value? - foreach

This is my twig code:
{% for page in output.pages %}
<li>{{page.name}}</li> <li>{{page.menu}}</li>
{% endfor %}
The output is
green colors
blue colors
yellow colors
orange colors
magenta colors
frog animals
elephant animals
I try to group this into the menu elements. The result I would need is:
colors
green
blue
yellow
orange
magenta
animals
frog
elephant
This is my approach:
{% for page in output.pages %}
{% for menu in page.menu %}
<ul>{{menu}}</ul>
<li>{{page.name}}</li>
{% endfor %}
{% endfor %}
But it is not grouping the pages by menu.

You can try something like this :
{% set ul = '' %}
{% for page in pages|sort((a, b) => a.menu <=> b.menu) %}
{% if ul == '' %} <ul> {% endif %}
{% if ul != page.menu %}
{% if ul != '' %} </ul></li> {% endif %}
{% set ul = page.menu %}
<li>{{ page.menu }}<ul>
{% endif %}
<li>{{page.name}}</li>
{% endfor %}
{% if ul != '' %} </ul></li></ul> {% endif %}
See twigfiddle

Related

Orderby Block .twig

Im sorry if this has been answered but I have been looking all over with out much luck.. im not a Craft Pro coder by any means. I'm trying to order my block by dateCreated. I have tried adding 'orderby' : (dateCreated asc) with no luck..
{% set popularTopic = craft.entries.section('blogPosts').search( block.topicKeyword ) %}
{% if entry.slug != 'naccho-voice' %}
{% set popularTopic = craft.entries.section('blogPosts').relatedTo(entry.id) %}
{% endif %}
{% include '/widgets/_topics' with {'entries': popularTopic, 'title': block.heading, 'backgroundColor': entry.blogSectionColor, 'limit': 5 } %}
{% set popularTopic = craft.entries.section('blogPosts').search( block.topicKeyword ) %}
{% if entry.slug != 'naccho-voice' %}
{% set popularTopic = craft.entries.section('blogPosts').relatedTo(entry.id) %}
{% endif %}
{% include '/widgets/_topics' with {'entries': popularTopic, 'title': block.heading, 'backgroundColor': entry.blogSectionColor, 'limit': 5,'orderby' : (dateCreated asc) } %}

How to print url of blog tags in drupal

How to print url alias of blog tags in Drupal?
uri.value is not working
{% for item in node.field_tags %}
{{ item.entity.uri.value }} {{ item.entity.name.value }}
{% endfor %}
{{ item.entity.uri.value }} # this isn't working
You can use path() function like below to print url alias of the tags:
{% for item in node.field_tags %}
{{ path('entity.taxonomy_term.canonical', { 'taxonomy_term': item.entity.id() }) }}
{% endfor %}
or url() if you want an absolute url:
{% for item in node.field_tags %}
{{ url('entity.taxonomy_term.canonical', { 'taxonomy_term': item.entity.id() }) }}
{% endfor %}

How to get Key=>Value in Salt

in pilars i have
...
opt:
miimon: 100
updelay: 0
downdelay: 0
mode: 802.3ad
...
And i want dynamically change this in sls file
{% for device, args in pillar['machine_id'][grains['machine_id']]['network'].items() %}
{{device}}:
...
{% if args['type'] == 'bond' %}
{% if 'opt' in args %}
- miimon: {{args['opt']['miimon']}}
- updelay: {{args['opt']['downdelay']}}
- downdelay: {{args['opt']['downdelay']}}
- mode: {{args['opt']['mode']}}
{% endif %}
{% endif %}
...
{% endfor %}
Here what i'm search completed on php
foreach ($arr as $key => $value) {
echo "Key: $key; Value: $value<br />\n";
}
I found solution.
{% if args['type'] == 'bond' %}
{% if 'opt' in args %}
{% for key, value in args['opt'].iteritems() %}
- {{ key }}: {{ value }}
{% endfor %}
{% endif %}
{% endif %}
New in 3005 there's a filter that does this transformation for you:
{% if args["type"] == "bond" %}
{{ args.get("opt", {}) | dict_to_sls_yaml_params | indent }}
{% endif %}

FullCalendar eventDragStart ui is empty

I set up a fullcalendar with a eventStartDrag. When I do a log of the UI variable in the event, it seems to be empty. Is there any reason why ? All the other variables that are passed are ok.
var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
$('#calendar').fullCalendar({
header: {
left: 'prev,next',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
defaultView: 'agendaWeek',
editable: true,
droppable: true, // this allows things to be dropped onto the calendar !!!
drop: function (date) { // this function is called when something is dropped
// retrieve the dropped element's stored Event Object
var originalEventObject = $(this).data('eventObject');
// we need to copy it, so that multiple events don't have a reference to the same object
var copiedEventObject = $.extend({}, originalEventObject);
// assign it the date that was reported
copiedEventObject.start = date;
$('#calendar').fullCalendar('renderEvent', copiedEventObject, true);
// is the "remove after drop" checkbox checked?
if ($('#drop-remove').is(':checked')) {
// if so, remove the element from the "Draggable Events" list
$(this).remove();
}
},
eventResize: function (event, delta, revertFunc) {
event.isChanged = true;
},
eventDrop: function (event, delta, revertFunc) {
event.isChanged = true;
},
eventDragStart: function (event, jsEvent, ui, view) {
console.log(event);
console.log(jsEvent);
console.log(ui);
console.log(view);
var dragged = [ui.helper[0], event];
},
events: [
{% for timeEntry in timeEntries %}
{
id: {{ timeEntry.id }},
title: '{{ timeEntry.task.description }}',
taskId: '{{ timeEntry.task.id }}',
start: '{{ timeEntry.startTime|date("Y-m-d\\TH:i:s") }}',
{% if timeEntry.stopTime is not null %}
end: '{{ timeEntry.stopTime|date("Y-m-d\\TH:i:s") }}',
{% else %}
end: null,
{% endif %}
isNew: false,
isChanged: false
},
{% endfor %}
]
});
As of version 2.1.1, jQueryUI is no longer a dependency (see release notes).
As the method signature kept the same, you will see in the source code that it always passes an empty object:
//(... fullCalendar 2.1.1 source code ...)
// line 4544
dragStart: function(ev) {
_this.triggerSegMouseout(seg, ev); // ensure a mouseout on the manipulated event has been reported
_this.isDraggingSeg = true;
view.hideEvent(event); // hide all event segments. our mouseFollower will take over
view.trigger('eventDragStart', el[0], event, ev, {}); // last argument is jqui dummy
},
When the eventDragStart is triggered the last argument is jqui dummy

How to add values to Jquery UI Autocomplete text input

So I have a Jquery UI Autocomplete widget in my Jinja2 template which works great. However, I want the value of each program to be the program ID, not the name. IE: {{ p.id }} How do I set the name as the label and the id as the value?
<script>
$(function() {
var programs = [
{% for p in programs %}
'{{ p.Name }}',
{% endfor %}
];
$( "#programs" ).autocomplete({
source: programs
});
});
</script>
<input type="text" name="program" id="programs" />
OK, this works!
The UI Autocomplete input is populated by the label attribute. The hidden_val attribute sets the hidden input with the select event.
<script>
$(function() {
var programs = [
{% for p in programs %}
{
hidden_val: "{{ p.id }}",
label: "{{ p.Name }}"
},
{% endfor %}
];
$( "#programs" ).autocomplete({
delay: 0,
source: programs,
select: function(event, ui){
$( "#program_val" ).val(ui.item.hidden_val);
}
});
});
</script>
<input type="text" id="programs" />
<input type="hidden" id="programs_val" />
Take a look at http://grover.open2space.com/content/using-jquery-autocomplete-ids that should do the trick!
Basically you supply the autocomplete textbox a list of name|id items. It will then show the name, and you only have to modify the textbox to put the id into a hidden form element using the .result() function. Like this:
$("#mytextbox")
.autocomplete(data)
.result(
function (evt, data, formatted)
{
$("#hiddenIDbox").val(data[1]);
}
);
This kind of works, but as soon as the autocomplete suggestion is selected, the item id appears in the input field.. confusing for the users, I guess. Any other ideas?
<script>
$(function() {
var programs = [
{% for p in programs %}
{
value: "{{ p.id }}",
label: "{{ p.Name }}"
},
{% endfor %}
];
$( "#programs" ).autocomplete({
source: programs
});
});
</script>

Resources