Javascript load depending on presenter

Notice: This thread is very old.
coyot_
Member | 12
+
0
-

Hello,

is there any way to load javascript depending on presenter?

Example:

I have gallery.js file and I want to load it only when current presenter is “Gallery” (/gallery).

Thanks in advance.

David

Last edited by coyot_ (2013-11-11 10:35)

Casper
Member | 253
+
0
-

The easiest way should be:

{if $presenter->isLinkCurrent("Gallery:*")}
<script type="text/javascript" src="{$basePath}/assets/js/gallery.js"></script>
{/if}
coyot_
Member | 12
+
0
-

I feel dumb. :-) Thank you.

Still, is there any more elegant solution? Previously, I worked with Zend and there was possibility to write it to config file something like this:

scripts.gallery = "js/gallery.js"

But if there is nothing like it, I'm perfectly ok with the suggested way.

Casper
Member | 253
+
0
-

There is no default implementation of the behaviour you need in Nette. You can check an addon called WebLoader, which can do many things with scripts and css. But I'm not using it.

coyot_
Member | 12
+
0
-

I'll stick to the easiest way.

Thank you.

Marek Šneberger
Member | 130
+
0
-

I'm using in @layout.latte block ‘scripts’ for load default scripts:

@layout.latte:

{#scripts}
<script src="{$basePath}/js/jquery.min.js"></script>
and more...
{/#}

And for view that need default and extended scripts (eg. Gallery/default)

{#scripts}
{#include parent} //load default scrips
<script type="text/javascript" src="{$basePath}/assets/js/gallery.js"></script>
{/#}
coyot_
Member | 12
+
0
-

Marek: This is quite elegant solution. Thank you very much! :-)

Edit: I found out, you propably have syntax error in your example. It seems, that you can't use e.g.{#scripts}, but {block #scripts}, etc.

My solution (inspired by yours):

@layout.latte

{block #scripts}
    <script src="{$basePath}/js/jquery.min.js"></script>
    ...
{/block}

And for view that need default and extended scripts (eg. Gallery/default)

{block #scripts}
{include #parent}
    <script type="text/javascript" src="{$basePath}/assets/js/gallery.js"></script>
{/block}

Last edited by coyot_ (2013-11-14 13:05)