user2418838
user2418838

Reputation: 381

Ruby hash syntax

I'm new to ruby and trying to get used to the new syntax. This is a line of code from the project i'm in, it's a simple description on the footer that shows the local company number, but the syntax of the second line is a little confusing to me.

%dt Indonesia
  %dd{ itemprop: 'telephone' }= I18n.with_locale(:id) { t('meta.phone_number') }

so {itemprop: 'telephone'} is just a block that maps a symbol itemprop to the value 'telephone', but then why do you have "=" in between the I18n.with_locale(:id)? what does it do? Also, is the third block {t('meta.phone_number')} a parameter for the I18n.with_locale(:id)? or is I18n.with_locale(:id) even a method call?

I would appreciate any help. Thank you

Upvotes: 0

Views: 122

Answers (1)

Amadan
Amadan

Reputation: 198496

  • %dd: tell Haml to emit a <dd> tag.
  • { itemprop: 'telephone' }: tell Haml that the current tag should have an attribute itemprop with value telephone.
  • =: tell Haml to set the text content of the current tag to whatever Ruby says the rest of the line evaluates to.
  • I18n.with_locale(:id) { ... }: tell Ruby to invoke the method with_locale on I18n, with one parameter (the symbol :id) and a block.
  • t('meta.phone_number'): tell Ruby to invoke the t helper method, with one parameter (string meta.phone_number).

All in all, it should generate something like this:

<dd itemprop="telephone">電話番号</dd>

if 電話番号 was a translation registered for meta.phone_number and the current locale was Japanese.

Upvotes: 2

Related Questions