In Drupal 7, I had js-output template.
I define the template output as javascript. This is used for our front end person that can easily input javascript without touching backend.
<script> <?php if(isset($content['field_custom_javascript'])) print $content['field_custom_javascript']['#items'][0]['value']; ?> </script>
In Drupal 8, I am trying to do the same thing by define twig file:
{% if content.field_custom_javascript is not empty %} <script> {{ content.field_custom_javascript }} </script> {% endif %}
But when I put js in the customize_js_output field, as the following:
(function ($, Drupal) { $( document ).ready(function() { console.log( "ready!" ); }); })(jQuery, Drupal);
Or
$( document ).ready(function() { console.log( "ready!" ); });
Have jQuery is not defined or $ is not defined error. Got all of jQuery libraries load correct in the footer.
My question: Is this a good way to do it? If not, do you have any suggestions?
If i only want to write js inside my body field in the source or even php filter as content editor, can I do it?