【问题标题】:Silverstripe 3 - RenderWith can't find templateSilverstripe 3 - RenderWith 找不到模板
【发布时间】:2014-09-17 23:16:26
【问题描述】:

我想用 DOMPdf 创建一个 PDF。我的 PDF 模板位于

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

但是我收到一个找不到模板的错误。

根据这个问题silverstripe Sitetree onAfterWrite - renderWith Error: Template not found,我将模板放入mysite/templates/Pdf,它可以工作。

有没有办法将模板保存在主题文件夹中?

这是我的代码。

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();
        }
    }

编辑: 我试过 spekulatius 的解决方案。但我做错了什么。

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

这是我收到的错误

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

正确的语法是什么?

【问题讨论】:

  • 您确定Template 是活动主题吗? renderWith 只会查看当前处于活动状态的主题文件夹。
  • 是的,它在活动主题中

标签: silverstripe


【解决方案1】:

而不是给 renderWith 一个字符串和 Silverstripe 搜索模板文件本身,你可以给 renderWith 一个 SS_Viewer http://api.silverstripe.org/3.1/class-SSViewer.html 的实例并设置你想要的模板文件与 setTemplateFile 一起使用。

通过这种方式,您可以构建查看器并定义渲染的外观,以避免 Silverstripe 看错地方。

编辑以更详细地解释它。我有更多的想法:

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

当然,未经测试。

【讨论】:

  • 我试过了,并发布了我的尝试。你能告诉我有什么问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多