Global Params in FOSUserBundle - symfony

I try to retrieve the global param that i define in parameters.yml and config.yml
in FOSUserBundle.en.yml under registration, in message i try to pass %myparam% and in my email.txt i try pass %myparam%:param like this
{{ 'registration.email.message'|trans({'%username%': user.username, '%confirmationUrl%': confirmationUrl,'%myparam%':myparam}, 'FOSUserBundle') }}
but it dosen't works.
and can i insert html inside yml and new line?
thanks

In your config.yml under fosuserbundle configuration.
confirmation:
enabled: true
template: MYMailBundle:Registration:email.html.twig
By doing this you are creating a twig template where you can render your variables and format your html how you want.
If you want to create a newline in your yml file I think you could do it by adding a \n.

Related

Twig global variable into symfony controller

In my Symfony 4 application I use a Twig global variable to store the name of my website. I need to fetch its value both into my templates and controllers.
twig:
globals:
site_title: My blog
I am able to get it inside my Twig templates : {{ site_title }}
In my controller, I tried $this->getParameter('site_title') but :
The parameter "site_title" must be defined.
Try this:
$twigglobals = $this->get("twig")->getGlobals();
and you can get content by:
$site_title = $twigglobals["site_title"];

Get the dbname on twig silex/symfony

I'm searching how i can get the "data base name" on my twig view on silex and symfony2 or 3.
I found that i can go on "app.db" and there is "_params" and "db name" witch are twig object and protected so i can access to it.
I have tried :
app.db._params.dbname
app.db.dbname
app.db.get('dbname')
app.db.get('_params')
app.db.get('params')
There is an other solution with out set the variable in the controller??
Thank in advance
You can inject global variables into templates like described in the docs
http://symfony.com/doc/3.1/cookbook/templating/global_variables.html
# app/config/parameters.yml
parameters:
database_name: name
# app/config/config.yml
twig:
globals:
app_database_name: '%database_name%'
{# default/index.html.twig #}
{{ app_database_name }}

Symfony2 twig translate string containing colon

How can I translate string containing colon in symfony2 using twig? Here's what I have:
Inside twig template:
{% trans with {'%order_no%': order_no} %}Loading offer: %order_no%...{% endtrans %}
Inside translation file:
Loading offer: %order_no%...: Įkeliamas užsakymas %order_no%...
As expected this doesn't work. How can I make it work?
You need to use quotes around the text as such:
'Loading offer: %order_no%...': Įkeliamas užsakymas %order_no%...
edited to show the correct example.

How to get current bundle in Symfony 2?

How can I detect in which bundle am I?
for exemple, when I'm in web.com/participants/list, I want to read "participants".
In order to get the bundle name in the controller:
// Display "AcmeHelloBundle"
echo $this->getRequest()->attributes->get('_template')->get('bundle');
And inside a Twig template:
{{ app.request.get('_template').get('bundle') }}
In order to get the controller name in the controller:
// Display "Default"
echo $this->getRequest()->attributes->get('_template')->get('controller');
And inside a Twig template:
{{ app.request.get('_template').get('controller') }}
In order to get the action name in the controller:
// Displays "index"
echo $this->getRequest()->attributes->get('_template')->get('name');
And inside a Twig template:
{{ app.request.get('_template').get('name') }}
AFAIK it's not yet possible (at least in a easy way). You should use reflection. I wrote a quick and dirty service to do get bundle name ang guess entity/repository/form names based on my conventions. Can be buggy, take a look at: http://pastebin.com/BzeXAduH
It works only when you pass a class that inherits from Controller (Symfony2). Usage:
entity_management_guesser:
class: Acme\HelloBundle\Service\EntityManagementGuesser
In your controller:
$guesser = $this->get('entity_management_guesser')->inizialize($this);
$bundleName = $guesser->getBundleName(); // Acme/HelloBundle
$bundleShort = $guesser->getBundleShortName(); // AcmeHelloBundle
Another possibility would be using kernel to get all bundles: Get a bundle name from an entity
Well you can get the controller of the current route by,
$request->attributes->get('_controller');
You can parse the bundle name from it.
You can get the bundle name in the controller simply like that:
// Display "SybioCoreBundle"
echo $this->getRequest()->attributes->get('_template')->get('bundle');
And inside a Twig template:
{{ app.request.get('_template').get('bundle') }}

How can I make a custom field type in symfony2?

I want to make a custom form field in symfony2 named daterange, which will extends the default symfony date type form field and take date range(start date and end date) into two different text-box.
Cause I don't like twig template engine this example only for PHP templating
What you need is to make:
New TestBundle\Form\Extension\Core\Type\DateRangeType which extends Symfony\Component\Form\AbstractType
Here you should:
a. write your own getParent, getName, buildForm methods
b. getParent return 'field'
c. getName return 'daterange'
d. buildForm has $builder->add('start', ...)->add('end', ...)->setAttribute('widget', 'daterange')
Add it to the DI (config.yml as example)
services:
form.type.daterange:
class: TestBundle\Form\Extension\Core\Type\DateRangeType
tags:
- { name: form.type, alias: daterange }
Create new widget for it in TestBundle/Resources/views/Form/daterange_widget.html.php
you can take date widget as example. Src/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/date_widget.html.php
Add to config (config.yml as example)
framework:
templating:
form:
resources:
- 'TestBundle:Form'
And for more widget customization as nefo_x said check form customization.
In order to do that, you need to add the following lines into app/config/config.yml
twig:
form:
resources:
- 'YourSuperBundle:Form:fields.html.twig'
then in src/Your/SuperBundle/Resources/views/Form/fields.html.twig:
{% extends 'form_div_layout.html.twig' %}
{% block daterange_widget %}
... do the customization.
{% endblock %}
For additional reference please read form customization of Symfony 2.0 book.
There is a good entry in official cookbook on creating custom field type

Resources