Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

How to pass a variable from action to my main or layout template?

I want to pass a variable, or assets from my action to my main template:

my controller:

<?php

namespace Frontend\Controllers;

use Frontend\Controllers\KernelBase;
use Common\Constants\BtsConstants;

class GeneralController extends KernelBase implements BtsConstants {
    public function initialize() {
        parent::initialize();
    $this->view->setMainView('common');
    $this->assets
                    ->collection('jsCommon')
                    ->setTargetPath('js/unifymin/unify_frontend_kernel.min.js')
                    ->setTargetUri('js/unifymin/unify_frontend_kernel.min.js')
                    ->addJs('js/vendor/jquery/jquery-2.1.3.min.js')
                    ->addJs('js/vendor/bootstrap/3.3.4/bootstrap.min.js')
                    ->addFilter(new \Phalcon\Assets\Filters\Jsmin())
                    ->join(true);
    }   
    public function showCaptchaAction(){      
        $o_captcha = new \Recaptcha();        
        $this->view->captchaHtml = $o_captcha->getHtmlCode();
        $this->assets->addJs('js/vendor/bootstrap/3.3.4/bootstrap.min.js');
    }
    public function verifyCaptchaAction(){

    }

}

i have common.volt:

<!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() }}        
    </head>
    <body>
        {{ this.getContent() }}     
        {{ assets.outputJs('jsCommon') }}
        {{ assets.outputJs() }}
    </body>
</html>

I want to put my js, css that i added on my controller's action to my main view, how i can do that?

becouse i have the problem if i put the asset on my action , and the

{{ assets.outputJs() }}

on the action view, this cause problem with jquery becouse jquery frameworks is not load yet, an for purpose of performance the js files must be on the bottom of the page.

Help!



29.7k
Accepted
answer

1/ you are loading bootstrap.min.js twice: in jsCommon collection and in function showCaptchaAction()

2/ in function showCaptchaAction(), you can add js files to jsCommon collection:

public function showCaptchaAction(){

$this->assets
        ->collection('jsCommon')
        ->addJs('js/tinymce/tinymce.min.js')
        ->addJs('js/ajaxupload.js')
        ;

}

in function showCaptchaAction(): i want to add (append) this js files :

->addJs('js/tinymce/tinymce.min.js')
    ->addJs('js/ajaxupload.js')

to the main template:

$this->assets
                ->collection('jsCommon')
                ->setTargetPath('js/unifymin/unify_frontend_kernel.min.js')
                ->setTargetUri('js/unifymin/unify_frontend_kernel.min.js')
                ->addJs('js/vendor/jquery/jquery-2.1.3.min.js')
                ->addJs('js/vendor/bootstrap/3.3.4/bootstrap.min.js')
                ->addFilter(new \Phalcon\Assets\Filters\Jsmin())
                ->join(true);
}

but not works !

ohh, sorry!, my bad!, its works!..

one more question!, i want to add addInlineJs too, but this not work!... is the same case!..

in my in initialize:

    $this->assets
            ->collection('jsCommon')
            ->setTargetPath('js/unifymin/unify_frontend_kernel.min.js')
            ->setTargetUri('js/unifymin/unify_frontend_kernel.min.js')
            ->addJs('js/vendor/jquery/jquery-2.1.3.min.js')
            ->addJs('js/vendor/bootstrap/3.3.4/bootstrap.min.js')
            ->addFilter(new \Phalcon\Assets\Filters\Jsmin())
            ->join(true);

in my action:

 public function showCaptchaAction() {
   $this->assets->collection('jsCommon')
   ->addJs('https://www.gstatic.com/recaptcha/api2/r20150514154044/recaptcha__en.js',FALSE) // this works!
   ->addInlineJs('alert("Hola Mundo");'); // this not works!

in my volt main template:

    <!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.outputInlineCss() }}
        </head>
        <body>
            {{ this.getContent() }}    
            {{ assets.outputJs('jsCommon') }} // this works fine!
            {{ assets.outputInlineJs() }} // this not works dont do the alert!
        </body>
    </html>

help!



29.7k

I never use inlineJs.

But I think it should be something like:

// controller

 public function showCaptchaAction() {
   $this->assets->collection('jsCommon')
   ->addJs('https://www.gstatic.com/recaptcha/api2/r20150514154044/recaptcha__en.js',FALSE); // this works!
    $this->assets->addInlineJs('alert("hola");'); 

// main view

        <body>
            {{ this.getContent() }}    
            {{ assets.outputJs('jsCommon') }}
            {{ assets.outputInlineJs() }} 
        </body>

dont work :(!



29.7k

OK, I did a test on one of my app:

// in controller

    /**
     * Displayes the creation form
     */
    public function newAction()
    {
        $this->assets
            ->collection('header')
            ->addJs('js/tinymce/tinymce.min.js')
            ->addJs('js/ajaxupload.js')
            ;
       $this->assets->addInlineJs('alert("hola");'); 
    }

// in main layout:
    {{ assets.outputJs('header') }}
    </head>
    <body>
        {{ content() }}

        {{ assets.outputJs('footer') }}
        {{ assets.outputInlineJs() }} 
    </body>

And it's working. Don't you have any error message ?

my app is a multimodule project,

in frontend module i have:

views/ common.volt general/ showcaptcha.volt

so my main view is common.volt and i define:

<!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.outputInlineCss() }}
    </head>
    <body>
        {{ this.getContent() }}    
        {{ assets.outputJs('jsCommon') }}
        {{ assets.outputInlineJs() }}
    </body>
</html>

and in my showcaptcha.volt: only:

<form id="frmDemo" action="/public/general/verifyCaptcha.html" method="post">
{{captchaHtml}}
  <input type="submit" name="enviar" value="Enviar">

</form>

in my controller:

<?php

namespace Frontend\Controllers;

use Frontend\Controllers\KernelBase;
use Common\Constants\BtsConstants;

class GeneralController extends KernelBase implements BtsConstants {
    public function initialize() {
    parent::initialize();
     $this->assets
            ->collection('jsCommon');
}
 public function showCaptchaAction() {
    $o_captcha = new \Recaptcha();
    $this->assets->collection('jsCommon')->addJs('file.js',FALSE)->addInlineJs('alert("hola");');
    $this->view->captchaHtml = $o_captcha->getHtmlCode('recaptcha', 'regenerateRecaptcha');
}

but this controller extends from KernelBase:

    public function initialize() {
    parent::initialize();
        $this->view->setMainView('common');  // i defined my main volt template
            $this->assets
                    ->collection('cssCommon')
                    ->setTargetPath('css/unifymin/unify_frontend_kernel.min.css')
                    ->setTargetUri('css/unifymin/unify_frontend_kernel.min.css')
                    ->addCss('css/vendor/bootstrap/3.3.4/bootstrap.min.css')
                    ->addCss('css/common/common.min.css')
                    ->addFilter(new \Phalcon\Assets\Filters\Cssmin())
                    ->join(true);
            $this->assets
                    ->collection('jsCommon')
                    ->setTargetPath('js/unifymin/unify_frontend_kernel.min.js')
                    ->setTargetUri('js/unifymin/unify_frontend_kernel.min.js')
                    ->addJs('js/vendor/jquery/jquery-2.1.3.min.js')
                    ->addJs('js/vendor/bootstrap/3.3.4/bootstrap.min.js')
                    ->addFilter(new \Phalcon\Assets\Filters\Jsmin())
                    ->join(true);

    }

so, i dont have a layout for controller, is directly my main view and my action view...

and you?

Sorry!!!!! my bad!!!!!!!, its works!!!

     public function showCaptchaAction() {
    $o_captcha = new \Recaptcha();
    $this->assets->collection('jsCommon')->addJs($o_captcha->getScriptLoadCode(),FALSE);
    $this->assets->addInlineJs('alert("hola");');
    $this->view->captchaHtml = $o_captcha->getHtmlCode('recaptcha', 'regenerateRecaptcha');
}

my problem was:

 public function showCaptchaAction() {
$o_captcha = new \Recaptcha();
$this->assets->collection('jsCommon')->addJs('file.js',FALSE)->addInlineJs('alert("hola");'); // into collection jsCommon!
$this->view->captchaHtml = $o_captcha->getHtmlCode('recaptcha', 'regenerateRecaptcha');
}

Thanks a lot!



29.7k

I think that

$this->assets->collection('jsCommon')->addJs('file.js',FALSE)->addInlineJs('alert("hola");');

should be:

$this->assets->collection('jsCommon')->addJs('file.js',FALSE);

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

also, I'm not sure that you have to call $this->assets->collection('jsCommon'); in GeneralController initialize method as it is declared in the parent class.

you are right!..i dont need:

    public function initialize() {
    parent::initialize();
     $this->assets
            ->collection('jsCommon');
}

in class GeneralController extends KernelBase implements BtsConstants {}

thanks!!