CRM-14949 - CRM_Core_Page_AJAX::returnDynamicJS() => CRM_Core_Smarty::fetchWith()
authorTim Otten <totten@civicrm.org>
Thu, 10 Jul 2014 03:23:52 +0000 (20:23 -0700)
committerTim Otten <totten@civicrm.org>
Thu, 10 Jul 2014 03:23:52 +0000 (20:23 -0700)
The returnDynamicJS() looks like sugar for Smarty's assign()+fetch() --
don't see anything about it that's particular to dynamic-JS.

CRM/Admin/Page/AJAX.php
CRM/Core/Page/AJAX.php
CRM/Core/Resources.php
CRM/Core/Smarty.php

index 68b71b1a31d01295c27d70132a053488f6a7c96f..ebc458f0b31e1a0fcb776c099d13470699383968 100644 (file)
@@ -51,7 +51,7 @@ class CRM_Admin_Page_AJAX {
       header('Content-Type:    application/javascript');
       header("Cache-Control: max-age=$year, public");
 
-      print CRM_Core_Page_AJAX::returnDynamicJS('CRM/common/navigation.js.tpl', array(
+      print CRM_Core_Smarty::singleton()->fetchWith('CRM/common/navigation.js.tpl', array(
         'navigation' => CRM_Core_BAO_Navigation::createNavigation($contactID),
       ));
     }
index b9ea566d03fa8b090b86cdf99de0670177287ce9..593beb5f3cc3a4569db118827e07f68e6f77cfb3 100644 (file)
@@ -199,19 +199,6 @@ class CRM_Core_Page_AJAX {
     CRM_Utils_System::civiExit();
   }
 
-  /**
-   * Render and output a template as a javascript file
-   * @param string $tplFile
-   * @param array $vars - template variables
-   */
-  static function returnDynamicJS($tplFile, $vars = array()) {
-    $smarty = CRM_Core_Smarty::singleton();
-    foreach ($vars as $name => $val) {
-      $smarty->assign($name, $val);
-    }
-    return $smarty->fetch($tplFile);
-  }
-
   /**
    * Send autocomplete results to the client. Input can be a simple or nested array.
    * @param array $results - If nested array, also provide:
index cb6381684a4563ae9c0b5e39dbd036786f4a9747..40b6bd20c252747f07f8e5874d65a5233c585345 100644 (file)
@@ -570,7 +570,7 @@ class CRM_Core_Resources {
       'otherSearch' => json_encode(ts('Enter search term...')),
       'contactCreate' => CRM_Core_BAO_UFGroup::getCreateLinks(),
     );
-    CRM_Utils_File::addDynamicResource($fileName, CRM_Core_Page_AJAX::returnDynamicJS('CRM/common/localization.js.tpl', $vars));
+    CRM_Utils_File::addDynamicResource($fileName, CRM_Core_Smarty::singleton()->fetchWith('CRM/common/localization.js.tpl', $vars));
   }
 
   /**
index 9396326f3c299093270ffacfb00f7bc8e1e0024a..f683c56574a3767edbe1dc3839825e18767ecf2c 100644 (file)
@@ -209,6 +209,27 @@ class CRM_Core_Smarty extends Smarty {
     return $output;
   }
 
+  /**
+   * Fetch a template (while using certain variables)
+   *
+   * @param string $resource_name
+   * @param array $vars (string $name => mixed $value) variables to export to Smarty
+   * @throws Exception
+   * @return bool|mixed|string
+   */
+  function fetchWith($resource_name, $vars) {
+    $this->pushScope($vars);
+    try {
+      $result = $this->fetch($resource_name);
+    } catch (Exception $e) {
+      // simulate try { ... } finally { ... }
+      $this->popScope();
+      throw $e;
+    }
+    $this->popScope();
+    return $result;
+  }
+
   /**
    * @param $name
    * @param $value