5

I see something like the below in magento

<?php echo $this->__('* Required Fields') ?>

Why can't I simply use

<?php echo "Required Fields" ?>

The output seems to be the same in both cases. What is the function of the $this->__ . I see this usage in a lot of places and would like to clear any misunderstanding I may be having.

Any help greatly appreciated.

Vinu D
  • 141
  • 1
  • 2
  • 11

1 Answers1

10

The main difference is that this:

<?php echo $this->__('* Required Fields') ?>

Allows for front end translations through a CSV file or through the Admin Panel (Configuration -> Developer -> Translate Inline)

Where as:

<?php echo "Required Fields" ?>

Will not allow you to use the translation features.

SR_Magento
  • 5,209
  • 13
  • 62
  • 103
  • Best practice is to use the $this->__('echo text here') - your site is English now but maybe down the line it will require additional languages. The code you posted gets the username of a logged in user. – SR_Magento Jul 14 '15 at 18:31
  • Thanks - that helps. I tried searching for the nature of escapeHtml ? What exactly is this doing ? why couldn't I simply have done $this->getUsername(). – Vinu D Jul 14 '15 at 18:34
  • Post a new question :) – SR_Magento Jul 14 '15 at 18:38
  • Matter of time before someone said that - I was asking for it :) – Vinu D Jul 14 '15 at 18:39
  • 2
    Even if your site is only in English, to might want to change texts per theme or store without having to change them in all templates. Regarding escapeHtml(): You want to be sure that it also works if the translation has funny characters. See: http://magento.stackexchange.com/questions/569/how-to-escape-output-data – Fabian Schmengler Jul 21 '15 at 12:59
  • Escaping HTML is for security (e.g. protecting against injection attacks) whereas __('xxx') is for translating static strings into other languages (the string xxx is the key in the CSV file). – Alan Kent Jul 22 '15 at 04:48