0
In the interim between posts at waxjelly, we’ve collectively become addicted to Smarty. In addition to this, we’ve become addicted to extending and customizing smarty to do a bunch of crap that we find ourselves repeating, and essentially building our own custom framework on this humble-for-no-good-reason templating system. In this tutorial, we’ll write our first custom smarty, and get the ball rolling in the right direction for being able to do pretty much anything you want to do with … okay, this post is going to say “Smarty” a lot, in case you can’t already tell. Let’s get started.


引用
  1. Locate your smarty plugins folder (mine is outside the document root, in the smarty/plugins directory)
  2. Create a new file and name it “function.waxjelly_hello.php”
  3. Create another file and name it “hello.tpl”
  4. Create a third file and name it “Index.php”


We’ll start by writing the index.php file, which will do the following:

引用
  1. include the smarty class file
  2. instantiate an object of the smarty class
  3. set the path to our templates directory
  4. set the path to our compile directory
  5. set a smarty variable assignment
  6. display our class


Next, we’ll write our template file, which will do the following:

引用
  1. call our custom smarty function
  2. pass it a static attribute
  3. pass it a smarty variable


Finally, we’ll write our custom smarty function, which will do the following:

引用
  1. receive the parameters sent to it
  2. receive the global smarty object
  3. send an error to the smarty object if the required parameters are not set
  4. display the values of all received parameters in a basic format


Now that we’ve got that down, here are the files associated with this tutorial. Feel free to download, open up, and follow along, but keep in mind that there is some configuration in order to get it to work, which will all be covered in this post.

  1. index.php


  2. index.tpl

         //In this file, we’re going to call the custom function, and pass it values for “greeting” and “name”.
         //NOTE: the “name” attribute is being filled by the assigned variable from the index.php, and the greeting attribute is being hard-coded

         {waxjelly_hello greeting=’Hello’ name=$name}


  3. function.waxjelly_hello.php

         When we’re writing our custom smarty function, it’s name has to start with “smarty_function” and end with “_your_function_name“. All of the attributes are being passed into one array, which we’re calling “params”. We also include access to the parent smarty object with the nifty “&$smarty” var, which will let us pass it values for error messages without breaking the page load.

         function smarty_function_waxjelly_hello ($params, &$smarty) {

         //This line of code makes “greeting” a required attribute, and sends an error through the smarty object if it’s not set…

         if (empty($params[’greeting’])) {
         $smarty->_trigger_fatal_error(”[waxjelly_hello] param ‘greeting’ cannot be empty “);
         return;
         }

         //…since we’re not requiring “name” as an attribute, we’re setting a default value for it if it’s not set

         if (isset($params[’name’])) {
         $name = $params[’name’];
         } else {
         $name = ‘(whoever you are).’;
         }

         //… now we build a string to return to the page calling this function

         $return = ‘<div class=”myclass”>’;
         $return .= $params[’greeting’] . ‘, ‘ . $name;
         $return .= ‘</div>’;

         //Finally, we return the string, which will be compiled and echo’d

         return $return;

         }


That’s it! You’ve just written your first smarty plugin. We’ll be back with some more complex ones, but for now, enjoy cracking these things out to help your template-based site development. If you wanna get some ideas and inspiration (and very usable code in most cases), check out the smarty plugin directory.

Your Friend and Mine,
- Meshach
Tags: , | 引用(0)
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]