Smarty registers the object and uses the instance code

Source: Internet
Author: User

 

Smarty allows you to access PHP objects through templates. There are two ways to access them. One is to register an object to a template and access it in a form similar to a user-defined function. Another method is to assign an object to the template, and then access the template by accessing other methods similar to the value assignment variable. The first method has a good template syntax and is restricted to several fixed methods and targets as a registration object. This is safer. However, a registered object cannot be repeatedly used and assigned values in its own array. In short, you may choose that method based on your own needs. However, using the first method can minimize the syntax of your companion mode. <Br/> If the security option is activated, no private members or functions can be accessed (starting ). If a member or object with the same name exists, the method is used. <Br/> You can restrict members and functions, but the external solution can access it by listing them as an array of Third-registered variables. <Br/> generally, the method used to pass a parameter to an object through a template is the same as that used to obtain a parameter from a custom function. A Hybrid Array is passed as the first parameter, and the smarty object is the second parameter. If you want to pass a parameter at the same time as a traditional object parameter, set the fourth parameter to false. 

 

<? Php <br/> // The object <br/> class my_object {<br/> function METH1 ($ Params, & $ smarty_obj) {<br/> return "this is my METH1"; <br/>}< br/> $ myobj = new my_object; <br/> // registering the object (will be by reference) <br/> $ smarty-> register_object ("foobar", $ myobj ); <br/> // if we want to restrict access to certain methods or properties, list them <br/> $ smarty-> register_object ("foobar", $ myobj, ar Ray ('meth1 ', 'meth2', 'prop1'); <br/> // if you want to use the traditional object parameter format, pass a Boolean of false <br/> $ smarty-> register_object ("foobar", $ myobj, null, false); <br/> // We can also assign objects. assign by ref when possible. <br/> $ smarty-> assign_by_ref ("myobj", $ myobj); <br/> $ smarty-> display ("index. TPL "); <br/>?> <Br/> template: <br/> {* access our registered object *} <br/> {foobar-> METH1 p1 = "foo" p2 = $ bar} <br/> {* You can also assign the output *} <br/> {foobar-> METH1 p1 = "foo" p2 = $ bar assign = "output"} <br/> the output was {$ output) <br/> {* access our assigned object *} <br/> {$ myobj-> METH1 ("foo", $ bar )}

 

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.