Jump to content

How to create template/field with editable javascript scripts


Recommended Posts

Hi, I decided to put a dynamic background to some pages of my site.
The background is controlled by a js script.
I also decided to create a template and a field to choose from time to time which background to put.
Unfortunately, the processwire condice to dynamically link scripts seems not to work as well as for images and simple links. Where am I going wrong?

DON'T WORK:

<script src="<?=$page->texturl_background?>"></script>

DON'T WORK:

<script src="<?php echo $config->urls->templates?><?=$page->texturl_background?>"></script>


Instead, it works well:
 

<link rel="icon" href="<?php echo $config->urls->templates?><?=$page->texturl_favicon?>" type="image/png">
Link to comment
Share on other sites

Please open the devtools in your browser and open the network tab. Then select JavaScript and see if it fails to load a resource.
Did you enter the correct scriptname (including the .js extension) in the inputfield?

Link to comment
Share on other sites

Hello everyone, I have discovered the problem.
x DOTNETIC: The address is correct. Checked and rechecked.
x GIDEON: the field type is plain text. By putting a field-url, the linking doesn't work.

Here is the solution, after banging my head for a while.

This code:

<script src="<?php echo $config->urls->templates?><?=$page->texturl_bgvegas?>"></script>

This code is correct and working, but it works ONLY if I put it DIRECTLY in the php-html page of the template.
Instead, I had written it and LINKED it in a file retrieved from the template page through the following code:

<?php wireIncludeFile('styles/background-and-other.php'); ?>

or

<?php include('styles/background-and-other.php'); ?>

 So what wasn't working was not the php-processwire code, but the link codes (php include or php wireIncludeFile) to external files, which from the template html page don't work for all file types. The <script> js processwire include/wireIncludeFile doesn't see them, for the other <title> or <link> tags, however, there doesn't seem to be a problem.

I hope that in the future processwire will improve or alert us to any dysfunctionality.
I was going crazy ???

 

Link to comment
Share on other sites

  • 2 weeks later...

Hi,

don't know if it could solve your problem (even if already solved ? ) but i once ran into the same kind of issue with external dynamic css file and soled it this way

in my config.php file

$config->contentTypes = array_merge($config->contentTypes,
    array(
        'css' => 'text/css'
    )
);

this adds the css file type in your template "files" tab / content-type select

then, on top of my bibicss.php file

<?php namespace ProcessWire;
header("Content-type: text/css; charset: UTF-8");
header("Charset:utf-8");


and i linked to my bibicss page the usual way in the head of the pages that need it and it worked fine

i think that would be the same for js files with a simple 'js' => 'text/javascript' and the correct headers Content-type in the template

in case it helps

have a nice day

Link to comment
Share on other sites

FORMATTED VERSION:

in my config.php file

$config->contentTypes = array_merge($config->contentTypes,
 array(
  'css' => 'text/css'
 )
);

this adds the css file type in your template "files" tab / content-type select then, on top of my bibicss.php file

<?php namespace ProcessWire;
header("Content-type: text/css; charset: UTF-8");
header("Charset:utf-8");

and i linked to my bibicss page the usual way in the head of the pages that need it and it worked fine i think that would be the same for js files with a simple 'js' => 'text/javascript' and the correct headers Content-type in the template in case it helps have a nice day

  • 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

×
×
  • Create New...