Forums / Setup & design / ezscript_require external resource?

ezscript_require external resource?

Author Message

Thomas Parisot

Thursday 17 March 2011 10:05:44 am

Hello,

I wanted to load Google Maps JS file as a remote URL like this:

{ezscript_require('http://maps.google.com/maps/api/js?sensor=true')}

The problem it loads the content as JS string inside a script tag.

<script type="text/javascript">http://maps.google.com/maps/api/js?sensor=true</script>

And I expect :

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>

Is there a way to achieve that, without having to code anything? (it would be crazy for this feature only).

Thanks for your concern :-)

Damien Pobel

Thursday 17 March 2011 12:03:54 pm

Hi Thomas,

As far as I can tell, ezscript_* template operators can not handle remote JavaScript files. I guess you call ezscript_require in some special view templates to call Google Maps JavaScript only when it's needed ? In this case it would make sense, I think you should open a enhancement request in the issue tracker.

As a workaround, you can define a persistent variable in your view template and then include remote JavaScript file(s) based on it. It's just a matter of some lines of template... sorry for the without having to code anything ;-)

Cheers

Damien
Planet eZ Publish.fr : http://www.planet-ezpublish.fr
Certification : http://auth.ez.no/certification/verify/372448
Publications about eZ Publish : http://pwet.fr/tags/keywords/weblog/ez_publish

Carlos Revillo

Thursday 17 March 2011 12:22:59 pm

Well, but ezscript_require also load jquery from google if you load it via ezjsc::jquery, right?

so, i think you can add a custom ezjscore server function and from there, output your needed javascript code.

i mean, something like https://github.com/ezsystems/ezjscore/blob/master/classes/ezjscserverfunctionsjs.php#L231

Marko Žmak

Thursday 17 March 2011 1:01:33 pm

Thomas, the fact is that ezscript_* template operators want your parameter to end in ".js". So by doing this little trick:

{ezscript('http://maps.google.com/maps/api/js?sensor=true&amp;ext=.js')}

it should work. (it worked on my site)

A note to the ezjscore author:

Why is it reqired for the parameters to end in ".js"? Could we avoid this requirement?

--
Nothing is impossible. Not if you can imagine it!

Hubert Farnsworth

Damien Pobel

Thursday 17 March 2011 3:43:33 pm

@Carlos : you're totally right, I've totally forgot about that feature. In this case, you'll just have to ignore LoadFromCDN setting though.

@Marko : wow that seems tricky :-) I've just tried but it does not seem to work for me on an eZ Publish 4.4 with the bundled ezjscore nor with the last git version.

Cheers

Damien
Planet eZ Publish.fr : http://www.planet-ezpublish.fr
Certification : http://auth.ez.no/certification/verify/372448
Publications about eZ Publish : http://pwet.fr/tags/keywords/weblog/ez_publish

Guillaume Kulakowski

Friday 18 March 2011 12:03:49 am

I had the same problem for my Google Custom Search Engine usage : http://recherche.fedora-fr.org/

I have used jquery for that :

$("head").prepend($('<link rel="stylesheet" href="http://www.google.com/cse/style/look/default.css" type="text/css" />') );

My blog : http://www.llaumgui.com (not in eZ Publish ;-))
eZC on RHEL : http://blog.famillecollet.com/pages/Config-en
eZC on Fedora : just "yum install php-channel-ezc"

Marko Žmak

Friday 18 March 2011 2:47:19 am

"

@Marko : wow that seems tricky :-) I've just tried but it does not seem to work for me on an eZ Publish 4.4 with the bundled ezjscore nor with the last git version.

"

First I did try it on eZP 4.5, but now I have tried it on 4.4 and it works too. It woks both with LoadFromCDN enabled or disabled.

Are you sure you did the exactly same thing as I did?

And there's an interesting detail...

When I enable the Packer in INI settings, and call:

{ezscript('http://maps.google.com/maps/api/js?sensor=true&amp;ext=.js')}

by itself, the the content of http://maps.google.com/maps/api/js?sensor=true if not included in the packed file nor it's included via the <script> tag in HTML.

But if I put http://maps.google.com/maps/api/js?sensor=true together with all other JS scripts in one ezscript() call, then it is included correctly via the <script> tag:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true&amp;ext=.js" charset="utf-8"></script>

Hm, maybe Andre R. could comment some more on this one...

--
Nothing is impossible. Not if you can imagine it!

Hubert Farnsworth

Marko Žmak

Friday 18 March 2011 2:48:43 am

"

I had the same problem for my Google Custom Search Engine usage : http://recherche.fedora-fr.org/

I have used jquery for that :

$("head").prepend($('<link rel="stylesheet" href="http://www.google.com/cse/style/look/default.css" type="text/css" />') );
"

Why did you use jquery for that?

You could have just put:

<link rel="stylesheet" href="http://www.google.com/cse/style/look/default.css" type="text/css" />

in yout HTML without any procesing by eZP.

--
Nothing is impossible. Not if you can imagine it!

Hubert Farnsworth

Damien Pobel

Friday 18 March 2011 3:21:28 am

@Marko : the packer was enabled when doing my tests. I guess Guillaume uses jQuery to be able to embed the stylesheet depeding on the page. His JavaScript line is probably generated in the full view of some special nodes, the template generates this code to include the stylesheet only when it's needed.

Cheers

Damien
Planet eZ Publish.fr : http://www.planet-ezpublish.fr
Certification : http://auth.ez.no/certification/verify/372448
Publications about eZ Publish : http://pwet.fr/tags/keywords/weblog/ez_publish

Marko Žmak

Friday 18 March 2011 4:05:19 am

"

I guess Guillaume uses jQuery to be able to embed the stylesheet depeding on the page. His JavaScript line is probably generated in the full view of some special nodes, the template generates this code to include the stylesheet only when it's needed.

"

Yes, but think about it, there's still no need for using jQuery...

--
Nothing is impossible. Not if you can imagine it!

Hubert Farnsworth

Thomas Parisot

Friday 18 March 2011 6:11:01 am

Thanks for your replies :-)

Indeed, yes I wanted to load gmap only for pages requiring it; not on all the site webpages.

I load the script asynchroneously so far (within a local JS) but there are many cases where you can call a remote script without an ending .js extension, for JS scripts or JSON-P response (like YQL, Twitter API or Webservices).

I guess the extension should check for an URI scheme, or at least, a "//" (no scheme = same protocol).

Nicolas Pastorino

Friday 18 March 2011 6:52:43 am

Hi,

Should you think what you are blocked by is a bug or would benefit from a request for enhancement on eZJSCore, i encourage you to report it there : http://issues.ez.no/ezpublish (select the right component in the right-column : eZJSCore).

Cheers,

--
Nicolas Pastorino
Director Community - eZ
Member of the Community Project Board

eZ Publish Community on twitter: http://twitter.com/ezcommunity

t : http://twitter.com/jeanvoye
G+ : http://plus.tl/jeanvoye

André R.

Friday 18 March 2011 8:48:19 am

> Why is it reqired for the parameters to end in ".js"? Could we avoid this requirement?

Yes, issue created: http://issues.ez.no/IssueView.php?Id=18167&activeItem=1

Workaround is (as mentioned) to append ".js" using querystring.

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

André R.

Monday 21 March 2011 8:09:50 am

Added patch to issue, feel free to test for your cases it so we can get this closed.

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

Jerome Despatis

Wednesday 23 March 2011 3:08:33 am

Thanks for the patch, it works well for me !

I can now clean all the ugly code...

{* TODO: load this in the <head> section *}
{run-once}
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
{/run-once}

thanks again