CakePHP uses Javascripthelper to introduce JS files

Source: Internet
Author: User
Tags naming convention

The content of the head part of the page is mainly controlled by HtmlHelper in CakePHP, while the JS part is controlled by Javascripthelper, which is set in controller: var $helpers = Array (' Html ', ' Javascript '), you can use the Javascripthelper object in the CTP file: $javascript. See the text for details.

The premise is that JS files are placed under the Webroot/js directory, the following is an example.

1, $javascript->link ($JsName, 0); echo $script _for_layout;

In the Views/controllers/method name. ctp file, use $javascript->link ($JsName, 0); To load the webroot/js/jsname.js file into the views/layouts/ DEFAULT.CTP's $script _for_layout inside.

2, Echo $javascript->link ($JsName, 1);

This sentence is used directly in the VIEWS/LAYOUTS/DEFAULT.CTP, placed in the head area can be, or placed in the Views/controllers/method name. The CTP file is also available.

3, Echo $javascript->includescript ();

This statement is very bull X, directly to all the webroot/js/all JS files read out, and print out. And the PHP include function is the same feeling, the other statement is generated <script src= "" ></SCRIPT> this generates <SCRIPT>JS1 content, js2 content ....</script The head area of the layout is appropriate.

4, Echo $javascript->includescript ($JsName);

This is the above statement is very similar, just the content of a particular JS file to print out.

Let's understand the code in the Blog example (do not use $scaffold)

  • According to the naming convention, the database table posts corresponds to a Post model class, defined in the app/models/post.php
  • Building control classes in app/controllers/posts_controller.php Postscontroller using the Post model class
  • Control class Postscontroller If the index action is defined, its output is defined in app/views/posts/index.thtml
  • Directory structure

    App

    Therefore, the request Http://.../posts/index use app/controllers/(note that the two directories are not present in the request path) control class Postscontroller action Index to handle, with app/views/posts The/index.thtml response returns the display
    • Models
      • post.php
    • Controllers
      • posts_controller.php
    • Views
      • Posts
        • Index.thtml
        • View.thtml
        • Edit.thtml
        • Add.thtml
        • ...
      • Layouts
        • Default.thtml
  • The Render method is automatically called at the end of the action index to display the index.thtml view with the default layout default.thtml
    • and the layout default.thtml in addition to defining the footer of the page, where the echo $content _for_layout is actually the place to display index.thtml
    • In app/views/layouts/default.thtml, you can customize the layout to replace the default layout
  • The key code in the action index $this->set (' posts ', $this->post->findall ()), so that the array variable $posts in the view index.thtml gets the database table through the model class Post All records of posts
    • is to set the value of the variable named posts in the index.thtml view
    • Where $this->post is the Post model class according to the naming convention
  • Similarly, the request HTTP://.../POSTS/VIEW/3 will find the action view of the control class Postscontroller, return the display with the app/views/posts/view.thtml response, but also with the action parameter $id value is 3
    • The code in the Action View $this->set (' posts ', $this->post->read (null, $id)); Set the variables in the view.thtml view $posts
    • The code in view view.thtml such as echo $posts [' Post '] [' title '] shows the value of the field with ID 3 recorded in table posts (warm reminder that the middle Post is uppercase P)
  • The action edit and add both use the Code $this->post->save ($this->data) to save the parameters of the control class through the model class Post $this->params-owned data
    • $this->data is an alias for $this->params[' data '), which means that the form (form) is submitted via POST

.:: $html::.

Example

    • Maybe the hyperlinks are simpler. Echo $html->link (' hyperlink text ', ' hyperlink address ')
      • If the current view is in the app/views/posts/directory, the ' hyperlink address ' is the relative address of the current view directory
    • Inserting a picture is not difficult echo $html->image (' Image address ', Array (' Alt ' = ' ", ' border ' =" 0 ")) with a picture as a hyperlink slightly more complex, detailed method parameter description please check the Cake manual echo $html-& Gt;link ($html->image (' Picture address '), ' hyperlink address ', NULL, NULL, FALSE)
      • The image is placed in the app/webroot/img/directory, and the ' picture address ' is the relative address of the picture's directory.
      • Put the properties of the image link in the array

CakePHP uses Javascripthelper to introduce JS files

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.