Display variable content, not variable name in Ruby on Rails -
i want customize message end user can modify welcome message. like:
welcome #{userinfo["name"]}, out latest member
where userinfo["name"]
variable.
i store message on model column called welcomemsj
.
then trying display message using model. like:
messages = model.first puts messages.welcomemessage
i have output like:
welcome #{userinfo["name"]}, out latest member
but want display:
welcome emmanuel, out latest member
what's correct syntax this?
thanks in advance.
the liquid template engine created kind of use case. have backend user include special string in double curly braces such {{name}}
populate @ render time. example, backend user set welcome message "welcome {{name}}, our latest member.
" stored in database. assuming current_user
variable represents user that's logged in, render customized message following code:
template = liquid::template.parse(messages.welcomemessage) template.render('name' => current_user.name)
another, more dangerous strategy use eval
interpret string code:
puts eval("\"" + messages.welcomemessage + "\"")
this isn't recommended because allow malicious user run arbitrary code. using liquid, maintain control of exact value gets used in place of {{name}}
.
Comments
Post a Comment