简体   繁体   中英

PHP + Slim PHP Framework: How to dynamically add stylesheets to the document head?

I have created a simple slim application with a login form, dashboard, registration page and account page. Each page has its own route. All the pages use the same header and footer, which is included by adding <?php include __DIR__ . '/header.phtml' ?> <?php include __DIR__ . '/header.phtml' ?> and <?php include __DIR__ . '/footer.phtml' ?> <?php include __DIR__ . '/footer.phtml' ?> to the beginning and end of each page template, respectively, with the page's content placed in between.

I would like to dynamically include different stylesheets depending on the page viewed, but I don't want to just lazily place them in my .phtml files and have them render in the middle of the web page.

I'd like to develop some if/switch function to be placed within the header.phtml file which recognises the current route and renders a link to the appropriate stylesheet. Something like the following code, placed within the document head:

<?php
if($currRoute === "/dashboard") {
  ?>
  <link rel="stylesheet" type="text/css" href="assets/css/dashboard.css">
  <?php
} else if($currRoute === "/register") {
  ?>
  <link rel="stylesheet" type="text/css" href="assets/css/register.css">
  <?php
} else if ($currRoute === "/account") {
  ?>
  <link rel="stylesheet" type="text/css" href="assets/css/account.css">
  <?php
}
?>

Is there any solution for acquiring the value of $currRoute in the above example without some complicated regular expression, any additional languages, class libraries etc.?

Instead of attempting to retrieve the current route, I created a string variable containing the route name within each route callback, then passed that name as an argument to the renderer (in this case I am using Slim's php-view package). Here is an example, passing the string "dashboard" to the dashboard.phtml template file as an argument:

$app->get('/dashboard', function (Request $request, Response $response, array $args) {
  $args['pageTitle'] = "dashboard";
  return $this->renderer->render($response, 'register.phtml', $args);
});

The $args['pageTitle'] key is assigned a keyword identifier as a string, based on the name of the route.

Then, within my header.phtml file I have created a switch statement which compares the value of $args['pageTitle'] with a set of hard-coded strings to determine the appropriate stylesheet to load:

<head>
  <title>Slim 4 PHP Template</title>

  <?php
    if(isset($pageTitle)) {
      switch($pageTitle) {
        case "dashboard":
          echo "<link rel='stylesheet' type='text/css' href='assets/css/dashboard.css'>";
          break;
        case "register":
          echo "<link rel='stylesheet' type='text/css' href='assets/css/register.css'>";
          break;
      }
    }
  ?>
</head>

When visiting the route at " http://example.domain.com/dashboard " the link to the appropriate stylesheet is rendered, like so:

<head>
  <title>Slim 4 PHP Template</title>

  <link rel="stylesheet" type="text/css" href="assets/css/dashboard.css">
</head>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM