If I created a service is there a way to access it from twig, without creating a twig.extension?
You can set the service a twig global variable in config.yml, e.g
#app/config/config.yml
twig:
globals:
your_service: "#your_service"
And in your template.html.twig file you can invoke your service this way:
{{ your_service.someMethod(twig_variable) }}
See here.
To get this done in Symfony 5, first you must declare the service in services.yaml, for example:
App\Service\NavigationHelper:
arguments:
foo: bar
Then you can declare the service for its use in Twig. To achieve this, you must add it as a variable in the "globals" section of the Yaml file located in packages/twig.yaml:
globals:
navHelper: '#App\Service\NavigationHelper'
Now you can use your service methods from the templates as Mun Mun Das suggested in his last code snippet.
Related
I'm looking for a way to access configuration parameters in twig template of the admin module. I tried to access it by:
{{ shopware.config.pluginName.config.fieldName }}
The problem is that in the twig template of the admin module {{ shopware.config }} isn't available. I tried to access it by {{ dump(shopware.config) }} but it returns nothing.
Am I doing something wrong?
Is there alternative to access plugin's configuration parameters in the admin module?
Is there a way to pass php variables to twig template of the admin module?
Thanks in advance,
Cheers!
Try to use following service:
this.systemConfigApiService.getValues('domain', this.selectedSalesChannelId)
where domain is the Plugin name.
Of course, you should inject that service first
inject: ['systemConfigApiService']
Using that service you can provide config value to twig template.
I'm using a service as twig global variable. In the service constructor I set a default value of the property $title. It works initially.... Twig render the property value using the command {{ service.getTitle() }} in a template file. But after update the service property by the controllers constructor and rendering the view, the value is not updated at screen. The goal is set a twig global variable by the controllers to render in all views. How to do it?
twig.yaml
twig:
globals:
pageMap: "#Base.PageMap"
services.yaml
services:
Base.PageMap:
class : App\Bundle\Base\Services\PageMap
public: true
controller
public function __construct(PageMap $pageMap)
{
$pageMap->setTitle('Registration listing');
}
twig template:
<div class="title">{{ pageMap.getTitle() }}</div>
Twig globals are setup at init time and compiled/cached for the duration of an execution.
If you want to update and be able to call things dynamically, you should create a RuntimeExtension (see documentation here : https://symfony.com/doc/current/templating/twig_extension.html#creating-lazy-loaded-twig-extensions)
Calling it from your template will be a little more expensive (but more correct !)
I have Pagerfanta installed and working, however I am having difficulty in customising the layout. I read on Github that I need to pass through my_template, however I am unsure where this should be configured and what specificially this refers to.
Custom template
If you want to use a custom template, add another argument
<div class="pagerfanta">
{{ pagerfanta(my_pager, 'my_template') }}
</div>
Ideally I would like to have my own Twig template that I can modify, however I don’t know if Pagerfanta supports this. Is it all done in PHP?
I don't think it supports Twig templates but for sure you can write your custom Template class to render the pagination however you want.
Let's say in your AppBundle, you will need to create MyCustomTemplate class which should extend Pagerfanta\View\Template\DefaultTemplate:
<?php
namespace Acme\AppBundle\Template;
use Pagerfanta\View\Template\DefaultTemplate;
class MyCustomTemplate extends DefaultTemplate
{
// override whatever you need here ...
}
then register it in your services.yml file together with the view service:
services:
acme_app.template.my_template:
class: Acme\AppBundle\Template\MyCustomTemplate
pagerfanta.view.my_template:
class: Pagerfanta\View\DefaultView
public: false
arguments:
- "#acme_app.template.my_template"
tags: [{ name: pagerfanta.view, alias: my_template }]
then in your Twig templates you will be able to use:
{{ pagerfanta(my_pager, 'my_template') }}
which will result in displaying your custom pagination template.
my_template is the alias of your view. The next section in the Github link you have provided explains more.
It would look something like this
services:
app.view.my_template:
class: App\View\MyView
public: false
tags: [{ name: pagerfanta.view, alias: my_template }]
I have a service called General, this is the configuration :
services:
app_bundle.general:
class: AppBundle\Services\General
I'm trying to set this service as a global variable for my twig templates, so in config.yml I added (like the documentation say) :
parameters:
general_service: "#services.app_bundle.general"
But with this I have this error : You cannot dump a container with parameters that contain references to other services (reference to service "services.app_bundle.general" found in "/general_service").
How can I set my service to a global variable for Twig ? And in twig, use it like : {{general_service.myMethod()}}
Thanks !
As Artamiel suggested, add your service to the twig engine globals:
#app/config/config.yml
twig:
globals:
general_service: "#app_bundle.general"
Please note: your service has the id app_bundle.general and not services.app_bundle.general as indicated in your example.
How to get a variable in symfony2 controller from parameters file app/config/parameters.yml.
parameters:
host_name: www.mydomain.con
You could do:
$this->container->getParameter('host_name');
Check this out
$this->container->getParameter('host_name');
And if you want in twig then try like below
Add in app/config/config.yml like:
twig:
globals:
host_name: '%host_name%'
and in twig file use as normal variable like:
{{host_name}}
Enkoy!
You will get like this
$this->container->getParameter('host_name');