gpt4 book ai didi

javascript - 在 PHP 中附加字符串的问题

转载 作者:行者123 更新时间:2023-11-30 11:35:17 25 4
gpt4 key购买 nike

我正在使用 Codeigniter 并编写了一个方法来创建 css/js 链接,连接它们,然后将它们传递到我的 View 。它之前一直在工作,直到我决定使用最新的 CI 版本创建一个干净的元素副本,但现在它不工作了。

连接数据的方法部分如下:

foreach ($header_css as $item) {
$str .= '<link rel="stylesheet" href="'. base_url() . $item . '" type="text/css" />';
}

JS 文件也有类似的文件。打印上一段代码时,我得到一个空字符串,所以我决定使用 htmlentities:

foreach ($header_css as $item) {
$str .= htmlentities('<link rel="stylesheet" href="'. base_url() . $item . '" type="text/css" />');
}

它的工作...部分。我的意思是,如果我此时打印带有链接的字符串,但现在当我将字符串传递给 View 时,数据将打印在屏幕上而不是添加到头部。所以这是我的观点:

<!doctype html>

<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo (isset($title)) ? $title : ''; ?></title>
<meta name="description" content="Test">
<meta name="author" content="Test">

<?php
# load assets
echo (isset($assets)) ? $assets : '';
?>

<script>var ajax_home = "<?php echo base_url(); ?>ajax/";</script>
</head>

<body>

与 Assets 相呼应的部分是在屏幕上打印变量的内容,而不是将 Assets 解析为 <script>...</script><link rel="stylesheet" ... />

关于为什么会发生这种情况的任何迹象?

谢谢。

最佳答案

The part of the method that concatenates the data is the following:

foreach ($header_css as $item) {
$str .= '<link rel="stylesheet" href="'. base_url() . $item . '" type="text/css" />';
}

There's a similar one for the JS files. When printing the previous piece of code I'm getting an empty string, so I decided to use htmlentities: Below Code is NOT FOR JavaScript, it's for Style sheet

foreach ($header_css as $item) {
$str .= htmlentities('<link rel="stylesheet" href="'. base_url() . $item . '" type="text/css" />');
}

它应该像,

foreach ($header_js as $item) {
$str .= "<script type='text/javascript' src='". base_url() . $item . "'/>";
}

& 你的观点有(咳咳!):

<?php
# load assets
echo (isset($assets)) ? $assets : '';
?>

所以,我的问题是:

  1. $str 变量 在哪里以及为什么您不使用内部 View 而不是 $assets 它应该是 echo (isset($str) )? $str : '';
  2. 我假设您在 Controller 中使用您的函数作为方法并将所有 js 和 css 文件构建存储在其中多变的。所以.. 你返回那个 $str 变量..?

好吧,这是给你的一些例子,希望你能解决你的问题:)

/**
* Build Css or Script link
* @param array $assets [description]
* @param string $type [description]
* @return [type] [description]
*/
function build_assets($assets = array(), $type = 'css')
{
$str = '';
if (!empty($assets) && is_array($assets) && $type == 'css') {
foreach ($assets as $key => $file) {
$str .= "<link type='text/css' rel='stylesheet' href='" . base_url($file) . ".css' />";
}
} elseif (!empty($assets) && is_array($assets) && $type == 'javascript') {
foreach ($assets as $key => $file) {
$str .= "<script type='text/javascript' src='" . base_url($file) . ".js'></script>";
}
}
return $str;
}
/**
* Testing the method
* @return [type] [description]
*/
function index()
{
$data = [];
// get all style sheets
$style = $this->build_assets(['style', 'page', 'form']);
// get all js files
$script = $this->build_assets(['style', 'page', 'form'], 'javascript');
// assign in 'assets' variable
$data['assets'] = $style . $script;
// send to template/view
$this->load->view('ViewTemplate', $data);
}

& 查看代码:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CodeIgniter</title>
<!-- CSS and JavaScript -->
<?php echo isset($assets) ? $assets : '' ?>
<!-- End Here -->
</head>
<body>
<di class="welcome">Say Hello to CodeIgniter!!!</di>
</body>
</html>

关于javascript - 在 PHP 中附加字符串的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44744465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com