Hello everybody,

I am really in need of some pointers: I am creating modules and want the output themed by smarty.

Here is my code so far:

Function to take over frontpage:

function frontpage_menu($may_cache) {
  if ($may_cache) {
    $items = array();
    $items[] = array(
      'path' => 'frontpage',
      'title' => t(''),
      'callback' => 'yoyo',
      'access' => TRUE,
      'type' => MENU_SUGGESTED_ITEM);
    return $items;
  }
}

So far, so good, this works as expected, now I want to load my new frontpage-template (frontpage.tpl) and have those {smartyplaceholders} replaced with the actual content.

Failure #1:

function yoyo() {
	$smarty->assign('feature', "TESTTEST");
	$output = _smarty_default("frontpage", $vars,'');
	
	print $output;
}

Failure #2:

function yoyo() {
	$smarty->assign('feature', "TESTTEST");
	$output = $smarty->fetch(drupal_dir ."/". path_to_theme() . "/frontpage.tpl");
	$output = smarty_page($output);
	
	print $output;
}

Neither of those two functions replace the assigned variable, which drives my crazy.

I would really appreciate it if someone would help out and explain to me what the easiest & most efficient way to create modules which use smarty is.

Thanks alot.

Comments

Speigei’s picture

ahem, I have since realized that the above code is crap, here is my newest revision, which kind of works, here's the problem:

The variable "name" that is smarty-assigend in do_it() is only available in "frontpage.tpl", and the vars that are supplied to "_smarty_callback()" are only available in the "altpage.tpl"-template.

However, I need to have access to the "$vars" from the "frontpage.tpl" template.

Basically, all I want is an alternative "page.tpl"-template which I can call in a similar manner to theme('page'....).

Can somebody help? please? thanks!


function do_it() {
	require_once('themes/engines/smarty/smartytemplate.php');
	$smarty = new SmartyTemplate();
	
	$smarty->assign("name","Joe joe");
	$page_content = $smarty->fetch(drupal_dir ."/". path_to_theme() . "/frontpage.tpl");	
	print theme("altpage", $page_content);
}

function smarty_altpage($content) {
	$vars['directory'] = path_to_theme();
	$vars['content'] = $content;
	
	return _smarty_callback('altpage', $vars);
}


function epad_test_menu($may_cache) {
  if ($may_cache) {
    $items = array();
    $items[] = array(
      'path' => variable_get('site_frontpage', 'node'),
      'title' => t('Testing 123'),
      'callback' => 'do_it',
      'access' => TRUE,
      'type' => MENU_CALLBACK);
    return $items;
  }
}

tclineks’s picture

Since front_page.module doesn't publish through a themeable function this is going to be tricky.

front_page.module isn't really suited for such.

If you're in need of more general examples say so.

See the attached patch to front_page.module to see how to introduce themable output.

You could then define a smarty_front_page_text(_yes) to control output.

This isn't really recommended as it essentially breaks the module by hijacking it.

--- front_page.module.orig  2006-02-21 09:20:36.000000000 -0600
+++ front_page.module   2006-02-21 09:20:32.000000000 -0600
@@ -114,7 +114,7 @@

   global $user;
   if (!$user->uid || variable_get("front_page2_size", "drupal") == "same"){
-    $output = variable_get("front_page_text", "drupal");
+    $output = theme("front_page_text");

     if (variable_get('front_page_php', 0)) {
       $output = drupal_eval($output);
@@ -135,7 +135,7 @@
     }
   }
   else {
-    $output = variable_get("front_page_text_yes", "drupal");
+    $output = theme("front_page_text_yes");

     // If PHP content is allowed, execute it.
     if (variable_get('front_page_php_yes',0)) {
@@ -160,4 +160,21 @@
   drupal_goto($path = $output, $query = NULL, $fragment = NULL);

 }
-?>
\ No newline at end of file
+
+function theme_front_page_text() {
+  return _front_get_text();
+}
+
+function theme_front_page_text_yes() {
+  return _front_get_text_yes();
+}
+
+function _front_get_text() {
+  return variable_get("front_page_text", "drupal");
+}
+
+function _front_get_text_yes() {
+  return variable_get("front_page_text_yes", "drupal");
+}
+
+?>
tclineks’s picture

Just read through my reply there -- very robot-esque =p

Speigei’s picture

Thanks for the effort, Travis.

I am currently not using the front_page.module although I might just try it with the supplied patch and see how it works out.

Nonetheless, I would really appreciate a more general example as I need this functuality for pages other than the frontpage.

Thanks you again,
Till

tclineks’s picture

You say there you need this for pages but mention theming a module as well.

Still unclear on your goals -- I'm not clear if you're trying to accomplish something for specific existing pages or for a module you're writing.

If the latter: http://drupal.org/node/306

Let me know