简体   繁体   English

如何在PHPTAL宏中打印未关闭的标签

[英]How to print unclosed tags in PHPTAL Macros

I'm in the process of transitioning a site to using PHPTAL templates, for now I am writing all new pages using templates while leaving the existing pages as is. 我正在将网站转换为使用PHPTAL模板,目前,我正在使用模板编写所有新页面,同时保留现有页面。

The older pages use a standard header and footer, a typical page has it's content generated like this: 较旧的页面使用标准的页眉和页脚,典型的页面具有如下内容:

printHeader();
//print page content
printFooter();

The new pages in PHPTAL will use the same standard header/footer so I'm moving them into macros. PHPTAL中的新页面将使用相同的标准页眉/页脚,因此我将其移入宏。 I want to use the same source HTML for both new and old page headers and footers. 我想对新页面和旧页面的页眉和页脚都使用相同的源HTML。 To accomplish this I want to edit the printHeader() and printFooter() functions to use a template to print the header and footer macros: 为此,我想编辑printHeader()和printFooter()函数以使用模板来打印页眉和页脚宏:

printHeader() {
   $source = '<metal:use-macro="macros.xhtml/header" />';
   $header = new PHPTAL()
   $header->setSource($source);
   echo $header->execute();
}

The problem I'm having is that my header contains the opening <html> and <body> tags, which are closed in the footer. 我遇到的问题是我的标题包含开头的<html><body>标记,这些标记在页脚中关闭。 PHPTAL is throwing an exception because my macros are not valid xml: PHPTAL引发异常,因为我的宏不是有效的xml:

Not all elements were closed before end of the document. Missing: </tal:block></tal:block></tal:block></body></html></tal:block>

What's the easiest way around this? 解决这个问题的最简单方法是什么? I've found a workaround using structure to include these tags as a string but it seems sloppy: 我发现使用结构将这些标签作为字符串包括在内的解决方法,但似乎草率:

<tal:block metal:define-macro="header">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<tal:block tal:content="structure string:&lt;html&gt;" />
   <head>
      head content
   </head>         
   <tal:block tal:content="structure string:&lt;body&gt;" />
      header content

Is there a cleaner way to do this? 有没有更清洁的方法可以做到这一点? Maybe some sort of tal attribute that will allow it to ignore the missing closing tags for <html> and <body> ? 也许某种tal属性将允许它忽略<html><body>缺少的结束标记?

PHPTAL is designed to make outputting of malformed markup as hard as possible, and unclosed tags are malformed markup. PHPTAL旨在使格式错误的标记输出尽可能地困难,而未关闭的标签则格式错误。

You should never have printHeader / printFooter functions. 您永远不应具有printHeader / printFooter函数。 You need to flip this inside-out and have something like printContent() function and call it from template that includes both header and footer: 您需要从内到外翻转,并具有类似printContent()函数的功能,然后从包含页眉和页脚的模板中调用它:

<!DOCTYPE>
<title>Header is here</title>
<body>

  ${php:printContent()}

  <p>Footer is here</p>
</body>

(it doesn't have to be function, you can assign output to a variable, you can call a macro, and macro name can be variable too). (它不一定是函数,您可以将输出分配给变量,可以调用宏,宏名称也可以是变量)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM