How to import the Javascript code from php file in plugin

<?php

use Kirby\Cms\App as Kirby;

Kirby::plugin('cookiebanner/kirby-cookie-plugin', [
    'blueprints' => [
        'cookiebannertab' => __DIR__ . '/blueprints/tabs/cookiebannertab.yml'
	],
    'snippets' => [
        'cookiebanner' =>  __DIR__ . '/snippets/cookiebanner.php'
    ],
    'translations' => [
        'de' => require __DIR__ . '/translations/de.php',
        'en' => require __DIR__ . '/translations/en.php',
    ],
]);

It seems that ‘blueprints’ and ‘translations’ work, but the file ‘cookiebanner’ => DIR . ‘/snippets/cookiebanner.php’ does not work. There is JS code in DIR . ‘/snippets/cookiebanner.php’, like:

<script>
    console.log("run here");
    <?php if($site->... == true): ?>
</script>

But the console log information will not show, how to import or use the javascript code in php file correctly in plugin?

Unless your path is not correct, the script should run if you include the snippet somewhere. If you put anything else into that snippet, like just some text, does this work?

Thanks for the reply. I added some text in the cookiebanner.php file, but it does not show.

Have you check your file path, spelling issues etc. Where do you include the snippet, please show your code.

The code structure is like this:

Plugin
  assets/
  blueprints/
    tabs/
      cookiebannertab.yml
  snippets/
    cookiebanner.php
  translations/
    de.php
    en.php
index.php
    

index.php

<?php

use Kirby\Cms\App as Kirby;

Kirby::plugin('cookiebanner/kirby-cookie-plugin', [
    'blueprints' => [
        'cookiebannertab' => __DIR__ . '/blueprints/tabs/cookiebannertab.yml'
	],
    'snippets' => [
        'cookiebanner' =>  __DIR__ . '/snippets/cookiebanner.php'
    ],
    'translations' => [
        'de' => require __DIR__ . '/translations/de.php',
        'en' => require __DIR__ . '/translations/en.php',
    ],
]);

cookiebanner.php

<script>
    console.log("run here");
    <?php if($site->... == true): ?>
</script>

Thanks, but I also wanted to know where you include the snippet

1 Like

Ah, i think this is the problem, i forgot to insert the snippet in template, thanks very much!