Javascript string literals cannot contain a bare (i.e., unescaped newline). When jinja2 interpolates the values of article[0].title into the template you get a literal newline that confuses the javascript parser. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals
If you can guarantee that the value does not contain a single or double quote just use the '%r'|format() trick to escape the newline and carriage-return. But it would be safer to use a filter like the Flask tosjon filter mentioned by Markus in his reply (which I just now realized was a reference to Flask and not a Jinja2 feature). Alternatively, "import json" into your python and use the json.dumps() function. On Mon, Mar 24, 2014 at 10:42 PM, Zarey Chang <[email protected]> wrote: > may be i was wrong,I know the reason: > after format the article[0].title, the result contains invisible '\r\n'. > if alert article[0].title which contains '\r\n', the alert will fail. > > WTF good debug skill '{{%r}}', thanks sharing. > > > I will remove the '\r\n' this moment, it seems easy by googling way. > or you have some magic skill to replace the invisiable '\r\n' with '<br > \>'? > > thanks very much. > > > > > 2014-03-25 11:54 GMT+08:00 Zarey Chang <[email protected]>: > > Dont care that, article is array of instance of 'articel',so article[0] is >> the firest instance of article.(what bad i mistake,so sorry). >> --- >> tojson also cant work with me, the same error with you getting. so, give >> up this solution. >> --- >> >> Now i found that the code below really can't work: >> alert("<p>mysomesting</p>"); or >> alert("<p>mysomesting</p>"); >> this code work fine: >> alert("mysomestring"); >> >> I think this may be the real reason for the problem. >> >> >> >> >> 2014-03-25 11:31 GMT+08:00 Kurtis Rader <[email protected]>: >> >> Your original problem statement was >>> >>> <script type="text/javascript"> >>> alert({{article.title}}); >>> </script> >>> >>> >>> You now say that "article" is a dictionary like object with a member >>> named "0" that has a "title" attribute or member. Those things are not >>> equivalent. >>> >>> You cannot use "{{ article.title }}" as a synonym for "{{ >>> article[0].title }}" unless your "article" object has overrides for the >>> normal double-underscore methods (which is very unlikely). >>> >>> >>> On Mon, Mar 24, 2014 at 7:59 PM, Zarey Chang <[email protected]> wrote: >>> >>>> @Kurtis Rader >>>> >>>> i'm sure the non-buildin type 'article' instance have 'title' attribute.The >>>> 'article' is dict like, so I always use article[0].title instead. >>>> after format 'article[0]', information shown as below: >>>> --- >>>> <!--Article(key=Key('Article', 5905064635924480), >>>> title=u'mytitlestring tag', type=u'Translate')--> >>>> ---(use ' instead) >>>> <!--Article(key=Key('Article', 5905064635924480), >>>> title=u'mysomestring', type=u'Translate')--> >>>> --- >>>> >>>> if i'm sure article[0] contains title property, as:article[0].title >>>> print out 'mysomestring'. >>>> For the non-buildin type, how to alert article[0].title in javascript? >>>> >>>> >>>> @Markus Unterwaditzer >>>> >>>> Did you say that I must use json to dumping the non-buildin type for >>>> jinja2? >>>> if it is, I must redirect my focus to json way. >>>> ( but i think jinja2 could process alert("{{article[0].title}}"); in >>>> some other way. ) >>>> >>>> >>>> >>>> >>>> >>>> 2014-03-24 23:25 GMT+08:00 Kurtis Rader <[email protected]>: >>>> >>>>> The expansion of {{article.title}} is done by the jinja2 template >>>>> engine on the server before the javascript is executed on the client >>>>> (i.e., >>>>> the browser). So the fact you're using javascript to display the value is >>>>> not relevant. Either GAE is affecting jinja2 behavior or your "article" >>>>> object doesn't have a "title" attribute or member (if it's a dict like >>>>> object) or the value isn't what you expect. Try embedding the following in >>>>> your document: >>>>> >>>>> <!-- >>>>> {{'%r'|format(article)}} >>>>> --> >>>>> >>>>> >>>>> That will embed a python representation of the "article" object as a >>>>> comment in the document. Then use your brower "view page source" command >>>>> to >>>>> see what was embedded. For example, I did this for one of my objects and >>>>> got this: >>>>> >>>>> <!-- >>>>> {'last_changed_rfc3339': '2014-03-24T15:04:01Z', >>>>> 'last_changed_rfc822': 'Mon, 24 Mar 2014 15:04:01 GMT', >>>>> ... >>>>> --> >>>>> >>>>> >>>>> Note that the ' is a single-quote, which if you make the >>>>> substitution yields the more familiar: >>>>> >>>>> {'last_changed_rfc3339': '2014-03-24T15:04:01Z', >>>>> 'last_changed_rfc822': 'Mon, 24 Mar 2014 15:04:01 GMT', >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> On Mon, Mar 24, 2014 at 7:31 AM, alex <[email protected]> wrote: >>>>> >>>>>> Hi guys, >>>>>> >>>>>> What i want like below: >>>>>> --- >>>>>> <script type="text/javascript"> >>>>>> alert({{article.title}}); >>>>>> </script> >>>>>> --- >>>>>> this code block was writen in some.html using jinja2 template, >>>>>> working by GAE. >>>>>> it alert nothing, empty content on dialog. >>>>>> if I take the code below instead,it work fine:(but need pass extra >>>>>> variable article.title as "title") >>>>>> --- >>>>>> <script type="text/javascript"> >>>>>> alert({{title}}); >>>>>> </script> >>>>>> --- >>>>>> >>>>>> so 1. ,jinja/jinja2 could only pass int, bool, and so on(these >>>>>> regular type), isn't it? >>>>>> 2. if I want pass the instance "article", do I must dump it by json? >>>>>> 3. Is any other way for code like article.title that passing to >>>>>> javascript? >>>>>> (some times i think this problem may be less relate with javascript.) >>>>>> >>>>>> thx, >>>>>> >>>>>> -- >>>>>> You received this message because you are subscribed to the Google >>>>>> Groups "pocoo-libs" group. >>>>>> To unsubscribe from this group and stop receiving emails from it, >>>>>> send an email to [email protected]. >>>>>> >>>>>> To post to this group, send email to [email protected]. >>>>>> Visit this group at http://groups.google.com/group/pocoo-libs. >>>>>> For more options, visit https://groups.google.com/d/optout. >>>>>> >>>>> >>>>> >>>>> >>>>> -- >>>>> Kurtis Rader >>>>> Caretaker of the exceptional canines Junior and Hank >>>>> >>>>> -- >>>>> You received this message because you are subscribed to a topic in the >>>>> Google Groups "pocoo-libs" group. >>>>> To unsubscribe from this topic, visit >>>>> https://groups.google.com/d/topic/pocoo-libs/Hfu3r1ZOWuw/unsubscribe. >>>>> To unsubscribe from this group and all its topics, send an email to >>>>> [email protected]. >>>>> >>>>> To post to this group, send email to [email protected]. >>>>> Visit this group at http://groups.google.com/group/pocoo-libs. >>>>> For more options, visit https://groups.google.com/d/optout. >>>>> >>>> >>>> >>>> >>>> -- >>>> Zarey C.Y. Chang >>>> >>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "pocoo-libs" group. >>>> To unsubscribe from this group and stop receiving emails from it, send >>>> an email to [email protected]. >>>> To post to this group, send email to [email protected]. >>>> Visit this group at http://groups.google.com/group/pocoo-libs. >>>> For more options, visit https://groups.google.com/d/optout. >>>> >>> >>> >>> >>> -- >>> Kurtis Rader >>> Caretaker of the exceptional canines Junior and Hank >>> >>> -- >>> You received this message because you are subscribed to a topic in the >>> Google Groups "pocoo-libs" group. >>> To unsubscribe from this topic, visit >>> https://groups.google.com/d/topic/pocoo-libs/Hfu3r1ZOWuw/unsubscribe. >>> To unsubscribe from this group and all its topics, send an email to >>> [email protected]. >>> To post to this group, send email to [email protected]. >>> Visit this group at http://groups.google.com/group/pocoo-libs. >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> >> >> -- >> Zarey C.Y. Chang >> > > > > -- > Zarey C.Y. Chang > > -- > You received this message because you are subscribed to the Google Groups > "pocoo-libs" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/pocoo-libs. > For more options, visit https://groups.google.com/d/optout. > -- Kurtis Rader Caretaker of the exceptional canines Junior and Hank -- You received this message because you are subscribed to the Google Groups "pocoo-libs" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/pocoo-libs. For more options, visit https://groups.google.com/d/optout.
