Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

google translation button gives smarty comilation error

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
shahab82
Smarty n00b


Joined: 18 Nov 2017
Posts: 4

PostPosted: Mon Dec 18, 2017 9:08 am    Post subject: google translation button gives smarty comilation error Reply with quote

Hi,

I am trying to add translation button on my website. Below is code.


Code:

<div id="google_translate_element"></div>

<script type="text/javascript">

function googleTranslateElementInit() {
  new google.translate.TranslateElement({pageLanguage: 'en', layout: google.translate.TranslateElement.InlineLayout.SIMPLE}, 'google_translate_element');
 
}

</script>

<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>


I am getting smarty compilation error

Code:
Syntax error in template "modules/mymodule/views/templates/front/mymodule.tpl" on line 78 "new google.translate.TranslateElement({pageLanguage: 'en', layout: google.translate.TranslateElement.InlineLayout.SIMPLE}, 'google_translate_element');" - Unexpected ": ", expected one of: "}"


.
Code:



Any solution?

-Thanks
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Mon Dec 18, 2017 10:05 am    Post subject: Reply with quote

A Smarty template requires that one observe the following requirement:

Smarty uses braces for itself.

If braces are needed for other reasons, such as javascript or CSS, they must have interior whitespace, or the code be enclosed in {literal} and {/literal} tags.
Code:
{literal}<script type="text/javascript">

function googleTranslateElementInit() {
  new google.translate.TranslateElement({pageLanguage: 'en', layout: google.translate.TranslateElement.InlineLayout.SIMPLE}, 'google_translate_element');
}

</script>{/literal}
or
Code:
<script type="text/javascript">

function googleTranslateElementInit() {
  new google.translate.TranslateElement(
    {
      pageLanguage: 'en',
      layout: google.translate.TranslateElement.InlineLayout.SIMPLE
    },
    'google_translate_element'
);}

</script>
Back to top
View user's profile Send private message
shahab82
Smarty n00b


Joined: 18 Nov 2017
Posts: 4

PostPosted: Mon Dec 18, 2017 12:21 pm    Post subject: Reply with quote

Many thanks. It works Smile
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Dec 19, 2017 2:42 am    Post subject: Reply with quote

No need to be overzealous with formatting, enough that you insert a space between "{" and following letter to escape Smarty parser.
However, a better approach would be to either use {literal} block or wrap code into a function.
I'm using {include … inline} and keeping each insert in its own file, wrapped in {literal}'s if necessary.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP