Access to global parameters in Symfony2 and Twig - symfony

I would like to search some parameters in my parameters.yml file from a template in twig, depending on a variable. I have tried the following but it didn't work:
parameters.yml
twig:
globals:
status:
0: Paused
1: Running
2: Closed
template.html.twig
(game.status value can be 1, 2 or 3)
{% set var_status = game.status %}
{% set var_statustext = status.get(var_status) %}
<p>Status: {{ var_statustext }}</p>
Also I would like to access this parameters in the controller.
How could I do it? Thanks in advance.

You're looking for a way to access the value of the the global variable status (type => array) for a given key that is itself stored in another variable game.status (type => integer/string ).
Assuming game.status returns 1 ...
Then you can output Running using:
{{ attribute(status, game.status) }}
The attribute function is what you're looking for.

Related

sending customized salt event with cmd.run and onfail

I want to trigger custom named event if sls state fails.
I have following code:
check-if-needs-restarting:
{% if grains['os'] == 'CentOS' %}
cmd.run:
- name: needs-restarting -r
- onfail:
- cmd.run:
- name: salt-call event.send needs-restarting
{% endif %}
but somehow it crashes salt renderer:
An un-handled exception was caught by salt's global exception handler:
SaltRenderError: Could not locate requisite of [cmd] present in state with name
Any idea why? I tried fire_event instead but thn I dont have custom name I want "needs-restarting"
onfail like other global state arguments works only with states as arguments
See examples in the official documentation

Saltstack get grains of a minion in orchestration

I'm using salt-cloud to deploy VMs and I'm trying to get them registred in my DNS with the Saltstack Reactor system.
I have a reactor.conf with this trigger:
reactor:
- 'salt/cloud/*/created': # Add a VM
- /srv/reactor/initialize_vm.sls
initilize_vm.sls :
invoke_orchestrate_add_to_dns:
runner.state.orchestrate:
- mods: orch.add_to_dns
- pillar:
event_name: {{ name }}
event_profile: {{ profile }}
orch/add_to_dns.sls:
{% set name = pillar['event_name'] %}
{% set profile = pillar['event_profile'] %}
vm-add-dns-{{ name }}:
sqlite3.row_present:
- db: /var/lib/powerdns/pdns.sqlite3
- table: records
- where_sql: "name='{{ name }}' and type='A'"
- data:
domain_id: 1
name: {{ name }}
type: A
content: {{ ??? }}
ttl: 300
prio: 0
disabled: 0
I just need to know the IP address of the new minion. But as the orchestration run on the master, I can't just do a content: {{ grains['fqdn_ip4'] }}.
Any ideas to get minions informations ?
You can use the Salt Mine to get information from minions. To use the Salt Mine you need to know which minion you want to have information from. Luckily the reactor receives the data from the event bus. data['id'] contains the minion ID.
In the Salt Mine you can add a function to retrieve a minions IP like this:
mine_functions:
public_ip4:
- mine_function: grains.get
- fqdn_ip4
Now you can use mine.get in your sls files to get the IP address from a minion. In your case it will be the minion ID you just received from the event bus like this:
{%- for server, addrs in salt['mine.get'](data['id']', 'public_ip4') %}
{{ addrs[0] }}
{% endfor %}
Notes:
I don't use Salt Cloud and orchestration for DNS so I can't combine
your code with mine and check if it works. I hope it will be useful
for you :)
I named the mine public_ip4 instead of fqdn_ip4. It's to
clarify that this is a name in the Salt Mine and not the Grain you
are requesting.
If you don't wish to add configuration you can also do the following:
{% set ip = salt.saltutil.runner('salt.execute', [data['id']], {'fun': 'grains.get', 'kwarg': {'key':'fqdn_ip4'}})[data['id']] %}
Or as mentioned in the comments:
{% set ip = salt['saltutil.runner']('cache.grains', tgt=data['id'])[data['id']]['fqdn_ip4'][0] %}
Figured I'd add this here as a reference for others.

Is it possible to have two files of translation for the same language?

Can I have two translation files for the same language ? for example :
messages1.fr.yml
hello: "Salut"
messages2.fr.yml
here: "ici"
Yes, it's called translation domain
{{ "hello"|trans({},"messages1") }}
{{ "here"|trans({},"messages2") }}
To use it in controller it is the same logic:
the args are the key, the array of parameters then the translation domain
$this->get('translator')->trans('key', [], 'yourDomain);
In your case
$this->get('translator')->trans('hello', [], 'message1);
The translation documentation is here

Get base_path in twig file -- Drupal 8

How can i get the base path of drupal installation in twig file?
For example i need to get http://localhost/drupal/.
I have tried
{{ base_path }}
But it's returning empty.
{{ directory }}
This is returning the theme path. but i need the base path to complete the url. Is there any way to do this?
#Ditto P S To get base path on twig you declare a variable in .theme file inside a preprocess
andthen simply use 'base_path_success' variable in twig as
{{ base_path_success }}
You can declare variable inside any of the preprocess like
function ttnd_preprocess_node(&$variables) {}
OR
function ttnd_preprocess_block(&$variables) {}
Here ttnd is themename.
For more information you can use the below link
https://drupal.stackexchange.com/questions/205289/base-path-drupal-8
You can get the hostname, "localhost/drupal/", directly from the getHost() request:
$host = \Drupal::request()->getHost();
In some cases you might want to get the schema as well, fx http://localhost/drupal/:
$host = \Drupal::request()->getSchemeAndHttpHost();
Source : https://drupal.stackexchange.com/a/202811/

Get config data in Yaml (bespoke variables) in any Symfony2 controller

I want to do this in the main app/config/config.yml file:
variables:
a: 1
email: jim#email.com
variable2: hello
And this in any controller:
$variables = **get Yaml config data**
echo $variables['email'];
Of course I can't, but is anything like this possible?
I've seen where you can set global variables for access by Twig, but not seen how a Symfony2 controller can get them.
Of course if there's a better method as well then please mention that as this to me seems a good way of doing it.
Use parameters in parameters.yml for that:
parameters:
email: boss#acme.com
This way you'll be able to get it in a controller this way:
$email = $this->container->getParameter('email');
You can also create a Twig global in config.yml to have access to it from Twig using the same parameter:
twig:
globals:
email: %email%
And in Twig:
{{ email }}

Resources