Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

how to add a custom twig template ? #98

Closed
ibasaw opened this issue Oct 16, 2014 · 5 comments
Closed

how to add a custom twig template ? #98

ibasaw opened this issue Oct 16, 2014 · 5 comments

Comments

@ibasaw
Copy link

ibasaw commented Oct 16, 2014

I would like to override the default template, and custom it with a template.html.twig file in my root views folder

Can't find anything on internet about it...

@pablodip
Copy link
Contributor

You can have a look at Pagerfanta's views: https://github.com/whiteoctober/Pagerfanta#views

@KnutSv
Copy link

KnutSv commented Feb 23, 2015

This does not really answer the question. I've spent countless hours trying and failing to create a custom template. If it is actually possible does anyone have a working example that we can look at?

From what I've gathered I can add a pagerfanta.yml file in my bundle's Resources/config folder:

services:
    pagerfanta.view.{{my_view}}:
        class: {{Company}}\{{Bundle}}\View\{{MyView}}View
        public: false
        tags: [{ name: pagerfanta.view, alias: default }]

I then add a {{MyView}}View.php-file in my bundles View folder:

<?php
namespace Pagerfanta\View;

use {{Company}}\{{Bundle}}\View\Template\{{MyView}}Template;

class {{MyView}}View extends DefaultView
{
    protected function createDefaultTemplate()
    {
        return new {{MyView}}Template();
    }

    protected function getDefaultProximity()
    {
        return 3;
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return '{{my_view}}';
    }
}

And then in my bundles View/Template folder, I add a fike {{MyView}}Template.php

<?php

namespace Pagerfanta\View;

/**
 * @author Pablo Díez <[email protected]>
 */
class {{MyView}}Template extends Template
{
    static protected $defaultOptions = array(
        ...
    );

   ...
}

In my template I try to include it like this:

{{ pagerfanta( pagerFolder, '{{my_view}}')}}

This code gives me the error:
An exception has been thrown during the rendering of a template ("The view "{{my_view}}" does not exist.")

Most of this is not really documented, which is why I've had to sniff around the source code and guess how it works. Would be great with a demo template.

@pinguin-wizard
Copy link

If anybody is looking for answer to ibasaw's question, here is how you use custom views with this bundle:

1. Create View class (./src/Foo/BarBundle/View/MyView.php):
namespace Foo\BarBundle\View;
use Pagerfanta\View\DefaultView;
use Foo\BarBundle\View\Template\MyTemplate;

class MyView extends DefaultView {
    protected function createDefaultTemplate() {
        return new MyTemplate();
    }

    protected function getDefaultProximity() {
        return 3;
    }

    /**
     * {@inheritdoc}
     */
    public function getName() {
        return 'my';
    }
}
2. Create Template class (./src/Foo/BarBundle/View/Template/MyTemplate.php):
<?php
namespace Foo\BarBundle\View\Template;

class MyTemplate extends \Pagerfanta\View\Template\Template {

// copy contents of class from  ./vendor/pagerfanta/pagerfanta/src/Pagerfanta/View/Template/TwitterBootstrapTemplate.php
// and adjust to your preferences

}
3. Add your new view class as service (./src/Foo/BarBundle/Resources/config/services.yml):
services:
    pagerfanta.view.my:
        class: Foo\BarBundle\View\MyView
        public: false
        tags:
            - { name: 'pagerfanta.view', alias: 'my' }
4. Use in your twig template:
{{ pagerfanta(pager, 'my') }}

@KirbyFox
Copy link

@mirodav could you please update your answer for Symfony 4?

@baris1892
Copy link

baris1892 commented Jan 6, 2018

@KirbyFox: The above example should already work for SF 4!?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants