I have a JavaScript function which returns a set of objects:
return Func("{{id}}", "{{name}}")
I have a problem with passing strings containing quotes, for instance "Dr.Seuss' "ABC""BOOk"" is invalid syntax.
I tried {{name|safe}} but to no avail. Any solution for this?
If I'm right in assuming that's a JavaScript function call you're trying to interpolate Django templating into, try the escapejs filter instead of safe.
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#escapejs
Related
In the following, myVar contains the string "Today, it's the ${date}".
Furthermore, there is an variable with the name date that contains "1st of October". I expect the following pug syntax to replace the literal ${date} with the date variable content.
span!= myVar
Unfortunately, the example results in
<span>Today, it's the ${date}</span>
Expected result:
<span>Today, it's the 1st of October.</span>
Best regards,
Benedikt
Yes, exactly as #omgninjas pointed out, it is called interpolation and preceded by # in Pug.
However you can't always use it (eg. inside a string). Here are some examples:
sensor is a variable passed by the controller to the view.
Normal interpolation. Works as expected:
<div id=#{sensor} style="width:90%;height:250px;"></div>
Inside a string with Template Literals (don't use these with user supplied values!):
img(src=`/images/${sensor}.png`, style="width:20%")
Inside a string used to denote a function call. Note that you cannot use the ` symbol (back tick aka grave accent used in template literals) with function calls because you would have to ecompass the entire function call . This results in a string which is not going to be executed. You need to use string concatenation.
body(onload="initTemp('"+ sensor +"')")
Here is the official documentation for Pug interpolation:
https://pugjs.org/language/interpolation.html
Hope this helps. Corrections and suggestions always welcome!
To render variables directly in a string in a Pug template, you can use the typical ES6 interpolation. Example (assuming pageTitle is in scope, and passed as template context):
- var pageTitle = `Google | ${pageTitle}`;
Pug interpolates with a hash. #{interpolation}
I am using laravel 4 and I want to pass a data with a view.
I have used this code in a controller.
$view = View::make('settings.editEvent');
$view->bounderyData = $bounderyData;
And I want to check whether this data exists or not in the view settings/editEvent.blade.php
Tried using this..
<script>
if('{{$bounderyData.length()}}'!=null)
console.log('exists');
</script>
Error :
Array to string conversion error
How can I check the existence ?
Do not assign the data to the View variable, but instead, pass it along using with as Laravel requests you to use:
$view = View::make('settings.editEvent')
->with('bounderyData', $bouderyData);
Actually both of the snippets work the same way. You can either pass data using with() method or by assigning it to view as property. So it doesn't really matter. But it looks like you are using some weird syntax because you are trying to access method length() using dot syntax inside Blade echo statement. Try:
if({{count($bounderyData)}}!=null)
console.log('exists');
or something similar. Remember that everything inside {{}} is going to be echo'ed by PHP. So if you have some sort of array there you may either want to count number of elements or maybe cast it to JSON and then decode it inside Javascript. If you still have problem, let us know what is the issue.
I'm propagating several variables to my template, some of which are strings that I want to use and manipulate through javascript. I'm setting this value as such:
var venue_toc = "{{terms_and_conditions}}";
terms_and_conditions is capable of having quotes in it itself, so I need to escape those in order for things to work as expected. What is the correct way to do that when bringing values up from Django?
Try with built-in filter escapejs
var venue_toc = "{{terms_and_conditions|escapejs}}";
Using JsViews is it possible to have a converter precede a helper function in a data-link property? Somehow like this..
<div data-link="{myConverter:~myFunction('param1'):}"></div>
Thanks!
If you mean that you want the converter to convert the value of the param1 field of your data object, and then pass it to the myConverter function, then no. The converter will process the result of the expression ~myFunction(...).
But you can create an equivalent helper function to your converter, and then chain the helpers:
<div data-link="~myFunction(~myConvert(param1))></div>"
BTW this will be data linked so that it updates when the param1 field changes. - I'm not sure if you meant to put quotes around 'param1'. If 'param1' is a string literal you are passing in, then data-linking to it won't have any reason to update, so you could just write:
<div>{{:~myFunction(~myConvert('param1'))}}</div>
By JSON text, I mean the return value of JSON.stringify. I know how to do this with JSON object, but I couldn't figure out how to do this with JSON text (add new attribute/element, say "sn":"1" to JSON text, but its structure is kept and I don't need to stringify it again), can anyone help me?
Thanks!
I don't know why you'd want to do this - why not just add the property before you stringify it?
But if you must, given a string that contains JSON:
var myJSON = '{"prop1":"val1","prop2":"val2"}';
You can easily add a property to the beginning by doing this:
myJSON = '{' + '"sn":"1",' + myJSON.substr(1);
Or add it to the end:
myJSON = myJSON.replace(/}$/, ',"sn":"1"' + '}');
Or use whatever other combination of String manipulation functions takes your fancy...
If you want to add the new property in a specific place within the string, say inside a nested object or array or something, well, again some kind of regex or combination of .indexOf() and .substr() or something could do it, but really I think it's nuts to approach it this way.
Obviously the above code can be wrapped up in a function, and '"sn":"1"' can be replaced with a parameter or variable name or whatever - but why?
Note also that I've assumed above that there will be at least one existing property and inserted a comma accordingly- up to you to make that smarter if you want to allow for empty objects.
P.S. There aren't "JSON strings" and "JSON objects": all JSON is a string. In JavaScript one way of creating objects is with the object literal syntax that inspired JSON, but there's no such thing as a JSON object.
It makes no sense to do it the way you're suggesting... just turn it back into an Object, add your field and stringify it again! Or am I missing something?
You're going to have to parse it somehow. The most straightforward way is probably un-stringifying it to object/array/literal data. But if you don't want to do that, you could either use regular expressions, or methods of the String object like substr to manipulate the string directly.