1818
1919use FloatPHP \Classes \{
2020 Filesystem \Stringify , Filesystem \Arrayify , Filesystem \Json ,
21+ Filesystem \Exception as ErrorHandler ,
2122 Http \Session ,
2223 Html \Template
2324};
2425use FloatPHP \Helpers \Framework \Permission ;
26+ use \Exception ;
2527
2628class View extends Base
2729{
@@ -70,18 +72,18 @@ protected function render($content = [], $tpl = 'system/default')
7072 echo $ this ->assign (Arrayify::merge ($ this ->content , $ content ), $ tpl );
7173 }
7274
73- /**
74- * Aassign content to view.
75- *
75+ /**
76+ * Aassign content to view.
77+ *
7678 * @access protected
7779 * @param array $content
7880 * @param string $tpl
79- * @return string
81+ * @return mixed
8082 */
8183 protected function assign ($ content = [], $ tpl = 'system/default ' )
8284 {
8385 // Set View environment
84- $ path = $ this ->applyFilter ('view-path ' ,$ this ->getViewPath ());
86+ $ path = $ this ->applyFilter ('view-path ' , $ this ->getViewPath ());
8587 $ env = Template::getEnvironment ($ path ,[
8688 'cache ' => "{$ this ->getCachePath ()}/view " ,
8789 'debug ' => $ this ->isDebug ()
@@ -95,96 +97,102 @@ protected function assign($content = [], $tpl = 'system/default')
9597 }
9698
9799 // Add view global functions
98- $ env ->addFunction (Template::extend ('dump ' , function ($ var ){
100+ $ env ->addFunction (Template::extend ('dump ' , function ($ var ) {
99101 var_dump ($ var );
100102 }));
101- $ env ->addFunction (Template::extend ('die ' , function ($ var = null ){
103+ $ env ->addFunction (Template::extend ('die ' , function ($ var = null ) {
102104 die ($ var );
103105 }));
104- $ env ->addFunction (Template::extend ('exit ' , function ($ status = null ){
106+ $ env ->addFunction (Template::extend ('exit ' , function ($ status = null ) {
105107 exit ($ status );
106108 }));
107- $ env ->addFunction (Template::extend ('getSession ' , function ($ var = null ){
109+ $ env ->addFunction (Template::extend ('getSession ' , function ($ var = null ) {
108110 return Session::get ($ var );
109111 }));
110- $ env ->addFunction (Template::extend ('hasCapability ' , function ($ capability = null , $ userId = null ){
111- return Permission::hasCapability ($ capability ,$ userId );
112+ $ env ->addFunction (Template::extend ('hasCapability ' , function ($ capability = null , $ userId = null ) {
113+ return Permission::hasCapability ($ capability , $ userId );
112114 }));
113- $ env ->addFunction (Template::extend ('hasRole ' , function ($ role = null , $ userId = null ){
114- return Permission::hasRole ($ role ,$ userId );
115+ $ env ->addFunction (Template::extend ('hasRole ' , function ($ role = null , $ userId = null ) {
116+ return Permission::hasRole ($ role , $ userId );
115117 }));
116- $ env ->addFunction (Template::extend ('getLanguage ' , function (){
118+ $ env ->addFunction (Template::extend ('getLanguage ' , function () {
117119 return $ this ->getLanguage ();
118120 }));
119- $ env ->addFunction (Template::extend ('isLoggedIn ' , function (){
121+ $ env ->addFunction (Template::extend ('isLoggedIn ' , function () {
120122 return $ this ->isLoggedIn ();
121123 }));
122- $ env ->addFunction (Template::extend ('isDebug ' , function (){
124+ $ env ->addFunction (Template::extend ('isDebug ' , function () {
123125 return $ this ->isDebug ();
124126 }));
125- $ env ->addFunction (Template::extend ('getConfig ' , function ($ config = '' ){
127+ $ env ->addFunction (Template::extend ('getConfig ' , function ($ config = '' ) {
126128 return $ this ->getConfig ($ config );
127129 }));
128- $ env ->addFunction (Template::extend ('getRoot ' , function (){
130+ $ env ->addFunction (Template::extend ('getRoot ' , function () {
129131 return $ this ->getRoot ();
130132 }));
131- $ env ->addFunction (Template::extend ('getBaseRoute ' , function (){
133+ $ env ->addFunction (Template::extend ('getBaseRoute ' , function () {
132134 return $ this ->getBaseRoute (false );
133135 }));
134- $ env ->addFunction (Template::extend ('getBaseUrl ' , function (){
136+ $ env ->addFunction (Template::extend ('getBaseUrl ' , function () {
135137 return $ this ->getBaseUrl ();
136138 }));
137- $ env ->addFunction (Template::extend ('getAssetUrl ' , function (){
139+ $ env ->addFunction (Template::extend ('getAssetUrl ' , function () {
138140 return $ this ->getAssetUrl ();
139141 }));
140- $ env ->addFunction (Template::extend ('getFrontUploadUrl ' , function (){
142+ $ env ->addFunction (Template::extend ('getFrontUploadUrl ' , function () {
141143 return $ this ->getFrontUploadUrl ();
142144 }));
143- $ env ->addFunction (Template::extend ('getLoginUrl ' , function (){
145+ $ env ->addFunction (Template::extend ('getLoginUrl ' , function () {
144146 return $ this ->getLoginUrl ();
145147 }));
146- $ env ->addFunction (Template::extend ('getAdminUrl ' , function (){
148+ $ env ->addFunction (Template::extend ('getAdminUrl ' , function () {
147149 return $ this ->getAdminUrl ();
148150 }));
149- $ env ->addFunction (Template::extend ('getVerifyUrl ' , function (){
151+ $ env ->addFunction (Template::extend ('getVerifyUrl ' , function () {
150152 return $ this ->getVerifyUrl ();
151153 }));
152- $ env ->addFunction (Template::extend ('getToken ' , function ($ action = '' ){
154+ $ env ->addFunction (Template::extend ('getToken ' , function ($ action = '' ) {
153155 return $ this ->getToken ($ action );
154156 }));
155- $ env ->addFunction (Template::extend ('decodeJSON ' , function ($ json = '' ){
157+ $ env ->addFunction (Template::extend ('decodeJSON ' , function ($ json = '' ) {
156158 return Json::decode ($ json );
157159 }));
158- $ env ->addFunction (Template::extend ('encodeJSON ' , function ($ array = []){
160+ $ env ->addFunction (Template::extend ('encodeJSON ' , function ($ array = []) {
159161 return Json::encode ($ array );
160162 }));
161- $ env ->addFunction (Template::extend ('serialize ' , function ($ data = []){
163+ $ env ->addFunction (Template::extend ('serialize ' , function ($ data = []) {
162164 return Stringify::serialize ($ data );
163165 }));
164- $ env ->addFunction (Template::extend ('unserialize ' , function ($ string = '' ){
166+ $ env ->addFunction (Template::extend ('unserialize ' , function ($ string = '' ) {
165167 return Stringify::unserialize ($ string );
166168 }));
167- $ env ->addFunction (Template::extend ('doAction ' , function ($ hook = '' , $ args = []){
168- $ this ->doAction ($ hook ,$ args );
169+ $ env ->addFunction (Template::extend ('doAction ' , function ($ hook = '' , $ args = []) {
170+ $ this ->doAction ($ hook , $ args );
169171 }));
170- $ env ->addFunction (Template::extend ('hasAction ' , function ($ hook = '' , $ args = []){
171- $ this ->hasAction ($ hook ,$ args );
172+ $ env ->addFunction (Template::extend ('hasAction ' , function ($ hook = '' , $ args = []) {
173+ $ this ->hasAction ($ hook , $ args );
172174 }));
173- $ env ->addFunction (Template::extend ('applyFilter ' , function ($ hook = '' , $ method = '' ){
174- return $ this ->applyFilter ($ hook, $ method );
175+ $ env ->addFunction (Template::extend ('applyFilter ' , function ($ hook = '' , $ method = '' ) {
176+ return $ this ->applyFilter ($ hook , $ method );
175177 }));
176- $ env ->addFunction (Template::extend ('hasFilter ' , function ($ hook = '' , $ method = '' ){
177- return $ this ->hasFilter ($ hook ,$ method );
178+ $ env ->addFunction (Template::extend ('hasFilter ' , function ($ hook = '' , $ method = '' ) {
179+ return $ this ->hasFilter ($ hook , $ method );
178180 }));
179- $ env ->addFunction (Template::extend ('doShortcode ' , function ($ shortcode = '' , $ ignoreHTML = false ){
180- return $ this ->doShortcode ($ shortcode ,$ ignoreHTML );
181+ $ env ->addFunction (Template::extend ('doShortcode ' , function ($ shortcode = '' , $ ignoreHTML = false ) {
182+ return $ this ->doShortcode ($ shortcode , $ ignoreHTML );
181183 }));
182- $ env ->addFunction (Template::extend ('translate ' , function ($ string = '' ){
184+ $ env ->addFunction (Template::extend ('translate ' , function ($ string = '' ) {
183185 return $ this ->translate ($ string );
184186 }));
185187
186188 // Return rendered view
187- $ view = $ env ->load ("{$ tpl }{$ this ->getViewExtension ()}" );
188- return $ view ->render (Arrayify::merge ($ this ->content ,$ content ));
189+ try {
190+ $ view = $ env ->load ("{$ tpl }{$ this ->getViewExtension ()}" );
191+ return $ view ->render (Arrayify::merge ($ this ->content , $ content ));
192+ } catch (Exception $ e ) {
193+ ErrorHandler::clearLastError ();
194+ }
195+
196+ return false ;
189197 }
190198}
0 commit comments