Jump to content

Template compiler bug?


tsdtsdtsd
 Share

Recommended Posts

Hi there,

I recently updated to PW 3 and I'm using the AUTO-mode for the compiler. Everything worked like a charm, but now I'm getting a strange behavior which I would classify as a bug.

I have the following in a template (which I'm including in the sites real template):

<script>
require(['hljs'], function(hljs) {
  hljs.initHighlighting();
});
</script>

Plain old HTML+JS.

Now, when I open up the site, the compiler made this:

<script>
 require(\ProcessWire\wire('files')->compile(['hljs'],array('includes'=>true,'namespace'=>true,'modules'=>true,'skipIfNamespace'=>true)), function(hljs) {
  hljs.initHighlighting();
</script>

Seems to me that the compiler touches something that it should not.

I couldn't find anything related here... bare with me if this is something well known.

 

Cheers

Link to comment
Share on other sites

The compiler, unfortunately, isn't really picky about things. It touches keywords like "require" and "include" inside and outside of the "real" PHP code, including comments.

So, the quick solution would be to load hljs the classic way (<script src=...>).

  • Like 2
Link to comment
Share on other sites

4 hours ago, tsdtsdtsd said:

I have the following in a template (which I'm including in the sites real template):


<script>
require(['hljs'], function(hljs) {
  hljs.initHighlighting();
});
</script>

Could this go in a linked JS file rather than directly in the template?

Link to comment
Share on other sites

You may also add a "namespace ProcessWire" into the first line of that template file and set the filecompiler to 0 / none for only that template.

But, if you started to work with PW3 now, and will do so in the future, I suggest to always use the namespace ProcessWire in your template files (and sub files) and skip the file compiler completly for all template related stuff!
Here, in template section, you have full control, other then in modules section, if you want to use third party modules that are not written as native PW 3 versions.

  • Like 2
Link to comment
Share on other sites

1 hour ago, horst said:

always use the namespace ProcessWire in your template files (and sub files) and skip the file compiler completly for all template related stuff!

"subfiles" here means partials to be included? And even more? Hanna code?

Link to comment
Share on other sites

1 hour ago, ottogal said:

"subfiles" here means partials to be included? And even more? Hanna code?

Yes, everything that you may somehow "include" into template files. Maybe, physically every php file under /site/templates/.

Link to comment
Share on other sites

Thanks for all the answers guys. I changed the JS structure a bit to solve my problem.

There are enough ways around this problem, but I still want to point out that I think that any kind of automation should be fail-safe as possible. A totally generic regex is not slightly fail-safe. It's not even code related, I could have "require(..." as a static text on a page.

Don't get me wrong, I don't want to bash PW. As I said, I think there are enough ways to work around this problem. But an automation should be sane or at least point out it's obvious traps and disadvantages in the first place. 

Anyway, cheers for your help!

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...