invictus
invictus

Reputation: 825

Silverstripe 3 - RenderWith can't find template

I want to create a PDF with DOMPdf. My PDF Template is located in

themes/Template/
themes/Template/templates
themes/Template/templates/Pdf
themes/Template/templates/Layout

But I receive a template not found error.

According to this Question silverstripe Sitetree onAfterWrite - renderWith Error: Template not found I put the template into mysite/templates/Pdf and it works.

Is there a way to keep the template in the theme folder?

Here's my code.

public function createPdf(){
        $pdf = new SS_DOMPDF();
        
        $data = array(
        
        );
        
        $pdf->setHTML($this->customise($data)->renderWith('InvOffPdf'));
        $pdf->render();
        
        $pdfFilename = str_replace(array(' ', '#'), '', $this->Title) . '.pdf';
        $pdfFolder = 'clientcenter/' . $this->Client()->CID . '-' . $this->Client()->Random;
        
        $pdf->toFile($pdfFilename, $pdfFolder);
        
        $filename = 'assets/' . $pdfFolder . '/' . $pdfFilename;

        $file = File::get()->Filter('Filename', $filename)->First();
        if( $file ){
            $file->ClientID = $this->ClientID;
            $file->ShowInSearch = 0;
            $file->write();
            
            $this->Download = '<a href="' . $filename . '">PDF herunterladen</a>';
            $this->write();
        }
    }

edit: I've tried spekulatius' solution. but I've done something wrong.

$pdfTemplate = SSViewer::setTemplateFile('Layout', 'InvOffPdf');
$pdf->setHTML($this->customise($data)->renderWith($pdfTemplate));

This is the error I receive

[Strict Notice] Non-static method SSViewer::setTemplateFile() should not be called statically, assuming $this from incompatible context

What's the correct syntax?

Upvotes: 1

Views: 1055

Answers (1)

spekulatius
spekulatius

Reputation: 1499

Instead of giving renderWith a string and Silverstripe searches for the template file itself you could give renderWith a instance of SS_Viewer http://api.silverstripe.org/3.1/class-SSViewer.html and set the template file you want to use with setTemplateFile.

This way you build the Viewer and define how the rendering should look like to avoid Silverstripe looking in the wrong place.

Edit to explain it more detailed. I had more this in mind:

$pdfTemplate = new SSViewer();
$pdfTemplate->setTemplateFile('themes/Template/templates/Pdf/InvOffPdf.ss');
$pdf->setHTML($this->customise($data)->renderWith($pdfTemplate));

Untested, of course.

Upvotes: 1

Related Questions