77use Symfony \Component \HttpKernel \HttpKernelInterface ;
88use Ajax \service \JString ;
99class JsUtils extends \Ajax \JsUtils{
10+
1011 public function getUrl ($ url ){
1112 //$request = Request::createFromGlobals();
1213 $ router =$ this ->getInjected ();
1314 if (isset ($ router )){
1415 try {
15- $ url =$ router ->generate ($ url );
16+ $ url =$ router ->generate ($ url );
1617 }catch (\Exception $ e ){
1718 return $ router ->getContext ()->getBaseUrl ();
1819 }
1920 }
2021 return $ url ;
2122 }
2223 public function addViewElement ($ identifier ,$ content ,&$ view ){
23- if (\array_key_exists ("q " , $ view )===false ){
24- $ view ["q " ]=array ();
24+ if (\is_array ($ view )){
25+ if (\array_key_exists ("q " , $ view )===false ){
26+ $ view ["q " ]=array ();
27+ }
28+ $ view ["q " ][$ identifier ]=$ content ;
29+ }elseif ($ view instanceof \Twig_Environment){
30+ $ vars =$ view ->getGlobals ();
31+ if (\array_key_exists ("q " , $ vars )===false ){
32+ $ vars ["q " ]=array ();
33+ }
34+ $ vars ["q " ][$ identifier ]=$ content ;
35+ $ view ->addGlobal ("q " ,$ vars ["q " ]);
2536 }
26- $ view ["q " ][$ identifier ]=$ content ;
2737 }
2838
2939 public function createScriptVariable (&$ view ,$ view_var , $ output ){
30- $ view [$ view_var ]=$ output ;
40+ $ this ->addVariable ($ view_var , $ output , $ view );
41+ }
42+
43+ protected function addVariable ($ key ,$ value ,&$ view ){
44+ if (\is_array ($ view )){
45+ $ view [$ key ]=$ value ;
46+ }elseif ($ view instanceof \Twig_Environment){
47+ $ view ->addGlobal ($ key ,$ value );
48+ }
3149 }
3250
3351 /**
@@ -48,15 +66,15 @@ public function forward($initialControllerInstance,$controllerName,$actionName,$
4866 }
4967
5068 public function renderContent ($ initialControllerInstance ,$ viewName , $ params =NULL ) {
51- if ($ initialControllerInstance ->has ('templating ' )) {
52- return $ initialControllerInstance ->get ('templating ' )->render ($ viewName , $ params );
53- }
69+ if ($ initialControllerInstance ->has ('templating ' )) {
70+ return $ initialControllerInstance ->get ('templating ' )->render ($ viewName , $ params );
71+ }
5472
55- if (!$ initialControllerInstance ->has ('twig ' )) {
56- throw new \LogicException ('You can not use the "renderView" method if the Templating Component or the Twig Bundle are not available. ' );
57- }
73+ if (!$ initialControllerInstance ->has ('twig ' )) {
74+ throw new \LogicException ('You can not use the "renderView" method if the Templating Component or the Twig Bundle are not available. ' );
75+ }
5876
59- return $ initialControllerInstance ->get ('twig ' )->render ($ viewName , $ params );
77+ return $ initialControllerInstance ->get ('twig ' )->render ($ viewName , $ params );
6078 }
6179
6280 public function fromDispatcher ($ dispatcher ){
0 commit comments