7
7
use Symfony \Component \HttpKernel \HttpKernelInterface ;
8
8
use Ajax \service \JString ;
9
9
class JsUtils extends \Ajax \JsUtils{
10
+
10
11
public function getUrl ($ url ){
11
12
//$request = Request::createFromGlobals();
12
13
$ router =$ this ->getInjected ();
13
14
if (isset ($ router )){
14
15
try {
15
- $ url =$ router ->generate ($ url );
16
+ $ url =$ router ->generate ($ url );
16
17
}catch (\Exception $ e ){
17
18
return $ router ->getContext ()->getBaseUrl ();
18
19
}
19
20
}
20
21
return $ url ;
21
22
}
22
23
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 " ]);
25
36
}
26
- $ view ["q " ][$ identifier ]=$ content ;
27
37
}
28
38
29
39
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
+ }
31
49
}
32
50
33
51
/**
@@ -48,15 +66,15 @@ public function forward($initialControllerInstance,$controllerName,$actionName,$
48
66
}
49
67
50
68
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
+ }
54
72
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
+ }
58
76
59
- return $ initialControllerInstance ->get ('twig ' )->render ($ viewName , $ params );
77
+ return $ initialControllerInstance ->get ('twig ' )->render ($ viewName , $ params );
60
78
}
61
79
62
80
public function fromDispatcher ($ dispatcher ){
0 commit comments