[英]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' ?>
来包含这些页眉和页脚<?php include __DIR__ . '/header.phtml' ?>
<?php include __DIR__ . '/header.phtml' ?>
and <?php include __DIR__ . '/footer.phtml' ?>
<?php include __DIR__ . '/header.phtml' ?>
和<?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. <?php include __DIR__ . '/footer.phtml' ?>
到每个页面模板的开头和结尾,页面内容放置在它们之间。
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. 我想根据查看的页面动态地包含不同的样式表,但是我不想只是懒惰地将它们放置在我的.phtml文件中,并将它们呈现在网页的中间。
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. 我想开发一些if / switch函数,将其放置在header.phtml
文件中,该文件可以识别当前路由并提供指向适当样式表的链接。 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.? 在上面的示例中,是否有任何解决方案来获取$currRoute
的值,而无需一些复杂的正则表达式,任何其他语言,类库等?
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). 我没有尝试检索当前路由,而是在每个路由回调中创建了一个包含路由名称的字符串变量,然后将该名称作为参数传递给渲染器(在这种情况下,我使用的是Slim的php-view包)。 Here is an example, passing the string "dashboard"
to the dashboard.phtml
template file as an argument: 这是一个示例,将字符串"dashboard"
作为参数传递给dashboard.phtml
模板文件:
$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. 根据路由名称,为$args['pageTitle']
键分配了一个字符串形式的关键字标识符。
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: 然后,在header.phtml
文件中,我创建了一个switch语句,该语句将$args['pageTitle']
与一组硬编码的字符串进行比较,以确定要加载的适当样式表:
<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: 在“ http://example.domain.com/dashboard ”上访问路由时,将呈现到相应样式表的链接,如下所示:
<head>
<title>Slim 4 PHP Template</title>
<link rel="stylesheet" type="text/css" href="assets/css/dashboard.css">
</head>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.