We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to add inline Js or Css from my action controller, to my view?

i try:

public function showCaptchaAction(){
$this->assets->addInlineJs('alert("hola");');
//or
$this->assets->addInlineJs('<script> var scriptPaypal; var onloadCallback = function() {scriptPaypal = grecaptcha.render(document.getElementById("recaptchaG"), {"sitekey" :"' . $this->_public_key . '"}); };$(document).ready(function() { $("#paypal").on( "click", "#regenerateCaptcha", function() { grecaptcha.reset(scriptPaypal); });  });</script>);
}

in my volt template:

    {{ assets.outputJs() }}

but this not works!, what is the valid code?



43.9k

Hi,

what does {{ assets.outputJs() }} outputs ?

As a workaround, I would try to put the javascript code in a partial view :


// views/shared/captcha.phtml

    // html code can use the 'big' parameter
<script> 
  var scriptPaypal; 
  var onloadCallback = function() {
    scriptPaypal = grecaptcha.render(document.getElementById("recaptchaG"), {"sitekey" :"' . $public_key . '"});
  };
  $(document).ready(function() { 
      $("#paypal").on( "click", "#regenerateCaptcha", function() { 
        grecaptcha.reset(scriptPaypal); 
      });
  });
</script>

// elsewhere in views/MyController/MyAction.phtml

<?php $this->partial("shared/captcha", array('public_key' =>$public_key, 'size' => 'big')) ?>

// in MyController.php MyAction

$this->view->setVar('public_key' => $this->_public_key);

Thanks for your answers... but in this case, the function:

$this->assets->addInlineJs('alert("hola");');

how can i use?



43.9k

Last function in https://docs.phalcon.io/en/latest/api/Phalcon_Assets_Manager.html

public outputInlineJs ([unknown $collectionName])

Prints the HTML for inline JS

Thanks for your answer!: i try:

 public function showCaptchaAction(){
    $o_captcha = new \Recaptcha();
    $this->assets->addInlineJs('alert("hola");');
    $this->view->captchaHtml = $o_captcha->getHtmlCode();
}

and in my volt template:

{{ assets.outputInlineJs() }}
but not print the alert('hola');
(

So what now? :I, help!

if i have: my template volt : general.volt ( this is the main view) if i put : {{ assets.outputInlineJs() }} show the alert...

but if.. showCaptcha.volt (this is the action view and Inherited from general.volt) if i put : {{ assets.outputInlineJs() }} not show the alert...

I want to put the {{ assets.outputInlineJs() }} in my main view becouse: general.volt have:

{% set cacheName = constant("SITE_DOMAIN")~"_"~config.application.langDefault~"_"~lang_before_cache~"_"~router.getControllerName() %}
{% cache cacheName~"_"~headerContent 18800 %}
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="description" content="{{ config.client.description }}">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="author" content="MCETS Team">
        <title>{{ config.client.siteName }} - COMMON PAGE </title>
        {{ assets.outputCss('cssCommon') }}
        {{ assets.outputCss() }}
        {% endcache %}
        {% cache cacheName~"_"~router.getActionName()~"_"~dynamicContent 18800 %}
    </head>
    <body>
        {{ this.getContent() }}
        {% endcache %}
        {% cache cacheName~"_"~footerContent 18800 %}
        {{ assets.outputJs('jsCommon') }}
        {{ assets.outputInlineJs() }} // i want to put here the inline Js from my actions views..
    </body>
</html>
{% endcache %}

i want to put here the inline Js from my actions views.. Any help please!!....

edited Jun '18

You can try this:

In the controller

$code_js = 'alert("hola");';

$this->assets->collection('jsVar')->addInlineJs('$(document).ready(function() {' . $code_js . '});');

and in the view

{{ assets.outputInlineJs ('jsVar') }}

Hope this help!