Jump to content

How does Prestashop load JS files in header?


Recommended Posts

Hi guys

 

I would like to understand how PS loads the JS files in header.tpl

 

In the code:

 

{if isset($js_files)}
{foreach from=$js_files item=js_uri}
<script type="text/javascript" src="{$js_uri}"></script>
{/foreach}
{/if}

 

What does $js_files stand for? How does PS select the $js_files? There's a bunch of .js files in the root js dir and the theme's js dir, however not all of them are selected

  • Like 1
Link to comment
Share on other sites

  • 3 weeks later...
  • 1 month later...
  • 1 year later...

I know very little about php but...

 

{foreach} is how a loop called in Smarty (php template render engine).

 

So basically its doing a loop to retrieve all available JS files, I think its looking for all available files ending with JS in 'root/js' and in 'themes/current_theme/js' those location defines in 'defines_uri.inc.php'

 

Its loading neccassary JS based on modules transplant in header

Edited by yaniv14 (see edit history)
Link to comment
Share on other sites

If I am not mistaken, like Yaniv - PHP is not my strong suit, its the controller.php for whatever page is being loaded that determines what files are loaded. For instance cms pages use CmsController.php to call css/scripts necessary to render that page in public function setMedia()

 

public function setMedia()
{
parent::setMedia();
if ($this->assignCase == 1)
$this->addJS(_THEME_JS_DIR_.'cms.js');
$this->addJS(_THEME_JS_DIR_.'jquery-1.3.2.js');
$this->addCSS(_THEME_CSS_DIR_.'cms.css');
}

 

If you are looking to add your own css/js this is where you would add it. Maybe someone can add to this to confirm or point you in the right direction

Link to comment
Share on other sites

  • 3 months later...

Hi there!

 

$js_files is defined as global array ( public $js_files = array(); inside of controller.php )

 

That means every js file inside that array will be loaded on header of webpage (with foreach function).

 

Like Whispar1 said, controllers call css/scripts ( addJS or addCSS function) necessary to render that page in public function setMedia().

 

If you want to see what is inside of that array and loaded on any page add {$js_files|@print_r} to tpl.

 

;)

Edited by Nuno Loureiro (see edit history)
Link to comment
Share on other sites

{if isset($js_files)}

{foreach from=$js_files item=js_uri}

<script type="text/javascript" src="{$js_uri}"></script>

{/foreach}

{/if

 

this load the theme js file which is inside the themes->mythemes->js folder. and also those js file which is also inside themes->mythemes->js->modules folder on theme directory and also load the controller addJs method assigned js file.

This is a global smarty variable. Hope you understand.

Edited by smartdatasoft (see edit history)
Link to comment
Share on other sites

×
×
  • Create New...