The Joomla MVC classes |
Written by Administrator | ||||||
Wednesday, 03 March 2010 | ||||||
Page 4 of 5
JViewThe JView object actually implemented is surprisingly simple because it generally delegates most of the view building to the template. If you can you should try to put anything into the View object which isn't about presentation but this is easier to say than to achieve. For example, the View object should obtain the data from the model but how much should it process the result what exactly should it pass on to the template? In most cases it delegates all of the formatting to the template and simply passes on the data. However there is an argument for saying that, for example, in the case of an HTML view the View should generate all of the HTML and the template should restrict itself to formatting. This isn't how things are done mainly because the Joomla page template sets the formatting for the site. The view constructor can be used directly to create an instance of the View but in most cases it is called implicitly by one of the controller or model methods. You can specify an array of configuration parameters including the view name, the charset used by the view's variable escaping functions (see later), an escape callback (see later), an alternate basepath, a template search path, a helper search path and a layout to use. DisplayThe key JView method is display and every derived view does most of its work by customising this method. The base implementation simply loads the template and returns any result that the template returns.The template is expected to generate the output, usually HTML, of the View. Also notice the potential confusion with the Model's display method, which as already explained calls the View's display method. Although this is the normal mode of operation the display method can be called directly. AssignmentFrom the point of view of the template the most important JView methods are the assignment methods - assign and assignref. These are used to add properties to the View object so that the template can make use of them. Notice that template is run in the scope of the View object and so it has direct access to the View's properties. The assign method is the most complicated in that you can assign by name and value: $this->assign('myproperty',value); which is equivalent to $this->myproperty=value; You can also set a collection of properties using a suitable array. For example: $a=array('prop1'=>value1, or you can do the same thing using an object: $obj=new stdClass; This final version is useful when database records are returned as row objects. The assignRef method can only be used to set a single property but because it assigns by reference the value can be almost anything you like and it is more efficient because it doesn't make a complete copy - the property simply references the same object. For example: $obj=new stdClass; assigns a reference to prop1 to the same object as $obj references. In general if you are working with large objects such as collections of database row objects then assignRef should be used in preference to assign which makes fresh copies of all the values specified. The View object also has a get method which will return the value of any property of the View or Model object. You can specify a model as part of the method call or simply use the default. For example: $temp=get("name"); first attempts to find a method called name on the default model. If it succeeds it calls the method and returns the result. If the method doesn't exist then the property with the same name of the view class is returned. There are specific methods to get each of the objects in the MVC system and to set a new model and to add search paths for each type of resource. escapeThere is one last JView-provided resource that has applications throughout Joomla - escape. This method scans a string and performs character escaping- that is it replaces any characters that are not legal within the context provided and replaces them by legal multi-character alternatives. For example in HTML the less than sign should be represented as < if it appear in text that will appear on the page - in HTML the < character always signals the start of a tag. The escape method will by default replace any HTML special characters with their multi-character equivalents. For example: $temp="<HTML></HTML>"; changes all of the occurrences of < to < and of > to >. Clearly you can't simply run the generated HTML through escape as this would change its meaning but any text that will appear on the page should be put through escape. You can also set escape to use the PHP htmlentities built-in function to escape even more characters and to provide full Unicode support - look it up in the PHP documentation. As mentioned earlier you can also provide your own function to be called by escape as part of the constructor's specification. Alternatively you can use the setEscape($callback) to set the function. Notice that this changes all of the existing uses of escape within the view. <ASIN:143021628X> <ASIN:1847198163> <ASIN:B002DOSB4S@ALL> <ASIN:0137012314> |
||||||
Last Updated ( Wednesday, 03 March 2010 ) |