Template mechanism in Zend framework

Source: Internet
Author: User

Welcome to my blog for discussion

Templates are common in some open-source systems, such as discuz, ecshop, and Wordpress. The so-called template mechanism is to integrate the HTML of all the presentation LayersCodeAnd Related static files are placed under a topic folder, you can freely switch the topic appearance in the background. By default, templates are not supported in Zend framework. Generally, HTML files are placed under the views folder of the application or module. However, we can implement this function through simple modifications.

Create a frontcontroller plug-in

The topic is embodied in the view. The view is determined by the Web request. Therefore, we need to create a Front Controller plug-in. Of course, you can also place it in _ initview of Bootstrap, but you do not know the request controller in Bootstrap. Therefore, you cannot set the corresponding topic based on modlue, Controller, and action. Bootstrap Implementation Method

 1       Protected   Function _ Initview ()
2 {
3 $ View = New Zend_view ();
4 $ View -> Setscriptpath (application_path.ds. 'view'. Ds. 'scripts ');
5 $ View -> Addscriptpath (application_path.ds. 'themes'. Ds. 'default'. Ds. 'view ');
6
7 $ Viewrenderer = Zend_controller_action_helperbroker: getstatichelper (
8 'Viewrenderer'
9 );
10 $ Viewrenderer -> Setview ( $ View );
11 Return $ View ;
12 }

First, register the plug-in application/Bootstrap. php In Bootstrap.

1 ClassBootstrapExtendsZend_application_bootstrap_bootstrap
2{
3Protected Function_ Initfrontcontrollerplugin ()
4{
5$ Front=$ This-> Bootstrap ('frontcontroller')-> getresource ('frontcontroller ');
6$ Front-> Registerplugin (NewApplication_plugin_theme ());
7}
8}

Application/plugins/theme. php

1   Class Application_plugin_theme Extends Zend_controller_plugin_abstract
2 {
3 Public Function Routeshutdown ( $ Request )
4 {
5 $ Theme = New Stdclass;
6 $ Theme -> Foldername = 'simple ';
7 Zend_registry: Set ('Theme ', $ Theme );
8
9
10 $ Bootstrap = Zend_controller_front: getinstance ()-> getparam ('bootstrap ');
11 $ View = $ Bootstrap -> Bootstrap ('view')-> getresource ('view ');
12 $ View -> Setbasepath (application_path. '/views /'. $ Theme -> Foldername );
13
14 $ Layout = $ Bootstrap -> Bootstrap ('layout ')-> getresrouce ('layout ');
15 $ Layout -> Setlayoutpath (application_path. '/layouts /'. $ Theme -> Foldername );
16 }
17 }

For the following URL, the View File structure is as follows:

URL:/blog/viewall
Controller/Action: blogcontroller: viewallaction ()
Layout: Application/layouts/simple/layout. phtml
View: Application/views/simple/scripts/blog/viewall. phtml

Create view Assistant

We also need to use a lot of static files in the template, such as CSS, JS, and images. These images are usually stored in the public folder, to load the corresponding file in the view according to the topic name, we need to make a view Assistant
Application_view_helper_theme

1 ClassApplication_view_helper_theme
2{
3Public FunctionTheme ($ URL)
4{
5$ Theme= Zend_registry: Get ('Theme ');
6$ Baseurl= "/Themes /{$ Theme-> Foldername }";
7Return $ Baseurl.$ URL;
8}
9}

You can also put the theme name in the session.

1   $ Session = Zend_registry: Get ('zend _ session ');
2 If (! Isset ( $ Session -> Theme ))
3 $ Session -> Theme = 'simple ';
4
5 // Obtain it if needed, in viewhelper
6 $ Session = Zend_registry: Get ('zend _ session ');
7 $ URL = $ Baseurl . $ Session -> Theme. $ Content ;

Use this assistant in the view

 
Echo'$ This-> Theme ('/images/smiley.gif'). '"alt =" Smiley "> ';

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.