processwire / processwire-issues

ProcessWire issue reports.
45 stars 2 forks source link

File Compiler modules do not work when ProcessWire namespace is present #882

Open Toutouwai opened 5 years ago

Toutouwai commented 5 years ago

Short description of the issue

ProcessWire v3.0 introduced a new type of module: File Compiler modules that extend the FileCompilerModule class. It also included a new module FileCompilerTags.

This issue is about the fact that File Compiler modules do not work when template files include the ProcessWire namespace.

To illustrate with FileCompilerTags...

basic-page.php without namespace:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title><?php echo $page->title; ?></title>
        <link rel="stylesheet" type="text/css" href="<?php echo $config->urls->templates?>styles/main.css" />
    </head>
    <body>
        <h1>{title}</h1>
        <?php if($page->editable()) echo "<p><a href='$page->editURL'>Edit</a></p>"; ?>
    </body>
</html>

Result: 2019-05-18_113200

basic-page.php with namespace:

<?php namespace ProcessWire; ?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title><?php echo $page->title; ?></title>
        <link rel="stylesheet" type="text/css" href="<?php echo $config->urls->templates?>styles/main.css" />
    </head>
    <body>
        <h1>{title}</h1>
        <?php if($page->editable()) echo "<p><a href='$page->editURL'>Edit</a></p>"; ?>
    </body>
</html>

Result: 2019-05-18_113309

The introductory blog post, the comments in FileCompilerModule.php, and the comments in FileCompilerTags.module do not mention that it is necessary for the ProcessWire namespace to be absent. And IMO it shouldn't be necessary - a person might include the ProcessWire namespace to get code completion and documentation in their IDE and this shouldn't impact on whether they can use File Compiler modules.

Setup/Environment

Toutouwai commented 5 years ago

I guess this issue relates to the the template setting "Use Compiled File?" which defaults to "Auto (compile when file has no namespace)".

I see now that the description for this field does mention FileCompiler modules, but I think this setup is not ideal. The "Auto" setting is probably unwanted when users have a FileCompiler module installed and it doesn't seem reasonable to expect these users to go through the settings for every existing template to change this, and to remember to change it for every new template they add.

Also, setting "Auto (compile when file has no namespace)" as the default seems to treat the FileCompiler as if it is only about adding the ProcessWire namespace, when the blog post that introduced the FileCompiler says that it is intended to be about more than just the namespace:

There are other reasons to use compiled template files. A new class of modules is now supported by 3.x, called FileCompiler modules.

Because FileCompiler modules have nothing to do with the PW namespace it doesn't seem right that they are subject to a default setting that leaves them ineffective when the PW namespace is present. Could the template compilation settings distinguish between the core FileCompiler use which is about the namespace, and FileCompiler modules? So maybe if one or more FileCompiler modules are installed there are additional options to disable the FileCompiler modules for that template, but FileCompiler modules will compile templates (and files included from them) by default.

As you may have guessed, I'm developing a FileCompiler module but having the module inactive on every template by default (I think most users are adding the namespace to their template files by now) makes the idea a lot less workable.

Toutouwai commented 5 years ago

I'm still struggling with this issue. A couple of additional notes:

1. The fact that /site/init.php, /site/ready.php and /site/finished.php are included with the skipIfNamespace FileCompiler option forced to true is frustrating. What if I want these files to be compiled (to trigger my FileCompiler module or hooks to FileCompiler) but I also want to include a namespace declaration for the sake of my IDE? As mentioned above, the FileCompiler is supposed to be about more than just adding a namespace.

The skipIfNamespace option should be configurable somewhere, via a $config option at least but also ideally in a way that automatically responds to the presence of FileCompiler modules where it's unlikely that the user will want skipIfNamespace to be true. And it would be good if there was some way to set the protected $options array for FileCompiler so these could be set in hooks before compile, etc.

2. The PhpDoc comments for $config->templateCompile seem to be inaccurate. The comments say:

Set to false do disable the option for compiled template files. 
When set to true, it will be used unless a given template's 'compile' option is set to 0.

To me that reads as if templates will be compiled (and the compiled file used for template rendering) if $config->templateCompile is true and the template's compile option is not set to 0 (i.e. the "No" option in the template settings). But actually that's not the case - if a template file has a namespace declaration and the template compile setting is "Auto" then the uncompiled template file is used even when $config->templateCompile = true.