dongwang788787 2012-07-29 07:31
浏览 21
已采纳

php中contentPlaceHolder的等价物

I am using PHP and CodeIgniter. In every view file, I include the header.php, seen below:

<head>
  <link rel="...">
  <script type="text/javascript">...</script>
</head>

Notice that the head tag is closed in this file. Now I want to add a <script> tag in another file without modifying header.php. C# has ContentPlaceHolder that can be used to indicate where tags can be added within head tag. Is there any equivalent in php? If not, How can I achieve this?

  • 写回答

3条回答 默认 最新

  • dongxing1853 2012-07-29 08:50
    关注

    You can easily add other scripts in your header by adding a single variable in your controller like this:

    Controller:

    $data['myScript'] = '<link rel="..."><script type="text/javascript">...</script>';
    $this->load->view('myView',$data);
    

    Then in your header.php add:

    <head>
      <link rel="...">
      <script type="text/javascript">...</script>
      <?php if (isset($myScript)) echo $myScript; ?>
    </head>
    

    This makes a very customizeable template for each view because you can add the scripts only in the views you need them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳