Pregunta

I'm making a project with laravel, in which I pass three variables in my blade view file,

$state_0
$state_1
$state_2

and I would like to make a progress bar using twiters' bootstrap css framework Bootstrap. So to set the width of each progress bar, i would have to calculate something like this

//calc percentage of a state
$percentage= 100*$state_0/($state_1+$state_2+$state_3)

However, making calculations and variables in a blade file is not very professional. Am I forced to set up these vars in the controller, or is there a smarter way to do this?

Cheers

¿Fue útil?

Solución 2

Calculate them in the controller and pass the result to be used in the view:

    //controller method
    $data = array(
        'percentage' => 100*$state_0/($state_1+$state_2+$state_3),
        'foo' => $foo,
        'bar' => $bar,
    );
    $view = View::make('myview', $data);

In your view you can access $percentage, $foo and $bar variables.

Otros consejos

Controller will be good place, especially if you need to use this calculation in the one place of your app. Keep in mind that helpers libraries are loaded by framework every time, even they are not used.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top