开发者

Create design template file in PHP

开发者 https://www.devze.com 2023-03-30 22:20 出处:网络
I intent to create a template PHP file, this template will only serve the design, not the content. The purpose is to decrease the development time in a sense that when creating a new PHP file or new m

I intent to create a template PHP file, this template will only serve the design, not the content. The purpose is to decrease the development time in a sense that when creating a new PHP file or new module, I can only need to concentrate on the main function of that PHP file not the design. Once I created the new file using the template, it should be able to display the consistent design and serve its specific function.

The issue is that I am not sure on how to make the design of the template works and applied to all of the new files created regardless of the location (as long as it is within the root directory).

As an example:

root directory (www.example.com): /
homepage (www.example.com/index.php): /index.php
css file: /style/style.css
template file: /template.php
newly created file (www.example.com/subone/find/css/file.php): /subone/find/css/file.php
another newly created file (www.example.com/subtwo/locate/css.php): /subtwo/locate/css.php

Content of the homepage (which is created base on the template.php, but the CSS file location is hard coded):

<html>
<head>
<title>Testing</title>
<link rel="stylesheet" type="text/css" href="style/style.css" />
</head>
<body>
<div id="header">logo and login form goes here
<div class="nav"> navigation goes here;</div>
</div>
<div id="main">main content goes here;</div>
<div id="footer">footer goes here; </div>
</body>
</html>

but, when I created a new file, /subone/find/css/file.php the location of the css must be changed and specified manually, like this:

<html>
<head>
<title>Testing</title&开发者_Python百科gt;
<link rel="stylesheet" type="text/css" href="../../../style/style.css" />
</head>
<body>
<div id="header">logo and login form goes here
<div class="nav"> navigation goes here</div>
</div>
<div id="main">main content goes here;</div>
<div id="footer">footer goes here;</div>
</body>
</html>

So, what I want to achieve is that, when creating a new file (/subone/find/css/file.php), I don't need to do anything, I can straight away concentrate on the main section:

<html>
<head>
<title>Testing</title>
...style.css is handled automatically
</head>
<body>
<div id="header">logo and login form goes here
<div class="nav"> navigation goes here</div>
</div>
<div id="main">main content goes here;
<?php
//I can continue to edit the file from this line onward
echo "I am concentrating on the main function of file.php right now!!";
?>
</div>
<div id="footer">footer goes here;</div>
</body>
</html>

example page can be seen at (only the desired design): neoborn.kodingen.com

I accept any answers as long as it can achieve my intention (template).

Thank you :)


Why don't you use absolute paths when referring to CSS files and other resources in your template file?

For example:

<link rel="stylesheet" type="text/css" href="/style/style.css" />


There are 2 options,

  1. Use absolute paths for your css files <link rel=stylesheet href="/style/style.css">
  2. Use HTML's <base> element to cause all relative paths on the page relate to it.


I would use a easy to install template engine. That will help speed up development and still give you the freedom to do whatever PHP you like.

Try http://www.raintpl.com/ that should be quick and easy for you to install and get back to coding the pages. If you include it in your PHP inc folder, it will be available for every PHP file you create. So you won't need to add an include line at the top of each PHP file.


Index.php

<?php define('BASE_URL', 'http://localhost'); ?>

Template.php

<link rel="stylsheet" type="text/css" href="<?php echo BASE_URL; ?>/style/style.css ?>" />
0

精彩评论

暂无评论...
验证码 换一张
取 消