[SOLVED] Loading CSS file in template

Hi everybody!

Here’s what I’m trying to do:
In one action, I want to output a PDF as content. The PDF gets its content from a template.

My action code:

public function printDocumentTemplateAction()
{
    $view = new StandaloneView();
    $view->setTemplatePathAndFilename("resource://XX.YY/Private/Templates/Document/Template.html");
    $view->setLayoutRootPath("resource://XX.YY/Private/Layouts/");

    $content = $view->render();

    ... creating PDF ...
}

Private/Templates/Document/Template.html:

<f:layout name="Document.html" />

Private/Layouts/Document.html:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <link href="{f:uri.resource(path: 'CSS/documentTable.css')}" rel="stylesheet" />
    </head>
    <body>
    </body>
</html>

You see, in the layout file, I’m trying to import a CSS file. Using this file in my Default layout (so using standard actions without setting any templates, template rooth pathes etc), it works.
However, in this case I’m getting the error:

Invalid resource URI “resource:///Public/CSS/documentTable.en.css”: Package “” is not available.

Nested Exception

Package “” is not available.

Does anyone have any idea what I’m doing wrong here? Thank you very much for your support!

The template rendering is called outside of a renderingContext, so the template doesn’t know the package name.
You can provide that with a package: 'Vendor.Package' attribute in the resource viewhelper, similar to the path: '...' attribute

Oh perfect - I didn’t expect such a simple solution. Again, I learned a lot. Thank you very much for your great help!

1 Like

Just for future readers, this happens because the standalone view knows nothing about the “current” action / controller / package otherwise it might fill in the information automatically so you could omit it.

It’s not exactly easy to set that for the standalone view though, it would require you to set a ControllerContext with a request pointing to the package. While that is possible it’s rather cumbersome and somewhat error prone.

So that is the reason why it behaves a bit different than in a “non standalone” View.

I believe that was the same thing I pointed out ? :slight_smile:

1 Like

Right shoudl’ve phrased that better, I just wanted to expand a bit on that :slight_smile: