Mailinglist Archive: yast-devel (62 mails)

< Previous Next >
[yast-devel] Using ERB templates for RichText in Yast

Hi,

I was adding AutoYast support to the registration module and found a nice
improvement
for creating RichText messages.

The current approach is using the Summary module, so for displaying the
overview of the current AutoYast configuration I used code like this:

summary = ""
summary = Summary.AddHeader(summary, _("Registration Settings"))
summary = Summary.OpenList(summary)
summary = Summary.AddListItem(summary, _("Email: %s") % @config.email)
summary = Summary.CloseList(summary)

This does not look nice.

Then I realized that RichText content is actually an HTML subset.
And for rendering HTML Ruby has a native support: ERB templates.

They are used in Ruby on Rails for rendering web pages so it should work
for Yast RichText as well.

I found out that it is actually very easy to use ERB, just create the summary
using
ERB template like for any other RoR application:

summary.erb:

<h3><%= _("Registration Settings") %></h3>
<ul>
<li><%= _("Email: %s") % h(@config.email) %></li>
</ul>

This looks much better! (Notice the h() method for escaping <>& characters in
the value!)


Then simply render it when needed:

erb = ERB.new(File.read("summary.erb"))
# render the ERB template in the context of the current object
# i.e. the @config attribute is accessible for the ERB renderer
summary = erb.result(binding)

And that's it!


I have added support for *.erb files in the latest y2makepot so even the
translatable
strings from *.erb files are correctly extracted to POT files.

Note: real code needs a small trick with specifying the textdomain
for y2makepot, see the full change in yast2-registration [1].


Enjoy!


[1] https://github.com/yast/yast-registration/pull/30/files



--

Ladislav Slez√°k
Appliance department / YaST Developer
Lihovarsk√° 1060/12
190 00 Prague 9 / Czech Republic
tel: +420 284 028 960
lslezak@xxxxxxxx
SUSE
--
To unsubscribe, e-mail: yast-devel+unsubscribe@xxxxxxxxxxxx
To contact the owner, e-mail: yast-devel+owner@xxxxxxxxxxxx

< Previous Next >
This Thread
  • No further messages