Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

fTemplate php inside js

posted by darren 8 years ago

Hi,

How can I or whats the best way of rendering php vars inside my external JS using fTemplate?

$this->tpl->add('js', APP_PATH.'/scripts/export.js');

Inside the JS there will a few php vars...

$(document).ready(function() {
	var options = {width: 900, type: 'application/pdf', url: '<?php echo this->get('some_var'); ?>'};

	//more code...
});

Thanks,

Darren

I am by no means an expert ad could be totally wrong here. I see two ways of doing this.

The first is to make your external js dynamic by making it a php page and give it a header.

In Your Template

<script type="text/javascript" src="/path/to/dynamic_js.php"></script>

Source for dynamic_js.php

<?php

// set the header to force it to be js Header("content-type: application/x-javascript"); ?>

$(document).ready(function() { var options = {width: 900, type: 'application/pdf', url: '<?php echo fTemplating::get('some_var'); ?>'}; //more code... });

// end dynamic_js.php

posted by dberry 8 years ago

The other is to set a js var equal to your php and pass it to your js...

<script type="text/javascript">
  some_var = <?php echo fTemplating::get('some_var');
</script>

<script type="text/javascript" src="/path/to/your.js"></script>
posted by dberry 8 years ago

Hi... thanks for the reply...

RE: first reply - Because i add that external JS dynamically based on the page through the flourish template engine $this->tpl->add('js', 'script') i cant have a .php file as it only supports .js extension.

Your second reply is very similar to what i was doing except you moved the var out of the js and placed inline?

However you have given me an idea though... i might be able to use the $this->tpl->set('some_name', 'js file') which may do it...

What i have done at the moment is put my JS string into a php function and add it via the set() method which works fine just that i think its messy...

But thanks and enjoy your weekend.

posted by darren 8 years ago

You can force the output type of fTemplating::place() by passing a second parameter with the type of thing you are placing. This allows for using PHP scripts as CSS or JS. There is some more info at fTemplating#PlacingElements.

$this->tpl->add('js', APP_PATH.'/scripts/export.js.php');
$this->tpl->add('css', APP_PATH.'/scripts/export.css.php');

$this->tpl->place('css', 'css');
$this->tpl->place('js', 'js');
posted by wbond 8 years ago

Hi Will,

Thanks, sorry for the late reply... this worked but didnt allow the use of $this inside those files?

What i did was use -

$this->tpl->add('js', APP_ROOT.'/scripts/export.js.php');
$this->tpl->place('js');

It needed an absolute path rather then APP_PATH being relative... Im not sure if this is your intention between .php files and the others?

Thanks

posted by darren 8 years ago

With the method I described you won't have access to template variables ($this) in the JS because the URL to the PHP page is just placed inside of a script tag and then the browser requests that page separately. This technique will allow for dynamic js, but it will not have access to everything in the current request.

For instance, if you look at the source of a page using the example I provided, you should see:

#!text/html
<script type="text/javascript" src="/path/to/scripts/export.js.php"></script>

If you want to have access to the current request, you'd have to have fTemplating place a PHP file that contains a <script> tag and PHP variables. It seems like this is what you did. In that case you would need the filesystem path to the PHP script, and all of the JS would be directly included in the rendered HTML.

posted by wbond 8 years ago

thanks Will that makes it clear :)

posted by darren 8 years ago