.. contents:: Contents .. section-numbering:: ----------------------------------------------------- Documentation for Horde_Template (Horde 4 or later) ----------------------------------------------------- This package may be deprecated in the future, but as of now large pieces of H4 are rendered with Horde_Template. Setup ===== Within a Horde environment, no specific setup is necessary. To set up the template object: :: $template = $GLOBALS['injector']->getInstance('Horde_Template'); You can proceed with feeding variables into the object (see below for examples). Once you're done feeding in variables, you need to get your PHP code to output the resulting HTML, which you can do in either of the following ways. If your template is in a local string, you can do the following: :: echo $template->parse($template_code); If your template is in a separate file, you should run this command: :: echo $template->fetch($template_file); **Note:** If you make the PHP script do any output of its own, outside of the Horde_Template:: system, it will appear in the output positioned relative to the position of the above fetch() call in the code. Simple Variables ================ PHP: :: // If in php-land you have a variable like: // $foo = 'bar'; // Set it into your template using: $template->set('foo', $foo); Template: :: foo value of ** Note:** The has to be exactly like above, with one space between the foo and />. Any other variation and the value will not be parsed. Unparsed tags are silently removed from the output string. Arrays ====== PHP: :: // In php, build an array to pass to the templates // $foo = array('apples', 'pears', 'peaches'); // To set in the template object $template->set('foo', $foo); Template: :: many foo values:
  • ** Note:** Use a loop tag to go through all foo array elements to pull out all the values of foo. Again the syntax has to be precise or the tags will not be parsed correctly. Arrays With Keys ================ PHP: :: // We now have an array with keys to pass // $foo = array( // 'city' => 'paris', // 'country' => 'france', // 'language' => 'french' // ); // To set in the template object $template->set('foo', $foo); Template: :: many foo values:
  • , located in where is spoken. ** Note:** You must have a loop array to parse through variables with keys. Using the tags on their own without the loop tag will not work properly. If Conditions ============= PHP: :: // Set up the php variable, this checks if a user has been authorized // with the checkUserAuth() function returning a true or false // $is_auth = checkUserAuth(); // Set the 'if' variable into the template object. // **** Note that the third parameter is required to tell the template engine that // the variable will be used in an 'if' test, otherwise the 'if' will _always_ return 'true'. $template->set('is_auth', $is_auth, true); // And we set up another variable for inclusion inside the if statement $template->set('visitors', countvisits()); Template: :: Welcome to our site...
    Today's site statistics are: visitors! ** Note:** as always the syntax has to be precise, and the part within the ... block will be shown only if $is_auth is true. If-Else Conditions ================== PHP: :: $template->set('somename', true); Template: :: Welcome to our site...
    somename is true somename is false ** Note:** The statement must be enclosed in the block. If Conditions and Arrays ======================== PHP: :: $users = array('john', 'peter', 'mary'); $template->set('users', $users); Template: :: Welcome to our site...
    Current users:

    There are no users at the moment
    ** Note:** The statement must be enclosed in the block. Nested Loops and Nested Tags ============================ PHP: :: $categories= array( array('type' => 'fruit', 'items' => array('apple', 'pear')), array('type' => 'veggie', 'items' => array('tomato', 'potato', 'carrot', 'onion')), array('type' => 'thing', 'items' => array('spoon', 'paperbag', 'tool')) ); $template->set('categories', $categories); Template: ::
    I have a . What could it be?
    Output: :: I have a fruit. What could it be? * apple * pear I have a veggie. What could it be? * tomato * potato * carrot * onion I have a thing. What could it be? * spoon * paperbag * tool