1. Home
  2. Docs
  3. Simpods MVC
  4. Tutorials
  5. JavaScript
  6. Specific JS For A Template

Specific JS For A Template

Auto-enqueue

Simpods MVC allows for the ability to automatically enqueue scripts for your pages without you having to enqueue them yourself. In order to utilise this functionality, your scripts need to meet the following criteria;

Frontend:

  • The file needs to be named the same as your template
    • e.g. “front-page.php” would have a script called “front-page.js”
  • The file is located in the same place relative to the theme within the “mvc/js” folder
    • e.g. “template/template-about.php” would have a script located in “mvc/js/template/template-about.js”

Backend:

  • The file needs to be named the same as the current page.
    • The rule is that if it is a post type, it will be post_type.php; if not and the variable “page” exists in the url, it will be $_GET[‘page’].php; else, it will just use the request script name, e.g. script.php
    • e.g. “script.php” would have a script called “script.js”
  • The file is located in the same place relative to the theme within the “mvc/admin/js” folder
    • e.g. “page” post type would have a script located in “mvc/admin/js/page.js”

If your script follows the above criteria, your script should be automatically enqueued into the page. The handle will be the file name with the ending min.js or .js replaced with -js.

Manual enqueue

If the page script depends on any libraries these will need to be enqueued manually using the controller’s include() method.

This methods accepts an associative array where the keys are script handles and values are file paths relative to the theme. Note that a -js suffix will be appended to the handle if it is not already present.

Example

In this example the page script depends on Shuffle which has been saved under the library folder structure. The following code should be placed within the page controller’s enqueue_fn()


Copy to Clipboard