Zend Framework-Zend_View view assist

Source: Internet
Author: User
Tags zend framework
14.4. View helper)

In your view scripts, you often need to execute certain complex functions, such as formatting dates, generating form objects, or displaying links to actions. You can use the helper class to complete these tasks.

You can use $ this-> helpername () to call helper. In this case, zend_view loads the zend_view_helper_helpername class, creates an object instance, and calls its helpername () method. The instance of the object will always exist in the zend_view instance and can be repeatedly called by $ this-> helpername.

14.4.1. Basic helper

Zend_view comes with several helper classes which are used to generate components. Each feature provides the ability to automatically filter variables.

  • Formbutton ($ name, $ value, $ attribs): Generate <input type = "button"/>

  • Formcheckbox ($ name, $ value, $ attribs, $ options): Generate <input type = "checkbox"/>. The $ options parameter is an array, the first value is "checked", and the second value is "unchecked" (default value: "1" and "0 "). If $ value matches the value of "checked", the checkbox is displayed as selected.

  • Formfile ($ name, $ value, $ attribs): Generate <input type = "file"/>

  • Formhidden ($ name, $ value, $ attribs): Generate <input type = "hidden"/>

  • Formpassword ($ name, $ value, $ attribs): Generate <input type = "password"/>

  • Formradio ($ name, $ value, $ attribs, $ options): generates a series of <input type = "button"/>. Each $ options array has one element and the key is the radio value, and the element value is the tag of radio.

  • Formreset ($ name, $ value, $ attribs): Generate <input type = "reset"/>

  • Formselect ($ name, $ value, $ attribs, $ options): Creates a <SELECT>... </SELECT> label, where each <option> corresponds to a $ option array element. The key of an element is the value of option, and the value of an element is the tag of option. The option of $ value is selected by default.

  • Formsubmit ($ name, $ value, $ attribs): Generate <input type = "Submit"/>

  • Formtext ($ name, $ value, $ attribs): Generate <input type = "text"/>

  • Formtextarea ($ name, $ value, $ attribs): Generate <textarea>... </textarea>

The usage is very simple. The following is an example.


<?php
// inside your view script, $this refers to the Zend_View instance.
// 
// say that you have already assigned a series of select options under
// the name $countries as array('us' => 'United States', 'il' =>
// 'Israel', 'de' => 'Germany').
?>
<form action="action.php" method="post">
    <p><label>Your Email:
        <?php echo $this->formText('email', 'you@example.com', array('size' => 32)) ?>
    </label></p>
    <p><label>Your Country:
        <?php echo $this->formSelect('country', 'us', null, $this->countries) ?>
    </label></p>
    <p><label>Would you like to opt in?
        <?php echo $this->formCheckbox('opt_in', 'yes', null, array('yes', 'no') ?>
    </label></p>
</form>

The preceding View Script outputs the following results:


<form action="action.php" method="post">
    <p><label>Your Email:
        <input type="text" name="email" value="you@example.com" size="32" />
    </label></p>
    <p><label>Your Country:
        <select name="country">
            <option value="us" selected="selected">United States</option>
            <option value="il">Israel</option>
            <option value="de">Germany</option>
        </select>
    </label></p>
    <p><label>Would you like to opt in?
        <input type="hidden" name="opt_in" value="no" />
        <input type="checkbox" name="opt_in" value="yes" checked="checked" />
    </label></p>
</form>
14.4.2. helper path

Like the View Script, your controller can also set the Helper path to zend_view. By default, zend_view searches for the helper class under "Zend/View/helper. You can use sethelperpath () and addhelperpath () to define your own path.


<?php
$view = new Zend_View();
$view->setHelperPath('/path/to/more/helpers');
?>

You can use addhelperpath () to add the Helper path. zend_view will use the recently added path. In this way, you can use your helper.


<?php
$view = new Zend_View();
$view->addHelperPath('/path/to/some/helpers');
$view->addHelperPath('/other/path/to/helpers');

// now when you call $this->helperName(), Zend_View will look first for
// "/other/path/to/helpers/HelperName.php", then for
// "/path/to/some/helpers/HelperName", and finally for
// "Zend/View/Helpers/HelperName.php".
?>
14.4.3. Compile a custom helper class

It is easy to compile a custom helper class. Follow the following principles:

  • The class name must be zend_view_helper _ *, and * is the name of helper. For example, if you are writing a class named "specialpurpose", the class name will be zend_view_helper_specialpurpose (case sensitive)

  • The class must have a public method. The method name is the same as the Helper name. This method will be executed when your template calls "$ this-> specialpurpose. In our "specialpurpose" example, the corresponding method declaration can be "Public Function specialpurpose ()".

  • In general, the helper class should not echo or print or have other forms of output. It only needs to return values. The returned value should be filtered.

  • The class file name should be the name of the Helper method. For example, in the "specialpurpose" example, the file must be saved as "specialpurpose. php ".

Put the helper class file under your helper path, zend_view will be automatically loaded, instantiated, persistent, and executed.

The following is an example:


<?php
class Zend_View_Helper_SpecialPurpose {
    protected $_count = 0;
    public function specialPurpose()
    {
        $this->_count++;
        $output = "I have seen 'The Jerk' {$this->_count} time(s).";
        return htmlspecialchars($output);
    }
}
?>

In view code, you can call the specialpurpose helper at any time. It only needs to be instantiated once and will survive in the lifecycle of the zend_view instance.


<?php
// remember, in a view script, $this refers to the Zend_View instance.
echo $this->specialPurpose();
echo $this->specialPurpose();
echo $this->specialPurpose();
?>

The output is as follows:


I have seen 'The Jerk' 1 time(s).
I have seen 'The Jerk' 2 time(s).
I have seen 'The Jerk' 3 time(s).

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.