Using Templates with PHP

Asked

Viewed 135 times

1

Hail! I have a system with template that I mounted on PHP represented by the following structure:

require("header.php");
require("content.php");  //carregado dinamicamente via GET
require("footer");

Everything works well, but I have to manage the CSS and JS of each content. Since the CSS this inserted in header.php and the JS in footer.php, what would be the best way to insert the specific files of each content? The intention would be to load the scripts referring to content specific, but I have problems mainly with JS when I try to insert them directly into the content. I thank you for your strength!

2 answers

1


you can use a variable!

//tens de declarar antes de chamar o arquivo content
$script = '<script type="text/javascript" src="source.js"></script>';

In the archive php content.

[..]
 echo (isset($script)) ? $script : '';
[..]

if you have multiple scripts you can use an array

  • I am currently using a similar solution. I have to create three files with the same name, however different extensions (php content., content js., content css.). My question is how performance and safety are compromised.

1

If you get via get the content parameter, try this way:

Assuming you are redirecting to a page called home. And you created the files home.css, home.js, home.php

URL will be the parameter via get you quoted.

AT HEADER

    <head>
      <link rel="stylesheet" type="text/css" href="<?php echo $_GET['url'];?>.css">
    </head>

IN CONTENT

<body> 
  require $_GET['url].'php';
</body>

ON FOOTER

<script type="text/javascript" src="<?php echo $_GET['url'];?>.js"></script>
  • 2

    I believe this is a bad habit

  • In fact, today it is working with a system similar to suggested solution. But I was wondering if this is the best solution in terms of safety and performance.

  • 1

    i think performance is serious because whenever you want to create a page you will have to create 3 files home.css, home.js, home.php ?

  • 1

    This is not a good solution, but regarding the question, it is a valid solution. Everything varies from project to project. The correct one would be a css, and the necessary Avascripts.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.