Friday, August 11, 2017

key-value hash for epp template

Leave a Comment

How to porting next code from 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 work with key-value hashes in epp templates. for example next code return error "Invalid EPP: Syntax error at '|'"

# 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.

1 Answers

Answers 1

1) Don't possible use model $classname::subclassname::lookup_variable in epp template for hashes

2) Need use <%- | Hash[String, String] $mime_types | -%> and <% include stdlib -%> only on start 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] %> <% } -%> 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment