How to introduce CSS styles in CI framework?

王林
Release: 2024-01-16 10:33:06
Original
602 people have browsed it

How to introduce CSS styles in CI framework?

The introduction method of CSS styles in the CI framework requires specific code examples

CI (CodeIgniter) is a lightweight PHP framework that is widely used in Web applications development. In the CI framework, in order to make the web page have good style and layout, we need to introduce CSS styles. This article will introduce how to introduce CSS styles in the CI framework and provide specific code examples.

1. Directly introduce CSS styles into the view file

In the CI framework, the view file (View) is responsible for rendering the HTML page. We can introduce CSS styles directly into the view file to change the appearance and layout of the page. The following are the specific steps:

  1. In the CI framework, view files are generally stored in the application/views directory. Therefore, we need to create a new view file in this directory and name it example_view.php.
  2. In the example_view.php file, add the following code:
<!DOCTYPE html>
<html>
<head>
    <title>示例页面</title>
    <link rel="stylesheet" type="text/css" href="<?php echo base_url('css/styles.css'); ?>">
</head>
<body>
    <h1>这是一个示例页面</h1>
    <p>这是一段示例内容。</p>
</body>
</html>
Copy after login

In the above code, we used <link> tag to introduce CSS style sheets. Among them, the value of the href attribute is dynamically generated through the base_url() function and points to the css/styles.css file. It is assumed here that the CSS style files are stored in the css folder in the root directory of the CI framework.

  1. Next, we need to specify loading the view file in the controller file of the CI framework. Open the corresponding controller file in the application/controllers directory, such as Example.php, and add the following code in the file:
class Example extends CI_Controller {
    public function index() {
        $this->load->view('example_view');
    }
}
Copy after login

In the above code , the index() method is used to load the example_view.php view file.

  1. Finally, enter the URL of the CI framework in the browser, such as http://localhost/ci/index.php/example, you can see the CSS style Sample page.

2. Using the resource loader of the CI framework

In addition to directly introducing CSS styles into the view file, the CI framework also provides a resource loader (Loader) function. Used to load resources such as CSS style sheets and JavaScript files. The following are the specific steps:

  1. First, in the application/config/autoload.php file, find the following code and modify it to:
$autoload['helper'] = array('url');
Copy after login

In the above code, we add the url helper library to the automatically loaded helper list to use the base_url() function in the view file.

  1. In the controller file, there is no need to manually call the resource loader. You only need to modify the code that introduces CSS styles in the view file to the following form:
<!DOCTYPE html>
<html>
<head>
    <title>示例页面</title>
    <?php echo link_tag('css/styles.css'); ?>
</head>
<body>
    <h1>这是一个示例页面</h1>
    <p>这是一段示例内容。</p>
</body>
</html>
Copy after login

In the above code, we use the link_tag() function to dynamically generate the <link> tag and load the css/styles.css file.

  1. Enter the URL of the CI framework in the browser and you will see a sample page with CSS styles.

Summary:

This article introduces two methods of introducing CSS styles into the CI framework: directly introducing CSS styles into the view file and using the resource loader of the CI framework. No matter which method is used, you can easily add styles and layouts to the pages of the CI framework. I hope this article will be helpful to you in dealing with CSS styles in CI framework.

The above is the detailed content of How to introduce CSS styles in CI framework?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!