1

我有管理面板,我可以在其中创建类别。当我创建类别时,我会在 htdocs 中自动创建文件夹...例如... categorie/first , categorie/second 等等

而我想要做的是,当我创建这些文件夹时,我会使用我需要的代码自动创建 index.php ......第一个变体是使用文件函数......但是我该如何输入字符串 php 命令?第二个..我猜是正确的,是创建这个模板..当我创建这些文件夹时,它会复制到我创建的目录......

那你怎么看?

编辑...但是我需要在当前类别打开的情况下存储在此代码变量中。

4

2 回答 2

1

一种解决方案是这样编写代码:

// Creates a template of category
$page = <<< EOT

<html>
    <head>
        <title>Category {$category_name}</title>
        (...)

    </body>
</html>
EOT;

$f = fopen($new_directory . "/index.php", 'w');
fwrite($f, $page);
fclose($f);

记得放EOT;在文档的最左侧。有关详细信息,请参阅heredoc 语法

此代码是一个示例,您当然需要检查 fopen/fwrite 是否成功。

另一种解决方案可能是在另一个带有模板标记的文件中使用模板。

这样,您将拥有一个名为 template_category.txt 的文件,其中包含:

<html>
    <head>
        <title>Category %category_name%</title>
        (...)

    </body>
</html>

然后,在您的 PHP 脚本中,您将用您的值替换模板标记:

$template = file_get_contents("template_category.txt");

$to_replace = array(
    '%category_name%',
    (...)
);

$replace_by = array(
    $category_name,
    (...)
);

$page = str_replace($to_replace, $replace_by, $template);

然后像上面那样在 index.php 上写 $page 。

于 2012-09-09T15:08:35.087 回答
0

尝试使用 php- 和 html- 代码的模板文件,并使用一些标记指定所需的变量,例如:

in template.php
<?php
$category_id = #TOKEN_CATEGORY_ID#;
...
?>

然后将此文件复制到新类别的文件夹并在#TOKEN_CATEGORY_ID# 上执行 preg_replace()。

于 2012-09-09T15:19:46.143 回答