在问这个问题之前,我做了一些研究,虽然我运气不好。
我想弄清楚的是我如何能够制作一个模板系统。我知道我可以看看其他人是如何完成的,但我只是想让我得到一个简单的答案。
我希望能够在html中使用类似$Name == "My Name";
的东西,这显然来自PHP文件中的贵重物品。
所以例如,在我拥有的PHP文件中,我有<?php echo $Name; ?>
的宝贵价值
而不是将PHP放入看起来很混乱的html %Name% or {Name}
我希望能够使用 @Named
@ViewScoped
public class ReportingBean extends BasicBean {
...
public void generateUebersichtMaterial() throws FileNotFoundException {
InputStream pathMaster_jrxml;
InputStream pathSubReport_jrxml;
pathMaster_jrxml = getClass().getResourceAsStream("/reports/mainreport.jrxml");
pathSubReport_jrxml = getClass().getResourceAsStream("/reports/subreport.jrxml");
try {
getConnection();
HashMap<String, Object> params = new HashMap<>();
List<ReportMaterialUebersicht> list = new ArrayList<>();
list = reportFacade.getReportMaterialUebersicht();
JasperReport subreport = JasperCompileManager.compileReport(pathSubReport_jrxml);
JasperReport report = JasperCompileManager.compileReport(pathMaster_jrxml);
params.put(JRParameter.REPORT_LOCALE, Locale.FRENCH);
params.put("subReport", subreport);
JasperPrint jasperPrint = JasperFillManager.fillReport(report, params,
new JRBeanArrayDataSource(list.toArray()));
connection.close();
content = JasperExportManager.exportReportToPdf(jasperPrint);
Faces.sendFile(content, "Materialuebersicht.pdf", false);
} catch (Exception e) {
Logger.getLogger(ReportingBean.class.getName()).log(Level.SEVERE, null, e);
}
}
...
}
。
希望有人能理解我的意思,并帮助我。
谢谢
答案 0 :(得分:1)
只需要替换字符串:
public function _output($tpl, $values) {
if(!is_readable($tpl)) {
return ['err'=>'The ('.$tpl.') template is missing or unreadable!'];
}
$output = file_get_contents($tpl);
if($output === false){
return ['err'=>'Error loading template file ('.$tpl.')'];
}
foreach ($values as $key => $value) {
$tagToReplace = "[@".$key."]";
$output = str_replace($tagToReplace, $value, $output);
}
return $output;
}
用法:
_output('path/to/html/file', ['key'=>'value', 'key'=>'value'])
以下是细分:
确保html模板文件存在且可读
将模板文件读入字符串
用实际值替换已知占位符
您的html文件可能如下所示:
<!DOCTYPE html>
<html>
<head>
<title>[@site_title]</title>
<script src="[@polymer_elements]webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="[@polymer_elements]paper-styles/paper-styles.html">
<link rel="import" href="[@polymer_elements]paper-styles/paper-styles-classes.html">
<link rel="import" href="[@polymer_elements]paper-header-panel/paper-header-panel.html">
<link rel="import" href="[@polymer_elements]paper-drawer-panel/paper-drawer-panel.html">
<link rel="import" href="[@polymer_elements]paper-toolbar/paper-toolbar.html">
<link rel="import" href="[@polymer_elements]iron-flex-layout/iron-flex-layout.html">
<link rel="import" href="[@polymer_elements]iron-media-query/iron-media-query.html">
<link rel="import" href="[@custom_elements]universal/ajax-widgets-loader/ajax-widgets-loader.html">
<link rel="import" href="[@custom_elements]universal/ajax-app-loader/ajax-app-loader.html">
</head>
假设您有一个像array('polymer_elements'=>'path/to/polymer/folder/', 'site_title'=>'my site title')
你的php的最终输出将是
<!DOCTYPE html>
<html>
<head>
<title>my site title</title>
<script src="path/to/polymer/folder/webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="path/to/polymer/folder/paper-styles/paper-styles.html">
<link rel="import" href="path/to/polymer/folder/paper-styles/paper-styles-classes.html">
<link rel="import" href="path/to/polymer/folder/paper-header-panel/paper-header-panel.html">
<link rel="import" href="path/to/polymer/folder/paper-drawer-panel/paper-drawer-panel.html">
<link rel="import" href="path/to/polymer/folder/paper-toolbar/paper-toolbar.html">
<link rel="import" href="path/to/polymer/folder/iron-flex-layout/iron-flex-layout.html">
<link rel="import" href="path/to/polymer/folder/iron-media-query/iron-media-query.html">
<link rel="import" href="path/to/polymer/folder/universal/ajax-widgets-loader/ajax-widgets-loader.html">
<link rel="import" href="path/to/polymer/folder/universal/ajax-app-loader/ajax-app-loader.html">
</head>
注意:对于所有说&#34;使用smarty&#34;的人,我认为聪明,所有其他模板引擎都很好。它们是由一个更大的社区开发的,而不仅仅是一个人,但有时我们只需要一个小片段而不是整个库。
答案 1 :(得分:0)
据我了解,您正在寻找str_replace,这样做可以用实际值替换keyword
。我有时会这样使用它:
$template = file_get_contents('test.php');
$name = 'Mrs. Happy Dash.';
//Preps keywords in the template that needs to be replaced with our data.
$keywords = array
(
"{TITLE}" => 'Thank you for visiting',
"{INTRO}" => 'I hope you enjoyed your stay ' . $name,
"{SUB_INTRO}" => '<p style="color:red;">REMEMBER TO PAY!</p>',
"{ORDER_SUMMARY}" => '1x Sky diving class - 10 Euro',
"{HOWTOPAY_TITLE}" => 'You can pay like this:',
"{HOWTOPAY}" => 'Credit card',
"{FOOTER}" => 'Bye bye'
);
//Replaces the data in the template
foreach($keywords as $search => $data){
$template = str_replace($search, $data, $template);
}
echo $template;
您几乎可以使用任何您想要的关键字%NAME%
,#NAME#
。只要你在关键字数组中写下它们。
您用来替换keywords
的数据也可能包含HTML,如果您想根据特定选项/标准显示不同的样式,这非常有用。
以上示例当然非常粗糙,可以通过多种方式进行改进,但它应该提供一种简单易行的方法(并且可以轻松调整以满足更个性化的需求/品味)。 / p>