PHP html和php渲染功能

时间:2013-11-21 19:05:15

标签: php html eval render

我有一个名为Tag的类,它必须动态地使用HTML脚本呈现PHP。

public function render () {
  $render = '';

  $render .= '<' . $this->tagname;

  //attributes
  foreach ($this->attr as $attr) {
    if(isset($attr['name']) && isset($attr['value'])){
      $render .= ' ' . $attr['name'] . '="' . $attr['value'] . '"';
    }
  }
  if($this->tagtype == 1) $render .= ' />';
  if($this->tagtype == 2) $render .= ' >';
  if($this->tagtype == 0) {
    $render .= '>';
    foreach ($this->content as $content) {
      if(isset($content['type']) && isset($content['data'])){
    //content as HTML
    switch ($content['type']) {
      case 'html':
        $render .= $content['data'];
      break;
      case 'tag':
        $tag = $content['data'];
        $render .= $tag->render();
      break;
      case 'script':
        $render .= file_get_contents($content['data']);
      break;
      default:  break;
    }
      }
    }
    $render .= '</' . $this->tagname . '>';
  }

  return $render;
}

此函数允许干净的HTML插入,其他Tag实例和外部PHP文件,因此我可以使用类似的东西。

$head->addTag(new Tag ('script',array('name' => 'src', 'data' => 'some_source')));
$body->addFile('nav.php');

然后,我只需要运行$site->render()即可显示它。

我在考虑使用eval,但由于通常不安全,我有任何类似的解决方案。

如果你想做这样的话,请告诉我你会用哪个方向?

0 个答案:

没有答案
相关问题