Zend Framework framework programming (6): View (3)

Source: Internet
Author: User
Tags uppercase letter

6.9 view Assistant (helper)
View scripts often have complicated things, such as formatting dates and generating form elements. These can be done with our assistants.
The helper class is actually a class starting with zend_view_helper _. The last section of the class name is the assistant name, and the assistant name must be capitalized, this class must have at least one method named after the assistant. The assistant name is usually a camper name, that is, it does not start with an uppercase letter. The class name is a mixed-size writing format. The method name is also a camper name.
The path of the default assistant usually points to Zend/View/helper. The sethelperpath () method is used to re-specify the path. The path is also maintained so that the default assistant can work.
6.9.1zf built-in Assistant
Sample Code:
<? PHP
Echo $ this-> form ('frm1 ', array ('action' => 'action. php', 'method' => 'post'), false ). "/N ";

Echo $ this-> formhidden ('id', 'submited ');

$ Content = 'your name: '. $ this-> formtext ('name', ", array ('SIZE' => 20).' <br> ';
$ Content. = 'password: '. $ this-> formpassword ('pass', ", array ('SIZE' => 20 ));
Echo $ this-> fieldset ('flst', $ content, array ('legend' => 'name: ', 'style' => 'width: 200pt ')). '<br> ';

Echo $ this-> formlabel ('email ', 'Your Email :');
Echo $ this-> formtext ('email ', 'you @ example.com', array ('SIZE' => 32). '<br> ';

Echo 'your country :';
Echo $ this-> formselect ('country', 'us', null, $ this-> countries). '<br> ';

Echo 'Would you like to opt in? ';
Echo $ this-> formcheckbox ('opt _ in', 'yes', null, array ('yes', 'no'). '<br> ';

Echo 'Choose them :';
Echo $ this-> formmulticheckbox ('chkbox', 'A', null, array ('A' => 'vala', 'B' => 'valb ', 'c' => 'valc', 'D' => 'vald'), '<br> '). '<br> ';

Echo 'Choose one :';
Echo $ this-> formradio ('Radio ', 'A', null, array ('A' => 'vala',' B '=> 'valb ', 'c' => 'valc', 'D' => 'vald '),"). '<br> ';

Echo $ this-> htmllist ($ this-> countries). '<br> ';

Echo $ this-> URL (Array ('k1 '=> 'v1', 'k2' => 'v2', 'k3 '=> 'v3 ′)). '<br> ';

Echo $ this-> formtextarea ('ta ', ", array ('rows' => '5', 'cols' => '25 ′)). '<br> ';

Echo $ this-> formfile ('file', ", array (). '<br> ';

Echo $ this-> formbutton ('btn ', 'click', array ('onclick' => "));
Echo $ this-> formsubmit ('OK', 'OK ');
Echo $ this-> formreset ('reset', 'reset ');
?>

6.9.2 action assistant
Allows us to execute action methods in a controller in the View Script.
Example:
<Div id = "sidebar right">
<Div class = "item">
<? = $ This-> action ('assign1 ', 'book');?>
</Div>
</Div>

6.9.3 regional (partial) Assistant
The basic usage of the Regional Assistant is to parse the fragments of another template within its own view range, similar to the nested call of the View Script.
Basic region assistant usage:
Example:
The booklist. php file contains script fragments:
<? PHP
If ($ this-> books ):
?>
<Table border = 1>
<Tr>
<TH> author </Th>
<TH> title </Th>
</Tr>
<? PHP
Foreach ($ this-> books as $ key => $ Val ):
?>
<Tr>
<TD> <? PHP echo $ this-> escape ($ Val ['author'])?> </TD>
<TD> <? PHP echo $ this-> escape ($ Val ['title'])?> </TD>
</Tr>
<? PHP endforeach;?>
</Table>
<? PHP
Else:
?>
<P> There are no books to display. </P>
<? PHP
Endif;
?>
In another view script, use the partial assistant to call the booklist. php file:
<? = $ This-> partial ('booklist. php', array (
'Books '=> array (
Array ('author' => 'hangqing', 'title' => 'book for php'),
Array ('author' => 'hangking', 'title' => 'book for JSP '),
Array ('author' => 'zhanghing', 'title' => 'book for ASP. net'),
)));
?>
<? PHP echo str_repeat ('-', 60). '<br>';?>
<? PHP
$ Model = array (
Array ('key' => 'mammal', 'value' => 'camel '),
Array ('key' => 'bird ', 'value' => 'penguin '),
Array ('key' => 'reference', 'value' => 'asp '),
Array ('key' => 'fish ', 'value' => 'flounder '),
);
?>
<DL>
<? = $ This-> partialloop ('partialloop. phpml', $ model)?>
</Dl>

Partialloop is used to parse the iterable model.
Example:
<? PHP
$ Model = array (
Array ('key' => 'mammal', 'value' => 'camel '),
Array ('key' => 'bird ', 'value' => 'penguin '),
Array ('key' => 'reference', 'value' => 'asp '),
Array ('key' => 'fish ', 'value' => 'flounder '),
);
?>
<DL>
<? = $ This-> partialloop ('partialloop. phpml', $ model)?>
</Dl>
In the above Code, partialloop calls the following partialloop. phtml content:
<DT> <? = $ This-> key?> </DT>
<DD> <? = $ This-> value?> </DD>

6.9.4 placement assistant
Example:
<? = $ This-> doctype ('xhtml1 _ strict ')?>
<? PHP
// Setting META keywords
$ This-> headmeta ()-> appendname ('keyword', 'framework PHP producti ');
$ This-> headmeta ()-> appendhttpequiv ('expires', 'wed, 26 Feb 1997 08:21:57 gmt ')
-> Appendhttpequiv ('pragm', 'no-cache ')
-> Appendhttpequiv ('cache-control', 'no-cache ');
$ This-> headmeta ()-> appendhttpequiv ('content-type', 'text/html; charset = UTF-8 ′)
-> Appendhttpequiv ('content-Language', 'en-use ');
$ This-> headmeta ()-> appendhttpequiv ('refresh', '3; url = http://www.some.org/some.html ');
Echo $ this-> headmeta ();
$ This-> headscript ()-> appendfile ('/JS/prototype. js ')
-> Appendscript ('xx. js ');
// Putting scripts in order
// Place at a particle offset to ensure loaded last
$ This-> headscript ()-> offsetsetscript (100, '/JS/myfuncs. js ');
// Use scriptaculous effects (append uses next index, 101)
$ This-> headscript ()-> appendscript ('/JS/scriptaculous. js ');
// But always have base prototype script load first:
$ This-> headscript ()-> prependscript ('/JS/prototype. js ');
Echo $ this-> headscript ();
// Setting links in a View Script:
$ This-> headlink ()-> appendstylesheet ('/styles/basic.css ')
-> Headlink (Array ('rel '=> 'favicon', 'href '=>'/img/favicon. ICO '), 'prepend ')
-> Prependstylesheet ('/styles/moz.css', 'screen', true );
// Rendering the links:
Echo $ this-> headlink ();
?>
<? PHP
$ This-> placeholder ('foo')-> setprefix ("<ul>/n <li> ")
-> Setseparator ("</LI> <li>/N ")
-> Setindent (4)
-> Setpostfix ("</LI> </ul>/N ");
$ This-> placeholder ('foo')-> set ("some text for later-1 ″);
$ This-> placeholder ('foo')-> bar = "some text for later-2 ″;
Echo $ this-> placeholder ('foo ');

$ Foo = $ this-> placeholder ('foo ');
Echo $ Foo [0]. '<br> ';
Echo $ Foo ['bar']. '<br> ';
?>
<! -Default Capture: append->
<? PHP
$ This-> placeholder ('hoo ')-> capturestart ();
Foreach ($ this-> data as $ datum)
{
?>
<Div class = "hoo">
<H2> <? = $ Datum ['title']?> </H2>
<P> <? = $ Datum ['content']?> </P>
</Div>
<? PHP
}
$ This-> placeholder ('hoo ')-> captureend ();
?>

<? PHP
Echo $ this-> placeholder ('hoo ')
?>
<! -Capture to key->
<? PHP
$ This-> placeholder ('woo')-> capturestart ('set', 'data ');
Foreach ($ this-> data as $ datum ):
?>
<Div class = "woo">
<H2> <? = $ Datum ['title']?> </H2>
<P> <? = $ Datum ['content']?> </P>
</Div>
<? PHP
Endforeach;
$ This-> placeholder ('woo')-> captureend ();
?>

<? PHP
Echo $ this-> placeholder ('woo')-> data;
?>
6.9.4 custom Assistant
Compile and call the custom assistant method:
The class name must, at the very minimum, end with the Helper name itself, using mixedcaps. e. G ., if you were writing a helper called "specialpurpose", the class name wocould minimally need to be "specialpurpose ". you may, and shocould, give the class name a prefix, and it is recommended that you use 'view _ helper 'as part of that prefix: "my_view_helper_specialpurpose ". (You will need to pass in the prefix, with or without the trailing underscore, to addhelperpath () or sethelperpath ()).

The class must have a public method that matches the Helper name; this is the method that will be called when your template CILS "$ this-> specialpurpose ()". in our "specialpurpose" helper example, the required method declaration wocould be "public function specialpurpose ()".

In general, the class shocould not echo or print or otherwise generate output. Instead, it shocould return values to be printed or echoed. The returned values shocould be escaped appropriately.

The class must be in a file named after the helper class. again using our "specialpurpose" helper example, the file has to be named "specialpurpose. php ".

Place the helper class file somewhere in your helper path stack, and zend_view will automatically load, instantiate, persist, and execute it for you.
Example:
<? PHP
// Custom helper "myfieldset"
Echo $ this-> myfieldset ('this my custom helper. ').' <br> ';
Echo $ this-> myfieldset ('this my custom helper. ').' <br> ';
Echo $ this-> myfieldset ('this my custom helper. ').' <br> ';
?>
Call code:
Function customhelperaction ()
{
$ View = new zend_view ();
$ View-> setscriptpath ('view ');
$ View-> sethelperpath ('Views/helpers', 'My _ view_helper ');
Echo $ view-> render ('My _ custom_helper.php ');
}

Author: Zhang Qing (mesh)
From mesh horizon: http://blog.why100000.com
Why 100,000 computer learning networks: http://www.why100000.com

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.