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 }]
Related
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 !)
How can I link a twig template? Something like this:
Learn more
Is there a way to do this or do I have to use URL rewriting?
You can use template controller for this. Just add new route, specify template controller FrameworkBundle:Template:template and twig template.
routes.yml
qwerty:
path: /qwerty
defaults:
_controller: FrameworkBundle:Template:template
template: Events.twig
and change this link to:
Learn more
http://symfony.com/doc/current/templating/render_without_controller.html
Your question is conceptually flawed. Twig is a templating language, and cannot be rendered as a web page on its own.
You probably don't want to link directly to a .twig file (in fact, .twig files should not be directly be accessible in your public document root at all)! Rather, you need some PHP code (either a standalone script, or preferably, a front controller route) that renders your template in the context of your application, and returns the fully rendered page at a URL that you have specified.
For example, in the Slim framework, you might do something like:
// Render Twig template in route
$app->get('/events', function ($request, $response, $args) {
return $this->view->render($response, 'events.html.twig');
});
// Run app
$app->run();
See this article for more information on moving away from procedural spaghetti code and thinking in MVC terms.
If you are using twig the template will need to be parsed, so you cannot link in the way you have tried. In Symfony 2 you would use something like {{ path('name_of_path') }}, you will need to name your route first though.
You can name the route with name="event" with Annotations:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
/**
* Matches /event exactly
*
* #Route("/event", name="event")
*/
public function eventAction()
{
// ...
}
In the twig template link with this:
Event
More info here:
http://symfony.com/doc/current/templating.html#linking-to-pages
You can't. Make another controller action that renders Events.twig and link to the respective route using path():
Learn more
https://symfony.com/doc/current/controller.html#a-simple-controller
http://symfony.com/doc/current/routing.html
I was successful with getting Symfony CMF and Auto Routing Bundle (and a custom contoller) to load documents in the following manner (path):
.../category/article-name
This works fine. But I need something like this, easily achieved by standard Symfony2:
.../category/article-name/{page}
Where page is passed to a controller as a parameter,
eg. $page = 2 -> controller processes content of the Article document to display only given page
or $page = null -> (so no {page} parameter at all) - as above, but displayed is page default that is 1
How to make routing-auto bundle of the Symfony CMF to use parameters in routes?
So that the basic path is exactly as it is configured as in this Auto Routing Bundle configuration, but additionally I can use parameters passed to a controller, so that additional decisions can be made upon them. Any hints? Thanks!
My config for auto routing:
MyApp\MyCmsBundle\Document\Article:
content_path:
#fixed path of all categories and therefore articles
articles_categories_path:
provider: [specified, { path: /cms/routes }]
exists_action: use
not_exists_action: throw_exception
#category path
category_path:
provider: [content_method, { method: getRouteNodeCategory }]
exists_action: use
not_exists_action: throw_exception
#article name
content_name:
provider: [content_method, { method: getTitle }]
exists_action: [auto_increment, { pattern: -%d }]
not_exists_action: create
MyApp\MyCmsBundle\Document\ArticlesCategory:
content_path:
#fixed path of all categories and therefore articles
articles_categories_path:
provider: [specified, { path: /cms/routes }]
exists_action: use
not_exists_action: throw_exception
#category name
content_name:
provider: [content_method, { method: getTitle }]
exists_action: use
not_exists_action: create
getRouteNodeCategory() of the Article Document just returns a parents (i.e. category) name. Here's part of this document content:
public function getRouteNodeCategory()
{
return $this->parent->getTitle();
}
This is not currently possible in the RoutingAutoBundle but it could be easily implemented.
The RoutingAutoBundle creates Route documents which extend the CMF's Route object. These Route objects do support variable patterns which allow you to specify dynamic paramters:
https://github.com/symfony-cmf/RoutingBundle/blob/1.2/Model/Route.php#L53
So we would just need to extend the mapping to include details on dynamic (variable) parameters in the URL.
Note also that the a new version of the routing auto bundle will soon be released which features a much better configuration format.
If you could create an issue detailing your use case, we will try and implement it for the 1.0 release.
I want to make a custom view an editable entity.
By default dashboards showing links to edit entities `(/links, where controller is CRUDController::listAction)
It is looks like:
But I need to show on main dashboard's page an enities's table like:
I have block to display on dashbord(service).
services:
sonata.block.service.date:
class: My\AuditBundle\Block\DateBlockService
arguments: ['sonata.block.service.date',#templating,#sonata.admin.pool,#service_container]
tags:
- { name: sonata.block }
How can I show table into block?
Thanks.
You need to create a service to be injected in a block you can display on homepage
there is a script how to create a block in sonata admin homepage here :
SonataAdminBundle : display non crud (statistics)
and the sonata block bundle documentation :
http://sonata-project.org/bundles/block/master/doc/reference/your_first_block.html
once you have created your block you just need to override the base_list template into your custom block template :)
hope this will help
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.