我知道如何从文本模板内部进行字符串替换,就像这样简单
文本文件
Hello ${name}
豪华
[string] $name = "World"
$template = Get-Content 'c:\temp\docs\template.txt'
$expanded = $ExecutionContext.InvokeCommand.ExpandString($template)
$expanded
预期输出
Hello World
这一切都很好,但是如果我需要更高级的东西怎么办?
文本文件
<div>title</div>
<ul>
$(${points} | % { $_ })
</ul>
豪华
[string[]]$points = "First Point", "Second Point"
$template = Get-Content 'c:\temp\docs\template.txt' -force
$expanded = Invoke-Expression $template
$expanded
预期输出
<div>title<div>
<ul>
<li>First Point</li>
<li>Second Point</li>
</ul>
我需要做什么才能在文本文件中创建循环模板?
编辑:
就模板而言,这是我要采用的概念。
<div class="container-fluid">
<div class="page-header">
<h1>${name}</h1>
<p class="lead">${synopsis}</p>
</div>
<div class="row">
<div class="col-md-12">
<h2> Syntax </h2>
</div>
<pre>
<code>${syntax}</code>
</pre>
</div>
<div class="col-md-12">
<h2> Detailed Description </h2>
<div>${description}</div>
</div>
{ifdef links}
<div class="col-md-12">
<h2> Related Commands </h2>
<div>${links}</div>
</div>
{endif links}
{ifdef parameters}
<div class="col-md-12">
<h2> Parameters </h2>
<table>
<tr>
<th>Name</th>
<th>Description</th>
<th>Required?</th>
<th>Pipeline Input</th>
<th>Default Value</th>
</tr>
<tr>
{foreach param in ${parameters}}
<td>$($param.Name)</td>
<td>$($param.Description)</td>
<td>$(param.Required)</td>
<td>$(param.PipelineInput)</td>
<td>$(param.DefaultValue)</td>
{endforeach}
</tr>
</table>
</div>
{endif parameters}
{ifdef notes}
<div class="col-md-12">
<h2> Notes </h2>
<div>${notes}</div>
</div>
{endif notes}
{ifdef examples}
<div class="col-md-12">
<h2> Examples </h2>
{foreach example in ${examples}}
<h3> Example </h3>
<pre>${example.code}</pre>
<div>${example.remarks}</div>
{endforeach}
</div>
</div>
最佳答案
我真的对这个问题的一个好的答案很感兴趣,但我会在评论中扩展我的建议。我相信你自己有能力做到这一点,但这对某人来说可能是有值(value)的。
如果您的文件包含
<div>title</div>
<ul>
${list}
</ul>
然后我们可以这样做来填充列表
$list = ("First Point", "Second Point" | ForEach-Object{"`t<li>$_</li>"}) -join "`r`n"
$template = Get-Content 'c:\temp\docs\template.txt' -raw
$ExecutionContext.InvokeCommand.ExpandString($template)
是的,我知道这不完全是您要问的,但确实有类似的结果。
为什么不把整个文件变成代码呢?
免责声明:我想指出的是,从文件中扩展字符串和执行代码是两件不同的事情。如果有人在您的模板文件中放入恶意代码怎么办?你可以盲目地执行它。现实问题?不。可能的灾难?也许。
我更新了您的示例编辑的一小部分以证明该方法。是的,HTML 格式错误并且很糟糕。它的存在是为了表明我的意思。
示例文件
'<div class="container-fluid">'
' <div class="page-header">'
" <h1>$name</h1>"
" <p class=`"lead`">$synopsis</p>"
' </div>'
' <tr>'
foreach($param in $parameters){
" <td>$($param.Name)</td>"
" <td>$($param.Description)</td>"
}
' </tr>'
我选择了那个片段,因为你看到了一个简单的变量、引号中的简单变量和一个循环结构。我在大多数行周围都有单引号,因为它们只会作为标准字符串输出发送。因此,我们使用以下代码:
$name = "Matt"
$synopsis ="Ughhh... stuff"
$parameters = 1,2 | Foreach-object{[pscustomobject]@{Name="$_ Factory";Description="$_ Apples"}}
$template = Get-Content 'c:\temp\docs\template.txt' -raw
Invoke-Expression $template
这将导致以下同样糟糕的输出。
<div class="container-fluid">
<div class="page-header">
<h1>Matt</h1>
<p class="lead">Ughhh... stuff</p>
</div>
<tr>
<td>1 Factory</td>
<td>1 Apples</td>
<td>2 Factory</td>
<td>2 Apples</td>
</tr>
关于loops - 包含循环的 Powershell 文本/html 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29780709/