2016-02-14 69 views
0

我是Mustache的初学者,我正试图在我的项目中实现它。它没有胡须。我不知道如何执行以下操作(也许是因为我不知道Google的关键字是什么?)。PHP Mustache - 在模板化时通过函数运行数据

如果问题标题不清楚,我很抱歉。我不知道如何以更清晰的方式来表达它。如果有人能帮我编辑标题,那将是最好的。

基本上,我有一个我想要转换成CSS内部样式的背景颜色数组。但我也想在打印出来之前处理该数组,以生成阵列中每个背景颜色的文本颜色。

的颜色阵列:

$colors = array(
    array(
     "name" => "blue", 
     "hex" => "#6CC5FA" 
    ), 
    array(
     "name" => "red", 
     "hex" => "#840715" 
    ), 
    ... 
) 

我有接受输入色彩串并输出该对比文字颜色的功能。目前,我公司采用循环呼应了<style>

我想打印出这样的输出:

<style> 
.bg-blue { 
    background-color:#6CC5FA; 
    color: #333333 /*This is the contrast color*/ 
} 
.bg-red { 
    background-color:#840715; 
    color: #ffffff 
} 
.... 
</style> 

当前方法(显然没有胡子)是这样的:

echo "<style>"; 
for ($i=0;i<sizeof($colors);i++) { 
    echo ".bg-".$colors[$i]["name"]....contrastColor($colors[$i]["hex"])....; 
    //You get the idea 
} 
echo "</style>"; 

我知道我可以使用一个循环推生成的文本颜色为$colors然后用胡子模板,但有没有办法来滑动功能到模板,并告诉小家鼠tache通过它来运行颜色代码?

有没有这样的事情?

$color_template = ' 
<style> 
{{#color}} 
.bg-{{name}} { 
    background-color:{{hex}}; 
    color: {{contrastColor(hex)}} 
} 
{{/color}} 
</style> 
'; 

回答

0

是的!您可以使用the FILTERS pragma来做到这一点:

{{% FILTERS }} 
<style> 
    {{# color }} 
    .bg-{{ name }} { 
     background-color: {{ hex }}; 
     color: {{ hex | contrastColor }}; 
    } 
    {{/ color }} 
</style> 
+0

它是否构建在胡须?如果我记得的话,我在文档中没有看到它。我今晚会检查它是否有效。谢谢。 –

+0

它是几个实现采用的mustache规范的扩展。 – bobthecow