How to insert async and Defer attributes without the value with Cakephp 3?

Asked

Viewed 95 times

2

I’m using the PHP Cakephp Framework 3.0.3 and would like to know how to insert the keywords Defer and async script tags and omit values:

How script insertion is done with the framework:

<?= $this->Html->script('jquery-1.11.1.min.js') ?>

How is rendered:

<script src="/js/jquery-1.11.1.min.js"></script>

As I’d like it to be:

<script async src="/js/jquery-1.11.1.min.js"></script>
<script defer src="/js/jquery-1.11.1.min.js"></script>

In the documentation I saw nothing I could make this insertion.

An alternative way I’m using is to do it this way:

<?= $this->Html->script('jquery-1.11.1.min.js',['defer' => true]) ?>

and this rendering:

<script src="/js/jquery-1.11.1.min.js" defer="defer"></script>
  • Do you want this by default at all? Why do you consider gambiarra ["defer" => true]?

  • @Wallacemaxters why it generates a different result than expected, but I will change the gambiarra

  • I used to use that term a lot :)

  • 1

    In my opinion, you should create a helper and extender Htmlhelper

  • This extending the Htmlhelper class is worth a great answer (if added with an example). @Wallacemaxters

  • @Ricardo on p ['defer'=>true] or [defer] beget defer="defer" seems to be cakephp’s own. Maybe just extending the class Cake\View\Helper\HtmlHelper same. An example that proves to be really the result Htmlhelper::media

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.