How to define map[string]object in yaml? - symfony

How to define map[string] object in YAML?
I have JSON object
{
"name": "SampleStore",
"books": {
"sample1": {
"author": "test1",
"prize": "1221"
},
"sample2": {
"author": "test2",
"prize": "890"
}
}
}
I had defined object in YAML as:
Types:
store:
name:
type: String
books:
Items:
Referemce: book_details
book_details:
author:
type: String
prize:
type: String
But this is syntax for list, I want map of book. How to define these type of map in YAML?

You should take a look at this:
stores:
-
name: SampleStore
books:
-
sample1:
author: test1
prize: 1221
-
sample2:
author: test2
prize: 1221
And you can try to run with this:
{%for store in stores %}
{{ store.name }}
{{ store.books|length }}
{%for book in store.books %}
{{ book }}
{% endfor %}
{% endfor %}
Will produce the output:
SampleStore
2
Array
Array
You could play with it at this link
I would suggest you to use a key for the book title also in order to facilitate the access (something like name: sample1).
Hope this help

Related

How to filter selected value using setcontent?

I am working on a Bolt template which returns different information about the listing and I have problems when trying to use setcontent filtering. I use URL queries to toggle the search, like so:
{% if app.request.query.get('onlyPaid') == 'true' %}
{% setcontent records = 'internships' where { is_paid: 1} limit 6 %}
{% endif %}
This filter works great because i have an is_paid field described in my contenttypes.yml
The question is, how do I apply the similar filter to the selected value of 'select' field?
city:
type: select
label: City
values: regions/{title}
sort: title
autocomplete: true
required: true
However this is not a perfect/straight solution, currently works by using "%like%" operator, as you can see below:
{% setcontent building_items = 'buildings' where { 'building_type': '%public%' } %}
The string 'public' is the selected value of the SELECT's field.
The definition of the field in the contenttypes.yaml is this:
building_type:
type: select
values:
residential: Residential
public: Public

Handlebars - accessing `this` within a conditional statement with #root

Is there any way to access this within a conditional?
To explain a bit moreā€¦ I have a loop over some items. I want to conditionally show the price if another value is set to something.
{
"firstName": "Terry",
"showPrice": "yes",
"items": [
{ "itemName": "Item 1", "itemPrice": "23.99" },
{ "itemName": "Item 2", "itemPrice": "50.99" }
]
}
{{#each items }}
<p>{{ this.itemName }}</p>
{{#equals #root.showPrice "yes"}}
<p>{{ this.itemPrice }}</p>
{{/equals}}
{{/each}}
The issue seems to be due to going back to the #root to check against the showPrice value, it then breaks this being accessible.
I've tried things like:
{{ this.itemPrice }} // doesn't work
{{ #root.this.itemPrice }} // doesn't work
{{ #root.items.this.itemPrice }} // doesn't work
{{ #root.items.[0].itemPrice }} // can now access, but only the 0 index
{{ #root.items.#index.itemPrice }} // doesn't work
Does anyone have any ideas?

Add array elements into variable in Twig

I stored some settings in array in Twig and I need to store them into some variable so I can print it. My array contains some data attributes like this:
{% set data = {
visible: { data: "data-visible-items", value: options.visible_items },
scroll: { data: "data-itemes-scroll", value: options.items_to_scroll },
speed: { data: "data-animation-speed", value: options.animation_speed },
infinite: { data: "data-infinite", value: options.infinite },
autoplay: { data: "data-autoplay", value: options.autoplay_enable },
interval: { data: "data-autoplay-interval", value: options.autoplay_interval },
hover: { data: "data-autoplay-hover", value: options.autoplay_hover },
} %}
Simply I want to store everything from array in one variable, in this variable it's need to be stored like this (separator is space) for example:
data-visible-items="5" data-items-scroll="2" data-animation-speed="400" data-infinite="0" data-autoplay="1" data-autoplay-interval="3000" data-autoplay-hover="1"
So, if the variable is for example attributes I just want to print it like this:
<div{{ attributes}}>
// Content
</div>
I wrote for loop like this:
{% for item in data %}
{{ item.data }} {{ item.value }}
{% endfor %}
and it will print each data and value, but how to store this in the variable in the way I described above?
If you want to store into a variable you can do this:
{% for item in data %}
{% set myvar = item.data ~ ' ' ~ item.value %}
{% endfor %}
If you want to transform that array you could make use of a Twig_Filter or Twig_Function
just chain http_build_query
PHP
$twig->addFunction(new Twig_SimpleFunction('http_build_query', http_build_query', ['is_safe' => [ 'html', ],]));
Twig
<div{{ http_build_query(attributes, '', ' ') }}>
Create the string yourself with a foreach
PHP
$twig->addFilter(new Twig_SimpleFilter('build_attribute_list', function (array $array) {
$str = '';
foreach($array as $key => $val) $str .= ' '.$key.'="'.$val.'"';
return $str;
}, ['is_safe' => ['html'],]);
Twig
<div{{ attributes|build_attribute_list }}>
(edit) Needless to say you can store the output in a variable as well
{% set my_var = attributes|build_attribute_list %}
{{ my_var }}

Symfony Easyadmin - How to add a custom action near "btn action-new"?

I would like to add a custom action near " btn action-new" in list page.
I try:
entities:
Pratiquant:
class: AppBundle/Entity/Pratiquant
actions:
- {name: 'fichePresence', type: 'method', action: 'fichePresence', label: 'fiche de presence' }
I don't need this:
entities:
Pratiquant:
class: AppBundle/Entity/Pratiquant
list:
actions:
- {name: 'fichePresence', type: 'method', action: 'fichePresence', label: 'fiche de presence' }
Hope someone understand me!
Your configuration is correct ... but it doesn't do what you want to achieve. Right now, all the actions configured for the list view are considered actions for the items displayed in the listing. There is no built-in way to define "global actions" for list view.
In any case, you can do what you want by overriding a small fragment of the list template. To do so, create the following Twig template (it's very important to store it in that exact location):
{# app/Resources/views/easy_admin/Pratiquant/list.html.twig #}
{% extends '#EasyAdmin/default/list.html.twig' %}
{% block view_actions %}
{{ parent() }}
Fiche de presence
{% endblock %}
This will execute the fichePresenceAction() method of your custom AdminController.
With EasyAdmin 3 :
public function configureActions(Actions $actions): Actions
{
$fichePresence = Action::new('fichePresence', 'fiche de presence', 'fa fa-download')
->linkToCrudAction('fichePresenceAction')
->createAsGlobalAction();
return $actions
->add(Crud::PAGE_INDEX, Action::DETAIL)
->add(Crud::PAGE_INDEX, $fichePresence);
}
See documentation

How to access a specific index in twig?

I'm trying to get a specific value from my object. I only want to print the first element in it, so I tried to treat it as an array by accessing it on the arrays position.
I also tried using the twig attribute function
However I can't get it to work.
{% for b in dotabets %}
{% for t in b.teams %}
You bet on: {{ t.name[0] }}
{% endfor %}
{% endfor %}
I have tried {{ t.name|first }} but it only returns the first letter of each item in the loop.
bet.orm.yml
AppBundle\Entity\Bet:
type: entity
repositoryClass: AppBundle\Entity\Repository\BetRepository
table: bets
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
createdDatetime:
type: datetime
column: created_datetime
betTime:
type: datetime
column: bet_time
status:
type: integer
length: 2
result:
type: integer
length: 2
nullable: true
homeOdds:
type: decimal
scale: 2
awayOdds:
type: decimal
scale: 2
closedDatetime:
type: datetime
column: closed_datetime
nullable: true
manyToOne:
game:
targetEntity: AppBundle\Entity\Game
manyToMany:
teams:
targetEntity: AppBundle\Entity\Team
joinTable:
name: bets_teams
joinColumns:
bet_id:
referencedColumnName: id
inverseJoinColumns:
team_id:
referencedColumnName: id
Controller
class IndexController extends ControllerBase
{
/**
* #Route("/", name="index")
* #Template()
*/
public function showMatchesAction()
{
$betRepo = $this->getEM()->getRepository('AppBundle:Bet');
$dotaBets = $betRepo->findDota2Matches();
$csgoBets = $betRepo->findCsGoMatches();
return [
'dotaBets' => $dotaBets,
'csgoBets' => $csgoBets,
];
}
To access the first object on an array in twig you can use the 'first' filter of twig itself:
See the Twig documentation about first for that.
Basically you'd do:
{{ b.teams|first }}
to access the first value of an array.
Your example won't work though, since {{ name }} is not defined anywhere as variable. If it is contained in team you'd rather go like that:
{{ b.teams[0].name }}
assuming you want the name of the first team in your collection, with index starting at 0

Resources