How to pass Jinja2 variable to javascript - jinja2

How to pass Jinja format variable to javascript
Ex: {{a.Value}}
How to pass the a.value to javascript ?
Getting this variable from py file.

You just pass through render_template as normal:
var = 'data’
return render_template('index.html', var=var)

Related

How can I set HTML string in DOM and bind to an object?

I have markup like this in my angular html component template:
<div *ngIf="htmlTemplate && jsonObj" [innerHTML]="htmlTemplate"></div>
I have my jsonObj variable coming from an endpoint as:
{ firstname: 'Dave' }
and my htmlTemplate string variable coming from an endpoint as:
<strong>Hi....{{firstname}}</strong>
So far, I have managed to get the htmlTemplate rendering in the page, using the [innerHTML] binding, but it's not doing the bind + replacement with the model value from the JSON object.
Q) Is there a way I can make Angular take this html + json obj and render it?
The string output by htmlTemplate will not be interpolated by Angular once it is part of the page. So if the sting is <strong>Hi....{{firstname}}</strong> that is exactly what you will see on the page, including the {{ firstname }} part.
You will need to set the variable in the code file when the result comes from the endpoint:
this.htmlTemplate = `Hello...${result.data.firstname}`
Change this as appropriate for the structure of your returning data.
So the string is, for example, Hello... Dave.

How to extract the Substring from URL present in JSON response using JMeter

I've the below JSON response received from the HTTP request. I want to extract the parameters from the node "url" present in the JSON response.
{"Id":"7S9LyBqyv1e0trKrVuP1OOZGHeg","Url":"https://abcd.com:443/u/custom-response?prov=34545sdf-9013e2e61e66&realmeId=%2Fxxxx","realmeId":"/abcd"}
In the above JSON response, i want to retrieve the value of "prov" which is 34545sdf-9013e2e61e66 using JMeter.
Solution Tried: Used Beanshell to read the response.
String url = vars.get("successURL");
vars.put("responseURL",url.toString());
responseURL = responseURL.replaceAll(":"," ");
log.info("String URL "+responseURL.toString());
Error Message:
attempt to resolve method: toString() on undefined variable or class name: responseURL
I think you need to update this line:
responseURL = responseURL.replaceAll(":"," ");
to something like:
responseURL = vars.get("responseURL").replaceAll(":"," ");
However I don't guarantee that it will work because I don't know how do you get this successURL variable.
What will work is doing everything in one shot using JSR223 PostProcessor and Groovy language which has built-in JSON support, suggested code:
def url = new groovy.json.JsonSlurper().parse(prev.getResponseData()).Url
def params = org.apache.http.client.utils.URLEncodedUtils.parse(new URI(url), 'UTF-8')
params.each { param ->
log.info(param.getName() + '=' + param.getValue())
}
Demo:
More information: Apache Groovy - Why and How You Should Use It
You don't need to use complex Beanshell coding, You can do this easily using - Regular Expression Extractor.
Here the example:-
You can see required value extracted and stored in variable name give in Regular expression extractor:
To understand try out reg-ex, you can use https://regex101.com/

Can you use 'require' in react to import a library?

In my react project, I'm trying to convert XML data from an API call into JSON (using a library called xml-js).
As per the documentation, I'm importing the library in my parent component as follows
const convert = require('xml-js')
and then attempting the convert the API data as follows
const beerList =
'<Product>
<Name>Island Life IPA</Name>
<Volume>300ml/473ml</Volume>
<Price>$10/$13</Price>
<ABV>6.3%</ABV>
<Handpump>No</Handpump>
<Brewery>Eddyline</Brewery>
<IBU/>
<ABV>6.3%</ABV>
<Image>islandlife.png</Image>
<Country>New Zealand</Country>
<Description>Fruited IPA</Description>
<Pouring>Next</Pouring>
<IBU/>
<TapBadge/>
<Comments/>
</Product>'
const beerJs = convert(beerList,{compact: true, spaces: 4})
The errors are telling me that 'convert' is not a function, which tells me that the library isn't being imported. So is the issue with using 'require' syntax, and if so, what alternative would work in react?
which tells me that the library isn't imported
No. If that were the case, you wouldn't even get that far, your require call would throw an error.
Instead, it tells you that convert is not a function - which it isn't! Look at it in a debugger or log it, and you'll see it's an object with several functions inside. You can't call an object like a function.
Take a look at the xml-js docs again:
This library provides 4 functions: js2xml(), json2xml(), xml2js(), and xml2json(). Here are the usages for each one (see more details in the following sections):
var convert = require('xml-js');
result = convert.js2xml(js, options); // to convert javascript object to xml text
result = convert.json2xml(json, options); // to convert json text to xml text
result = convert.xml2js(xml, options); // to convert xml text to javascript object
result = convert.xml2json(xml, options); // to convert xml text to json text
So the solution is to call convert.xml2json and not convert:
const beerJs = convert.xml2json(beerList, {compact: true, spaces: 4})
Or maybe you want an actual object and not a JSON string, then you'd use convert.xml2js (in which case the spaces option is useless):
const beerJs = convert.xml2js(beerList, {compact: true})

how to pass JSON data to view in laravel

I want to pass this JSON data to some view but don't know how its works.
I have used, make view also, and convert this data to JSON and pass other way but it didn't work
$items = Items::all();
return response()->JSON($items);
e.g view is items.create
For Laravel ver 4.2,
You can pass your data to your blade view with a variable (eg:$data) having an array which will be used in your blade.
$flag is variable being used in the JS part of your code, so you can pass that also as an array: Response::json(['param1' => $foo1, 'param2' =>$foo2)]);
In your controller return the view:
return Response::json(['view' => View::make('yourbladename', $data)->render(), 'flag'=>$flag]);
In your JS use the data variables as:
function(data){
$('#DivToAppendHTML').append(data.view); //this appends html blade to the Div having the ID DivToAppendHTML
if(data.flag == 1){ //this uses the second variable passed in controller for any other purpose
$('.classname').remove();
}
}
If you want to create JSON response, you need to convert collection to an array:
$items = Items::all()->toArray(); // $items is array now
return response()->json($items);
If you want to pass some JSON data to a view, do this:
$items = Items::all()->toJson();
return view('items.create', compact('items'));

Django send object as json

Is there a way to send with json (or anything else other than render) an object_list made with paginator? The browser is making a getjson jquery request and the views.py function is supposed to return the object. The reason I want to return a json object rather than render a new page is because I don't want the page to reload
The following views.py code:
searchresults = form.search()#this is a call to a haystack form template
results = Paginator(searchresults, 20)
page = results.page(1)
return HttpResponse(json.dumps(page), content_type='application/json')
Gets this error:
TypeError: <Page 1 of 1> is not JSON serializable
Just use django serialization https://docs.djangoproject.com/en/dev/topics/serialization/
from django.core import serializers
...
return HttpResponse(serializers.serialize("json", [q.object for q in results.page(1).object_list]), content_type='application/json')
You need to create a dictionary that is serializable as #evilx commented or make your own Json by hand.