thinkphp Template Engine Import resource file method detailed _php instance

Source: Internet
Author: User

Generally speaking, the traditional way of Web page import external JS and CSS resource file method is directly in the template file use:

<script type= ' text/javascript ' src= '/public/js/util/array.js ' > <link rel= ' stylesheet ' type= ' text/css '
href= "/app/tpl/default/public/css/style.css"/>

The thinkphp template engine provides a special label to simplify the import above.

1.import Label

The first is the import label, which takes the form of a namespace similar to the thinkphp import function, for example:

<import type= ' js ' file= ' Js.Util.Array '/>

The Type property defaults to JS, so the following effects are the same:

<import file= "Js.Util.Array"/>

You can also support multiple file bulk imports, such as:

<import file= "Js.util.array,js.util.date"/>

Importing an external CSS file must specify the value of the type attribute, for example:

<import type= ' css ' file= ' Css.common '/>

The way above the default import path is the public directory under the root of the Web site, and if you need to specify a different directory , you can use the BasePath property, for example:

<import file= "Js.Util.Array" basepath= "/common"/>

If the imported file contains a "." Number, you can use:

<import file= "Js.util.array#min"/>

Represents the import of a/public/js/util/array.min.js resource file.

The version number of the resource file is also supported for import, for example:

<import type= ' js ' file= ' js.util.array?v=120 '/>

can also be supported when importing multiple files

<import type= ' js ' file= ' js.util.array?125,js.util.date?130 '/>

The Improt tag supports the decision to load , for example, to first determine whether the name variable is set:

<import type= ' js ' file= ' Js.Util.Array ' value= ' name '/>

or more complex, you can even use a function :

<import type= ' js ' file= "Js.Util.Array" value= "Think.get.name|isset"/>

The compiled template cache is:

<?php
if (isset ($_get[' name ')): 
?>
<script type= "Text/javascript" src= "/public/js/util/" Array.js "></script>
<?php
endif;
? >

2.load Label

The second is the load label, which imports the public JS or CSS of the current project by URL, for example:

<load href= "/public/js/common.js"/> <load href= "/public/js/date.js?v=235"/> <load "href=
" /public/css/common.css "/>

You can replace the special template label with the href attribute, for example:

<load href= "!-public-!/js/common.js"/>

The load label does not need to specify the type attribute, and the system automatically determines automatically according to the suffix.
Of course, the load tag also supports conditional judgment calls:

<load href= "/public/js/common.js" value= "name"/>

The system also provides two tag alias JS and CSS usage and load consistency , for example:

<js href= "/public/js/common.js"/> <css href= "/public/css/common.css
"/>

The load label also supports importing multiple resource files, even different types of resource files, at the same time :

<load href= "/public/js/common.js,/public/css/common.css"/>

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.