Search code examples
opencart-3

In Opencart Twig what is the proper way to add elements to the head tag only on pages that require them?


I am trying to add style and/or script elements in the head tag from header.twig but I don't want them on every page that uses header.twig. Product.twig should get only the styles and scripts which apply to it and other pages likewise. How do I do that in Opencart's Twig?

I thought maybe I could use extends or embed or filter somehow but I don't how to do that in Opencart's version of Twig.


Solution

  • you can use

    $this->document->addScript
    $this->document->addStyle
    

    directly from the controller page to add styles and javascript files to header only on this page

    for example in product.php

    $this->document->addScript('catalog/view/javascript/jquery/magnific/jquery.magnific-popup.min.js');
    
    $this->document->addStyle('catalog/view/javascript/jquery/magnific/magnific-popup.css');
    

    this will add style and js file in the head only on product page