Key hash for epp template
How to port the following code from the erb template to epp:
<%- if @mime_types -%>
# Custom additional mime types
<%- @mime_types.sort_by {|key,value| key}.each do |key,value| -%>
<%= key %> <%= value %>;
<%- end -%>
<%- end -%>
or how to work with key value hashes in epp templates. for example, error returning the following code " Invalid EPP: syntax error in '|' "
# mime.types.epp
<%- | Hash[String, String] $nginx::config::mime_types | -%>
<% include stdlib -%>
<% $nginx::config::mime_types.keys.sort.each |$key| { -%>
<%= $key %> <%= $nginx::config::mime_types[$key] %>
<% } -%>
manifests:
# manifests/config.pp
class nginx::config {
$mimetypes=lookup('nginx::mimetypes')
file { "${nginx::params::conf_dir}/mime.types":
ensure => file,
content => epp("${module_name}/mime.types.epp"),
}
hiera:
nginx::mimetypes:
video/ogg: 'ogv'
puppet version:
#puppet --version
5.0.1
Many thanks.
+3
source to share
1 answer
1) Can't use model $classname::subclassname::lookup_variable
in epp template for hashes
2) Must be used <%- | Hash[String, String] $mime_types | -%>
and
<% include stdlib -%>
only when running the epp template
correct files:
# manifests/config.pp
class nginx::config {
$mimetypes=lookup('nginx::mimetypes')
file { "${nginx::params::conf_dir}/mime.types":
ensure => file,
content => epp("${module_name}/mime.types.epp", { mimetypes => $mimetypes }),
}
# mime.types.epp
<%- | Hash[String, String] $mimetypes | -%> # 1 line
<% include stdlib -%> # 2 line
# some code...
<% $mimetypes.keys.sort.each |$key| { -%>
<%= $key %> <%= $mimetypes[$key] %>
<% } -%>
0
source to share